2016-10-22 21:30 UTC

View Issue Details Jump to Notes ]
IDProjectCategoryView StatusLast Update
0004128CentOS-5gccpublic2010-01-07 23:20
Product Version5.4 
Target VersionFixed in Version 
Summary0004128: Segmentation fault on printf("%s\n", NULL);
Descriptionyum install gcc

echo -e '#include <stdio.h>\nint main() { printf("%s\\n", NULL); return 0; }' > testcase.c
gcc testcase.c -o testcase

Result: Segmentation fault
Expected: (null)
Additional InformationFrom gdb backtrace, seems that printf("%s\n", x) is being silently converted into puts(x) which is fine except when x is NULL, where printf shows (null) but puts() segfaults.
Changing the string, or using the return value avoids this "optimization".
Changing the optimization level -O doesn't affect it.
TagsNo tags attached.
Attached Files




tru (administrator)

[tru@snoopy ~]$ gcc -Wall testcase.c
testcase.c: In function ‘main’:
testcase.c:2: warning: reading through null pointer (argument 2)
testcase.c:2: warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘void *’


smooge (reporter)

The code while giving warnings works on CentOS-4 and Fedora-12. It works on a non-stock CentOS-5 (its a virtual box at a provider so can't tell exactly what changes are kernel related though the glibc and gcc are stock CentOS-5).

gcc -W -Wall -Wfloat-equal -Wundef -Wshadow -Wpointer-arith -Wmissing-format-attribute -g -ggdb testcase.c -o testcase
testcase.c: In function `main':
testcase.c:3: warning: reading through null pointer (arg 2)
testcase.c:3: warning: char format, void arg (arg 2)
<3533 smooge@www->smooge> ./testcase
<3534 smooge@www->smooge> uname -a
Linux www.justintimeadventures.com 2.6.9-89.0.16.EL #1 Tue Nov 3 17:15:02 EST 2009 i686 i686 i386 GNU/Linux

Please give us the rpm versions of: glibc, gcc and kernel.


tru (administrator)

gcc34 (compat-gcc-34-3.4.6-4.i386) on CentOS-5 yields the expected results ie: (null)
gcc (gcc-4.1.2-46.el5_4.1.i386) yields the reported segfault
gcc44 (gcc44-4.4.0-6.i386) same as above^^

-Issue History
Date Modified Username Field Change
2010-01-07 22:27 Angel New Issue
2010-01-07 22:50 tru Note Added: 0010705
2010-01-07 23:09 smooge Note Added: 0010707
2010-01-07 23:20 tru Note Added: 0010708
+Issue History