public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/60194] New: -Wformat should also warn when using %d (instead of %u) for unsigned arguments
@ 2014-02-14 10:48 burnus at gcc dot gnu.org
  2014-02-14 12:27 ` [Bug c/60194] " burnus at gcc dot gnu.org
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: burnus at gcc dot gnu.org @ 2014-02-14 10:48 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=60194

            Bug ID: 60194
           Summary: -Wformat should also warn when using %d (instead of
                    %u) for unsigned arguments
           Product: gcc
           Version: 4.9.0
            Status: UNCONFIRMED
          Keywords: diagnostic
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: burnus at gcc dot gnu.org

cppcheck has the following warning:

(warning) %d in format string (no. 1) requires 'int' but the argument type is
'unsigned int'

I think it would be useful to have the same warning with -Wformat. [One can
argue whether the warning should be always printed with -Wformat[=1] or only
with -Wformat=2 (+ -Wformat-unsigned).]


And of course also the other way round, e.g. using "%lu" or "%lz" with an
argument which is (signed) "long".


Example:

#include <stdio.h>
#include <limits.h>

void foo(unsigned i) {
  printf("%d\n", i);
}

int main() {
  foo(UINT_MAX);
  return 0;
}


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug c/60194] -Wformat should also warn when using %d (instead of %u) for unsigned arguments
  2014-02-14 10:48 [Bug c/60194] New: -Wformat should also warn when using %d (instead of %u) for unsigned arguments burnus at gcc dot gnu.org
@ 2014-02-14 12:27 ` burnus at gcc dot gnu.org
  2014-02-18 16:18 ` burnus at gcc dot gnu.org
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: burnus at gcc dot gnu.org @ 2014-02-14 12:27 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=60194

--- Comment #1 from Tobias Burnus <burnus at gcc dot gnu.org> ---
> And of course also the other way round, e.g. using "%lu" or "%lz" with an
> argument which is (signed) "long".

(Ignore '%lz' that should be '%zu'.)

Thinking a bit more about it, -Wformat-unsigned (implied by -Wformat=2) is
probably the best - and ignoring the signdness for %c / character.

(Currently, the code has the following. However, the example of comment 0
doesn't trigger even with -pedantic" as it only covers pointers.)

c-family/c-format.c's check_format_types has:

      /* Don't warn about differences merely in signedness, unless
         -Wpedantic.  With -Wpedantic, warn if the type is a pointer
         target and not a character type, and for character types at
         a second level of indirection.  */
      if (TREE_CODE (wanted_type) == INTEGER_TYPE
          && TREE_CODE (cur_type) == INTEGER_TYPE
          && (!pedantic || i == 0 || (i == 1 && char_type_flag))
          && (TYPE_UNSIGNED (wanted_type)
              ? wanted_type == c_common_unsigned_type (cur_type)
              : wanted_type == c_common_signed_type (cur_type)))
        continue;


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug c/60194] -Wformat should also warn when using %d (instead of %u) for unsigned arguments
  2014-02-14 10:48 [Bug c/60194] New: -Wformat should also warn when using %d (instead of %u) for unsigned arguments burnus at gcc dot gnu.org
  2014-02-14 12:27 ` [Bug c/60194] " burnus at gcc dot gnu.org
@ 2014-02-18 16:18 ` burnus at gcc dot gnu.org
  2014-04-11 22:44 ` burnus at gcc dot gnu.org
  2014-04-11 22:49 ` burnus at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: burnus at gcc dot gnu.org @ 2014-02-18 16:18 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=60194

Tobias Burnus <burnus at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |patch

--- Comment #2 from Tobias Burnus <burnus at gcc dot gnu.org> ---
Patch: http://gcc.gnu.org/ml/gcc-patches/2014-02/msg01043.html


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug c/60194] -Wformat should also warn when using %d (instead of %u) for unsigned arguments
  2014-02-14 10:48 [Bug c/60194] New: -Wformat should also warn when using %d (instead of %u) for unsigned arguments burnus at gcc dot gnu.org
  2014-02-14 12:27 ` [Bug c/60194] " burnus at gcc dot gnu.org
  2014-02-18 16:18 ` burnus at gcc dot gnu.org
@ 2014-04-11 22:44 ` burnus at gcc dot gnu.org
  2014-04-11 22:49 ` burnus at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: burnus at gcc dot gnu.org @ 2014-04-11 22:44 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=60194

--- Comment #3 from Tobias Burnus <burnus at gcc dot gnu.org> ---
Author: burnus
Date: Fri Apr 11 22:43:54 2014
New Revision: 209328

URL: http://gcc.gnu.org/viewcvs?rev=209328&root=gcc&view=rev
Log:
2014-04-11  Tobias Burnus  <burnus@net-b.de>

        PR c/60194
gcc/
        * doc/invoke.texi (-Wformat-signedness): Document it.
        (Wformat=2): Mention that this enables -Wformat-signedness.

gcc/c-family/
        * c.opt (Wformat-signedness): Add
        * c-format.c(check_format_types): Use it.

gcc/testsuite/
        * * g++.dg/warn/warn_format_signedness.C: New.
        * gcc.dg/format/warn-signedness.c: New.

Added:
    trunk/gcc/testsuite/g++.dg/warn/warn_format_signedness.C
    trunk/gcc/testsuite/gcc.dg/format/warn-signedness.c
Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/c-family/ChangeLog
    trunk/gcc/c-family/c-format.c
    trunk/gcc/c-family/c.opt
    trunk/gcc/doc/invoke.texi
    trunk/gcc/testsuite/ChangeLog


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug c/60194] -Wformat should also warn when using %d (instead of %u) for unsigned arguments
  2014-02-14 10:48 [Bug c/60194] New: -Wformat should also warn when using %d (instead of %u) for unsigned arguments burnus at gcc dot gnu.org
                   ` (2 preceding siblings ...)
  2014-04-11 22:44 ` burnus at gcc dot gnu.org
@ 2014-04-11 22:49 ` burnus at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: burnus at gcc dot gnu.org @ 2014-04-11 22:49 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=60194

Tobias Burnus <burnus at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|---                         |FIXED

--- Comment #4 from Tobias Burnus <burnus at gcc dot gnu.org> ---
FIXED on the 4.10 trunk


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2014-04-11 22:49 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-02-14 10:48 [Bug c/60194] New: -Wformat should also warn when using %d (instead of %u) for unsigned arguments burnus at gcc dot gnu.org
2014-02-14 12:27 ` [Bug c/60194] " burnus at gcc dot gnu.org
2014-02-18 16:18 ` burnus at gcc dot gnu.org
2014-04-11 22:44 ` burnus at gcc dot gnu.org
2014-04-11 22:49 ` burnus at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).