public inbox for glibc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug libc/10818] New: printf("%s\n", NULL) segfaults
@ 2009-10-21  7:14 kir at sacred dot ru
  2009-10-21  7:45 ` [Bug libc/10818] " kir at sacred dot ru
                   ` (5 more replies)
  0 siblings, 6 replies; 8+ messages in thread
From: kir at sacred dot ru @ 2009-10-21  7:14 UTC (permalink / raw)
  To: glibc-bugs

[kir@kir ~]$ cat null.c 
#include <stdio.h>

int main(void) {
	fprintf(stdout, "%s\n", NULL);
	printf("%s%s\n", NULL, NULL);
	printf("%s", NULL);
	printf("\n-newline-\n");
	printf("%s\n", NULL);
	return 0;
}
[kir@kir ~]$ gcc null.c
[kir@kir ~]$ ./a.out 
(null)
(null)(null)
(null)
-newline-
Segmentation fault

Note that only printf with "%s\n" segfaults, while others are fine.

This is because 
(1) call to printf("%s\n", str) is optimized to puts(str)
(2) puts(str) calls strlen(str)
(3) strlen(NULL) segfaults

System info:

$ rpm -q fedora-release gcc glibc
fedora-release-10-1.noarch
gcc-4.3.2-7.x86_64
glibc-2.9-3.i686
glibc-2.9-3.x86_64

PS
I discovered this bug when trying to do something like this:

	/* This should return NULL -- buflen is not big enough */
	printf("%s\n", inet_ntop(AF_INET, &in, buf, 2);

and got SIGSEGV instead of (null) being printed.

-- 
           Summary: printf("%s\n", NULL) segfaults
           Product: glibc
           Version: 2.9
            Status: NEW
          Severity: normal
          Priority: P2
         Component: libc
        AssignedTo: drepper at redhat dot com
        ReportedBy: kir at sacred dot ru
                CC: glibc-bugs at sources dot redhat dot com


http://sourceware.org/bugzilla/show_bug.cgi?id=10818

------- You are receiving this mail because: -------
You are on the CC list for the bug, or are watching someone who is.


^ permalink raw reply	[flat|nested] 8+ messages in thread
[parent not found: <bug-10818-131@http.sourceware.org/bugzilla/>]

end of thread, other threads:[~2014-07-01  5:37 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-10-21  7:14 [Bug libc/10818] New: printf("%s\n", NULL) segfaults kir at sacred dot ru
2009-10-21  7:45 ` [Bug libc/10818] " kir at sacred dot ru
2009-10-21 10:46 ` kir at sacred dot ru
2009-10-21 10:49 ` jakub at redhat dot com
2009-10-21 10:53 ` kir at sacred dot ru
2009-10-21 10:53 ` kir at sacred dot ru
2009-10-21 11:49 ` manu at gcc dot gnu dot org
     [not found] <bug-10818-131@http.sourceware.org/bugzilla/>
2014-07-01  5:37 ` fweimer at redhat dot com

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).