Bug 67956 - gcc's printf attribute accepts %m as a format character
Summary: gcc's printf attribute accepts %m as a format character
Status: RESOLVED WORKSFORME
Alias: None
Product: gcc
Classification: Unclassified
Component: c (show other bugs)
Version: unknown
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
: 67957 (view as bug list)
Depends on:
Blocks:
 
Reported: 2015-10-13 15:16 UTC by Christos Zoulas
Modified: 2016-02-25 12:31 UTC (History)
0 users

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


Attachments
patch (1.11 KB, patch)
2015-10-13 15:16 UTC, Christos Zoulas
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Christos Zoulas 2015-10-13 15:16:06 UTC
Created attachment 36503 [details]
patch

According to TOG (http://pubs.opengroup.org/onlinepubs/009695399/functions/fprintf.html), %m is not a legal formatting character for printf(3) like functions. Some implementations (glibc) accept this formatting character as an extension and expand it to the error string via strerror(3) that errno currently points to. Most other c library implementations do not expand %m. 

Attached is a patch that makes printf warn when %m is present and adds a "syslog" attribute that accepts %m. I recognize that this is an incompatible change, and I welcome suggestions how to do this in a less intrusive way.

I tried to mininize the size of the patch; there are better ways to do this, but require more refactoring.
Comment 1 Andreas Schwab 2015-10-13 15:33:19 UTC
*** Bug 67957 has been marked as a duplicate of this bug. ***
Comment 2 Christos Zoulas 2015-10-13 15:44:53 UTC
something is wrong with the bug tracking system, when posting a bug with an attachment as the first post; this is how the dup got created.
Comment 3 jsm-csl@polyomino.org.uk 2015-10-13 16:28:14 UTC
The gnu_printf format attribute is specifically supposed to accept %m; you 
can use -pedantic to disallow it.  Targets can override what the printf 
attribute maps to; see config/i386/msformat-c.c for example; other OSes 
should do likewise so the correct set of formats for each target is 
accepted.  The desire for a syslog format is bug 15338.
Comment 4 Christos Zoulas 2015-10-13 17:19:31 UTC
I believe that -pedantic enables other "pedantic" warnings which I don't belive I want. I can add a specific printf format that does not accept %m, but I would still need to add a syslog format that does (and annotate the syslog functions with that instead of printf).

Perhaps it is possible to have a separate -W switch that will enable this behavior (warn for %m in printf formats), and add a syslog format that allows %m.

I think that we want to encourage people to write portable code, and using %m in printf isn't.

My goal is to differentiate between syslog-like functions that support %m and printf-like functions that don't.

Is adding a separate -W flag to enable warnings for %m in printf and a syslog format acceptable? I think that non-glibc systems will benefit from that.
Comment 5 jsm-csl@polyomino.org.uk 2015-10-13 17:26:17 UTC
There has been some discussion recently of adding -Wformat-pedantic (see 
bug 67479), which would seem reasonable to me.  A syslog format also seems 
reasonable (but we already have bug 15338 tracking that idea).  A 
special-case option for only this single case of non-portable formats 
seems more dubious to me.
Comment 6 Christos Zoulas 2015-10-13 17:31:12 UTC
great, thank you. since this is tracked already you can close it if you want.
Comment 7 Marek Polacek 2016-02-25 12:31:54 UTC
Nothing to do here.