简体   繁体   English

printf格式字符串棉绒警告

[英]printf format string lint warning

I'm stuck fixing ancient code, and here is today's issue: 我被困在修复古老的代码,这是今天的问题:

output_file_status = fprintf ( data_file, "%03d%08s%+014.2f%06.3f%",
    LongValue, CharStarValue, Double1, Double2 );

Lint32 produces: Lint32 results in “malformed format string” Lint32产生:Lint32结果为“格式错误的字符串”

1) Do you all agree the format string can not end in a % sign? 1)你们都同意格式字符串不能以%符号结尾吗? I don't believe a standalone % has meaning. 我不认为独立%具有意义。

2) When I either remove the trailing % , or append an additional % , I still get the same warning. 2)当我删除尾随的%或附加一个额外的% ,我仍然收到相同的警告。

This is using the Oracle Pro*C compiler (so the CharStarValue is actually a (char*)VarChar.arr ). 这使用的是Oracle Pro * C编译器(因此CharStarValue实际上是(char *)VarChar.arr)。

Taking it piece by piece: 一步一步地:

"%03d" expects an int . “%03d”期望为int OP supplies a LongValue . OP提供LongValue With a long , specifier should be "%03ld" . 使用long ,说明符应为“%03ld”

"%08s" expects a pointer to char . “%08s”需要一个指向char的指针。 OP supplies CharStarValue . OP提供CharStarValue OK. 好。 But the "0" in the specifier is undefined behavior for a %s . 但是对于%s ,说明符中的“ 0”是未定义的行为 Recommend "%8s" 推荐“%8s”

"%+014.2f" expects a double . “%+ 014.2f”期望为double OK. 好。 Flags '+', '0' are OK. 标志“ +”,“ 0”可以。

"%06.3f" expects a double . “%06.3f”期望为double OK. 好。 Flags '+', '0' are OK. 标志“ +”,“ 0”可以。

"%" should have something after it else behavior is undefined . 未定义行为之后,“%”应包含某些内容。 Recommend removal or "%%". 建议删除或“ %%”。


To OP's 2 points 到OP的2分

1 A proper format should not end with a lone % , but may end with paired %% s. 1正确的格式不应以孤立的%结尾,而可以以配对的%% s结尾。
A standalone % introduces "If a conversion specification is invalid, the behavior is undefined" C11 7.21.6.1 9. 独立的%引入“如果转换规范无效,则行为未定义” C11 7.21.6.1 9。

2 To get rid of all warnings, try "%03ld%8s%+014.2f%06.3f" . 2要摆脱所有警告,请尝试使用"%03ld%8s%+014.2f%06.3f"

Yes, you are correct that % by itself at the end is an error. 是的,您很正确地认为最后的%本身就是错误。 It should be %% to produce one literal % in the formatted output. 在格式化的输出中产生一个文字%应该为%%

It might be that your linter is also complaining about the use of %03d with a long value. 可能是您的短毛猫还在抱怨使用%03dlong值。 That should be %03ld . 那应该是%03ld

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM