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++).
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.
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.
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.
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.
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.
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).
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.
*** Bug 70952 has been marked as a duplicate of this bug. ***
(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
(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.
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.