Bug 50154 - attribute printf and scanf should imply attribute nonnull
Summary: attribute printf and scanf should imply attribute nonnull
Status: RESOLVED INVALID
Alias: None
Product: gcc
Classification: Unclassified
Component: c (show other bugs)
Version: 4.6.1
: P3 enhancement
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: diagnostic
Depends on:
Blocks:
 
Reported: 2011-08-22 16:26 UTC by Bruno Haible
Modified: 2011-08-25 11:13 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments
test case (69 bytes, text/x-csrc)
2011-08-22 16:26 UTC, Bruno Haible
Details
test case (corrected) (177 bytes, text/x-csrc)
2011-08-22 16:29 UTC, Bruno Haible
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Bruno Haible 2011-08-22 16:26:43 UTC
Created attachment 25076 [details]
test case

When a function is declared as taking a printf or scanf format string, gcc
gives a warning when a format string with invalid syntax is passed. It should
also give a warning when a NULL format string is passed.

How to reproduce:
Save the test case file as foo.c.
$ gcc -Wformat=2 -c foo.c
[no warning]
$ gcc -Wformat=2 -c foo.c -DDECLARE_NONNULL
foo.c: In function 'foo':
foo.c:10:3: warning: null argument where non-null required (argument 3) [-Wnonnull]

Rationale:
1) ISO C 99 specifies that the printf and scanf format strings must be "multibyte
character sequences"; that excludes NULL.
See ISO C 99 sections 7.19.6.1 paragraph 3 and 7.19.6.2 paragraph 3.
2) Passing NULL as first argument to printf crashes the program on FreeBSD,
Solaris, and other platforms.
Comment 1 Bruno Haible 2011-08-22 16:29:26 UTC
Created attachment 25077 [details]
test case (corrected)
Comment 2 Andrew Pinski 2011-08-24 21:15:37 UTC
I think it is better if the person adds nonnull attribute themselves rather than GCC adding it with the printf format.
Comment 3 Jakub Jelinek 2011-08-25 11:13:59 UTC
Yeah, I agree with that.