0

I'm trying to install intel's psm package from source. When I run make, I get this weird error.

$ 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

I want to suppress the error. It seems like this is a warning more than an error. Is that something I can do here? How would I do that?

kilojoules
  • 9,768
  • 18
  • 77
  • 149
  • 2
    You'll need to modify the Makefile to remove that -Werror flag from the cc command. That is causing the warning to be treated as an error. – mlibby Jul 01 '18 at 02:13

3 Answers3

4

It's treating that as an error because of the -Werror flag being passed to the compiler. That flag tells the compiler to turn any warnings into errors (see the cc1: all warnings being treated as errors) output line. Remove that flag to change the behavior (you'll probably have to edit the makefile).

  • 1
    Thanks. That makes sense. It says `WERROR := -Werror` in `buildflags.mak`. I changed it to `WERROR := -Wno-error` and it compiled. – kilojoules Jul 01 '18 at 02:24
4

Looks like a possible bug in the source, so it makes sense for the compiler to warn, especially with -Wall. (And with -Werror, this is treated as an error.)

"%1d" is never different from "%d": setting the minimum width to 1 is 1 redundant. (I didn't find an SO Q&A about it, but see http://www.kurabiyeaski.com/ym/201501/a_Meaning_of__1d_in_printf_statement_in__c__.html).

"%.1d" would also be redundant: it sets the minimum number of digits to 1 (http://man7.org/linux/man-pages/man3/printf.3.html), but %d already always prints at least 1 digit and maybe a sign.

Anyway, this may indicated that the programmer who wrote this code intended something else, like perhaps only printing a single digit. You can't truncate integer formats with printf, as far as I know, so you'd have to use ports[i] % 10 if you want the last decimal digit, for example.

I'd recommend changing the format string to "%d" and submitting a patch to the authors.


However, snprintf does 0-terminate the string even when it's too big for the buffer, so can safely use the truncated output as a C string. Unlike strncpy().

This means it's safe to ignore this warning as long as the program does work correctly. It's not a crash or buffer overrun waiting to happen.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
  • @kilojoules: Sure, that would be nice. Feel free to copy as much of this answer as you want, if you don't want to retype explanation in your own words. (Or just link to it, or both to credit text you copy.) – Peter Cordes Jul 01 '18 at 02:48
2

This is merely a correction, the compiler needs the keyword 'ignored' not 'ignore'.

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

Many times, it is inconvenient to change Makefiles, so this is a quick and easy way to silence the error. Please be aware, that if the compiler thinks it is an error, careful attention must be given. However, the compiler has no contextual information, so it is erring on the safe side. For instance, the '%s' s(n)printf tag wants to have 512 bytes of space; clearly, not a frequent case; that is where this pragma is useful.

Peter Glen
  • 21
  • 2