Page MenuHomeFreeBSD

cdefs.h: Add back fallback define for __printf0like
ClosedPublic

Authored by imp on Tue, Jul 2, 5:57 PM.
Tags
None
Referenced Files
Unknown Object (File)
Thu, Jul 4, 10:58 PM
Unknown Object (File)
Thu, Jul 4, 3:32 AM
Unknown Object (File)
Wed, Jul 3, 6:12 AM
Unknown Object (File)
Tue, Jul 2, 11:39 PM
Unknown Object (File)
Tue, Jul 2, 10:21 PM
Unknown Object (File)
Tue, Jul 2, 8:35 PM
Unknown Object (File)
Tue, Jul 2, 6:46 PM
Subscribers

Details

Summary

The format function printf0 is originally a FreeBSD extension. clang has
adopted it for all supported versions. The FreeBSD-specific gcc versions
have it. The stock gcc versions, including ports, do not. bcc and tcc
follow stock gcc behavior. For cases we know don't implement it, define
it away so do not get warnings with -Wsystem-headers. My testing for
67d1a1cd9e77 didn't test that case, so I introduced a regression.

All these compilers need to be considered because __printf0like is used
in err.h and stdlib.h. Since it's used in system headers, it has to
work on all the compilers we support on FreeBSD, not just the ones that
can build FreeBSD itself.

Noticed by: jhb
Fixes: 67d1a1cd9e77
Sponsored by: Netflix

Diff Detail

Repository
rG FreeBSD src repository
Lint
Lint Not Applicable
Unit
Tests Not Applicable

Event Timeline

imp requested review of this revision.Tue, Jul 2, 5:57 PM
imp edited the summary of this revision. (Show Details)
jrtc27 added inline comments.
sys/sys/cdefs.h
361
sys/sys/cdefs.h
361

Doh! Sometimes I think we want is_clang and is_gcc or __is_freebsd_gcc and use those

imp marked an inline comment as done.Tue, Jul 2, 11:38 PM

I need to do some more archaeology on GCC's sources as my reading of my current patch for GCC 12 seems to imply that in stock GCC, __printf already accepts NULL format strings.

So it seems that starting in GCC 11, GCC no longer wants for NULL format strings, instead it requires functions like printf() to use the "nonnull" attribute if they want to enforce this. Contrast the warning you get from GCC 10 vs GCC 11 here:

https://godbolt.org/#g:!((g:!((g:!((h:codeEditor,i:(filename:'1',fontScale:14,fontUsePx:'0',j:1,lang:___c,selection:(endColumn:18,endLineNumber:6,positionColumn:18,positionLineNumber:6,selectionStartColumn:18,selectionStartLineNumber:6,startColumn:18,startLineNumber:6),source:'%23include+%3Cstdio.h%3E%0A%0Avoid%0Afoo(void)%0A%7B%0A++++printf(NULL)%3B%0A%7D'),l:'5',n:'1',o:'C+source+%231',t:'0')),k:33.333333333333336,l:'4',n:'0',o:'',s:0,t:'0'),(g:!((h:compiler,i:(compiler:cg105,filters:(b:'0',binary:'1',binaryObject:'1',commentOnly:'0',debugCalls:'1',demangle:'0',directives:'0',execute:'1',intel:'0',libraryCode:'0',trim:'1',verboseDemangling:'0'),flagsViewOpen:'1',fontScale:14,fontUsePx:'0',j:1,lang:___c,libs:!(),options:'-Wall',overrides:!(),selection:(endColumn:1,endLineNumber:1,positionColumn:1,positionLineNumber:1,selectionStartColumn:1,selectionStartLineNumber:1,startColumn:1,startLineNumber:1),source:1),l:'5',n:'0',o:'+x86-64+gcc+10.5+(Editor+%231)',t:'0')),k:33.333333333333336,l:'4',n:'0',o:'',s:0,t:'0'),(g:!((h:output,i:(compilerName:'x86-64+gcc+9.5',editorid:1,fontScale:14,fontUsePx:'0',j:1,wrap:'1'),l:'5',n:'0',o:'Output+of+x86-64+gcc+10.5+(Compiler+%231)',t:'0')),k:33.33333333333333,l:'4',n:'0',o:'',s:0,t:'0')),l:'2',n:'0',o:'',t:'0')),version:4

And clang has just never warned at all, so printf0 is just an alias for printf on clang on all versions.

Compiler explorer does not have pcc, but tcc with -Wall doesn't raise a warning either, so instead I think what you want is something like this:

/*
  * Like __printflike, but allows fmtarg to be NULL.
  * GCC versions prior to 11 explicitly warned for NULL format strings, but other
  * compilers do not.
  */
#if defined(__clang__) || __GNUC_PREREQ(11, 0)
#define  __printf0like __printflike
#else
#define __printf0like(fmtarg, firstvararg)
#endif

And I will just drop my patches to GCC 12+ to add printf0. This could really benefit from a "is actually GCC for real and not some other compiler pretending to be GCC" helper so that we could truly stub out only for real GCC < 11.

In D45836#1045523, @jhb wrote:

Compiler explorer does not have pcc, but tcc with -Wall doesn't raise a warning either, so instead I think what you want is something like this:

/*
  * Like __printflike, but allows fmtarg to be NULL.
  * GCC versions prior to 11 explicitly warned for NULL format strings, but other
  * compilers do not.
  */
#if defined(__clang__) || __GNUC_PREREQ(11, 0)
#define  __printf0like __printflike
#else
#define __printf0like(fmtarg, firstvararg)
#endif

And I will just drop my patches to GCC 12+ to add printf0. This could really benefit from a "is actually GCC for real and not some other compiler pretending to be GCC" helper so that we could truly stub out only for real GCC < 11.

Nobody else claims to be gcc 11 or newer yet :)

But I totally agree... but maybe here isn't the hill to die on for that...

gcc 11 and newer does the right thing, so just test for that. gcc 10 no longer
is supported as a 'freebsd' build tool, so we don't have to check for the
internal version number.

Updated the commit message locally...

This revision was not accepted when it landed; it landed in state Needs Review.Sat, Jul 6, 4:17 PM
This revision was automatically updated to reflect the committed changes.