【问题标题】:make—can I suppress a format-truncation error?make—我可以抑制格式截断错误吗?
【发布时间】:2018-07-01 02:09:06
【问题描述】:

我正在尝试从源代码安装intel's psm package。当我运行make 时,我得到了这个奇怪的错误。

$ make
...
make libpsm_infinipath.so
make[1]: Entering directory `/home/kilojoules/psm'
cc   -Wall -Werror  -fpic -fPIC -D_GNU_SOURCE -funwind-tables   -O3 -g3   -DNVALGRIND -I. -I/home/kilojoules/psm/include -I/home/kilojoules/psm/mpspawn -I/home/kilojoules/psm/include/linux-x86_64  -c psm_context.c -o psm_context.o
cc   -Wall -Werror  -fpic -fPIC -D_GNU_SOURCE -funwind-tables   -O3 -g3   -DNVALGRIND -I. -I/home/kilojoules/psm/include -I/home/kilojoules/psm/mpspawn -I/home/kilojoules/psm/include/linux-x86_64  -c psm_ep.c -o psm_ep.o
psm_ep.c: In function '__psm_ep_open':
psm_ep.c:1013:27: error: '%1d' directive output may be truncated writing between 1 and 5 bytes into a region of size 4 [-Werror=format-truncation=]
      snprintf(pvalue, 4, "%1d", ports[0]);
                           ^~~
psm_ep.c:1013:26: note: directive argument in the range [0, 65535]
      snprintf(pvalue, 4, "%1d", ports[0]);
                          ^~~~~
psm_ep.c:1013:6: note: 'snprintf' output between 2 and 6 bytes into a destination of size 4
      snprintf(pvalue, 4, "%1d", ports[0]);
      ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
psm_ep.c:1041:27: error: '%1d' directive output may be truncated writing between 1 and 5 bytes into a region of size 4 [-Werror=format-truncation=]
      snprintf(pvalue, 4, "%1d", ports[i]);
                           ^~~
psm_ep.c:1041:26: note: directive argument in the range [0, 65535]
      snprintf(pvalue, 4, "%1d", ports[i]);
                          ^~~~~
psm_ep.c:1041:6: note: 'snprintf' output between 2 and 6 bytes into a destination of size 4
      snprintf(pvalue, 4, "%1d", ports[i]);
      ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
cc1: all warnings being treated as errors
make[1]: *** [psm_ep.o] Error 1
make: *** [libs] Error 2

我想抑制错误。这似乎是一个警告而不是错误。这是我可以在这里做的吗?我该怎么做?

【问题讨论】:

  • 您需要修改 Makefile 以从 cc 命令中删除 -Werror 标志。这导致警告被视为错误。

标签: c makefile intel


【解决方案1】:

由于-Werror 标志被传递给编译器,因此将其视为错误。该标志告诉编译器将任何警告转换为错误(参见cc1: all warnings being treated as errors)输出行。删除该标志以更改行为(您可能必须编辑 makefile)。

【讨论】:

  • 谢谢。这就说得通了。它在buildflags.mak 中显示WERROR := -Werror。我将其更改为 WERROR := -Wno-error 并编译。
【解决方案2】:

看起来源代码中可能存在错误,因此编译器发出警告是有道理的,尤其是-Wall。 (对于-Werror,这将被视为错误。)

"%1d""%d" 没有什么不同:将 minimum 宽度设置为 1 是多余的 1。 (我没有找到关于它的 SO Q&A,但请参阅 http://www.kurabiyeaski.com/ym/201501/a_Meaning_of__1d_in_printf_statement_in__c__.html)。

"%.1d" 也将是多余的:它将最小位数设置为 1 (http://man7.org/linux/man-pages/man3/printf.3.html),但 %d 已经总是打印至少 1 个数字,可能还有一个符号。

无论如何,这可能表明编写此代码的程序员有其他意图,比如可能只打印一个数字。据我所知,您不能使用printf 截断整数格式,因此例如,如果您想要最后一个十进制数字,则必须使用ports[i] % 10

我建议将格式字符串更改为 "%d" 并向作者提交补丁。


但是,snprintf does 0-terminate the string even when it's too big for the buffer,因此可以安全地将截断的输出用作 C 字符串。 Unlike strncpy().

这意味着只要程序运行正常,忽略此警告是安全的。这不是等待发生的崩溃或缓冲区溢出。

【讨论】:

  • @kilojoules:当然,那很好。如果您不想用自己的话重新输入解释,请随意复制此答案。 (或者只是链接到它,或者两者都链接到您复制的文本。)
【解决方案3】:

这只是一个更正,编译器需要关键字'ignored'而不是'ignore'。

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wformat-truncation" 
// Code here   
#pragma GCC diagnostic pop

很多时候,更改 Makefile 很不方便,因此这是一种快速简单的方法来消除错误。请注意,如果编译器认为这是一个错误,必须给予特别注意。但是,编译器没有上下文信息,因此在安全方面它是错误的。例如,'%s' s(n)printf 标签想要 512 字节的空间;显然,不是常见的情况;这就是这个 pragma 有用的地方。

【讨论】:

    猜你喜欢
    • 2011-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-31
    • 1970-01-01
    相关资源
    最近更新 更多