make—can I suppress a format-truncation error?


make—can I suppress a format-truncation error?



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


psm


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



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?





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 1 at 2:13




2 Answers
2



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).


-Werror


cc1: all warnings being treated as errors





Thanks. That makes sense. It says WERROR := -Werror in buildflags.mak. I changed it to WERROR := -Wno-error and it compiled.
– kilojoules
Jul 1 at 2:24


WERROR := -Werror


buildflags.mak


WERROR := -Wno-error



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.)


-Wall


-Werror



"%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"


"%d"



"%.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.


"%.1d"


%d



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.


printf


ports[i] % 10



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


"%d"



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().


snprintf


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.





I should credit you in the pull request, right?
– kilojoules
Jul 1 at 2:43





@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 1 at 2:48







By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

PySpark - SparkContext: Error initializing SparkContext File does not exist

List of Kim Possible characters

Python Tkinter Error, “Too Early to Create Image”