Bug 60523 - Warning flag for octal literals [-Woctal-literals]
Summary: Warning flag for octal literals [-Woctal-literals]
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: c (show other bugs)
Version: unknown
: P3 enhancement
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: diagnostic
Depends on:
Blocks: new-warning, new_warning
  Show dependency treegraph
 
Reported: 2014-03-14 08:53 UTC by David Brown
Modified: 2022-08-29 23:25 UTC (History)
4 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2017-09-28 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description David Brown 2014-03-14 08:53:21 UTC
It would be nice if there were a warning flag that triggered on octal literals.  

Octal literals are rarely used in modern C and C++ code, but can easily be introduced by mistake - "int x = 050;" appears at first reading to set x to 50, but in C and C++ the leading 0 means it is interpreted as octal and therefore sets x to 40 (decimal).

As octal literals are seldom useful, and often confusing or accidental, they are banned by coding standards like MISRA.  But as they are part of the language defined by the C and C++ standards, and are used in some existing code, they obviously cannot be removed.  As a compromise, I would like to propose the introduction of a warning flags "-Woctal" which would produce a diagnostic message when a literal is interpreted as an octal number.  Of preference, this would be included in "-Wextra" (it probably should not be in "-Wall", since octal literals are valid C and C++).
Comment 1 Andrew Pinski 2014-03-14 08:57:59 UTC
Octal literals are very useful for expressing unix/posix file modes like 0777 or even 0666.  So having the warning part of eith -Wall or -Wextra does not make sense.
Comment 2 David Brown 2014-03-14 10:39:10 UTC
Yes, octal literals are traditionally used for posix file modes (I know of no other common usage for them, but of course there may other uses that I haven't heard of).  That is one of the reasons why "-Woctal" should probably not be part of "-Wall".  But I think it is still within the scope of "-Wextra" of warnings that are triggered by valid code, but which often indicate problems.  

However, I am not fussy about its inclusion in -Wextra - it would be convenient for many users and let them take advantage of -Woctal without using it explicitly, but the main enhancement request is for the -Woctal flag itself.
Comment 3 Marek Polacek 2014-03-14 10:45:09 UTC
I agree with Andrew - if we are ever to implement -Woctal, making it a part of -Wextra doesn't sound sensible.  Especially with -Werror it'd be a pain.
Comment 4 Manuel López-Ibáñez 2014-03-14 10:51:42 UTC
I would suggest that you implement this as a plugin. Plugins are particularly useful for enforcing this type of coding standards. If you make a generally useful plugin like one enforcing MISRA standards, it could be distributed with GCC in order to serve as a testcase for the plugins framework.
Comment 5 David Brown 2014-03-14 11:31:41 UTC
I agree that warnings to match something like the MISRA coding standards would be best done as a plugin.

But I believe that in this case, warning on octal literals would be quite a small addition to the main gcc code.  I am not familiar enough with the gcc source code to implement it myself (like many people, I blame lack of time) - I therefore registered this enhancement request in the hope that someone was /is/ familiar with this part of gcc would be able to make the change quickly and easily.
Comment 6 jsm-csl@polyomino.org.uk 2014-03-14 17:32:42 UTC
Octal literals are also used in macro definitions from system headers, so 
care would be needed that a warning doesn't apply to those.

Such a warning should of course not apply to 0 (and maybe more generally 
should not apply to any octal constant consisting of some number of 
leading 0s followed by at most one nonzero digit, as those have the same 
value whether interpreted as octal or as decimal).
Comment 7 Eric Gallager 2017-09-28 16:02:45 UTC
Confirmed, octal literals have confused me in the past, too. A separate -Woctal that's controlled by neither -Wall nor -Wextra would be my preferred solution.
Comment 8 Eric Gallager 2018-01-29 15:39:49 UTC
*** Bug 70952 has been marked as a duplicate of this bug. ***
Comment 9 Eric Gallager 2019-10-29 04:15:38 UTC
(In reply to Eric Gallager from comment #8)
> *** Bug 70952 has been marked as a duplicate of this bug. ***

While this was a mistake, it still might be worth grouping the flag proposed in that bug, -Woctal-escapes, and the flag proposed in this bug, -Woctal-literals, under an umbrella flag called just -Woctal
Comment 10 David Brown 2019-10-29 08:11:03 UTC
(In reply to Eric Gallager from comment #9)
> (In reply to Eric Gallager from comment #8)
> > *** Bug 70952 has been marked as a duplicate of this bug. ***
> 
> While this was a mistake, it still might be worth grouping the flag proposed
> in that bug, -Woctal-escapes, and the flag proposed in this bug,
> -Woctal-literals, under an umbrella flag called just -Woctal

That makes a lot of sense.  I expect users who want one of these warnings would want both, so combining them would save effort for users and gcc developers.
Comment 11 Frank Heckenbach 2022-08-29 23:25:48 UTC
For file modes we have S_IXUSR etc., and most programs don't even need them very often. So I think making it part of -Wextra seems reasonable these days, but I'd welcome such a warning either way.

https://thedailywtf.com/articles/padded-mailers recently reminded me that 0-prefixed octals can cause real problems even for experienced programmers.