代码之家  ›  专栏  ›  技术社区  ›  kilojoules

make–我可以抑制格式截断错误吗?

  •  0
  • kilojoules  · 技术社区  · 8 年前

    我正在尝试安装 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
    

    我想隐瞒这个错误。这似乎是一个警告而不是一个错误。我能在这里做些什么吗?我该怎么做?

    2 回复  |  直到 8 年前
        1
  •  4
  •   Blistering Sheep    8 年前

    因为 -Werror 正在传递给编译器的标志。该标志告诉编译器将任何警告转换为错误(请参见 cc1: all warnings being treated as errors )输出线。删除该标志以更改行为(可能需要编辑makefile)。

        2
  •  3
  •   Peter Cordes    8 年前

    看起来源代码中可能有一个错误,所以编译器发出警告是有意义的,特别是 -Wall 是的。(和 -Werror ,这将被视为错误。)

    "%1d" 从来没有不同于 "%d" :设置 最低限度 宽度为1是多余的。(我没有找到关于它的问题,但请看 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 已经打印了至少一个数字或者一个符号。

    无论如何,这可能表明编写这段代码的程序员另有目的,比如可能只打印一个数字 是的。不能使用 printf ,据我所知,你必须使用 ports[i] % 10 例如,如果您想要最后一个十进制数字。

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


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

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