Bug 44984 - gcc passes unsigned instead of int for printf width/precision (warnings generated)
Summary: gcc passes unsigned instead of int for printf width/precision (warnings gener...
Status: RESOLVED INVALID
Alias: None
Product: gcc
Classification: Unclassified
Component: middle-end (show other bugs)
Version: 4.5.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2010-07-19 07:23 UTC by Jay
Modified: 2013-11-10 08:17 UTC (History)
1 user (show)

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


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Jay 2010-07-19 07:23:29 UTC
4.3.5 but 4.5.0 looks about the same, but it has an additional instance:


mber/dpd -I../libdecnumber -I/usr/local/include   ../../gcc/gcc/opts.c -o opts.o
../../gcc/gcc/opts.c: In function `wrap_help':
../../gcc/gcc/opts.c:1037: warning: field width is not type int (arg 3)
../../gcc/gcc/opts.c:1037: warning: field width is not type int (arg 5)
../../gcc/gcc/opts.c: In function `common_handle_option':
../../gcc/gcc/opts.c:1409: warning: field width is not type int (arg 3)
gcc -c   -g -O2 -DIN_GCC   -W -Wall -Wwrite-strings -Wstrict-prototypes -Wmissin


jbook2:gcc jay$ diff -uw opts.c.orig opts.c
--- opts.c.orig    2010-07-19 00:07:18.000000000 -0700
+++ opts.c    2010-07-19 00:16:27.000000000 -0700
@@ -1034,7 +1034,10 @@
         }
     }
 
-      printf( "  %-*.*s %.*s\n", col_width, item_width, item, len, help);
+      gcc_assert(col_width <= INT_MAX);
+      gcc_assert(item_width <= INT_MAX);
+      gcc_assert(len <= INT_MAX);
+      printf ("  %-*.*s %.*s\n", (int)col_width, (int)item_width, item, (int)len, help);
       item_width = 0;
       while (help[len] == ' ')
     len++;
@@ -1404,9 +1407,12 @@
             }
 
         if (i == cl_lang_count)
+          {
+            gcc_assert (len <= INT_MAX);
           fnotice (stderr,
                "warning: unrecognized argument to --help= switch: %.*s\n",
-               len, a);
+                 (int)len, a);
+          }
           }
 
         if (comma == NULL)


This makes a line go a little over 80 columns, but one of the "nearby" lines does already.
 (nearby as presented here, not really in the code)

This probably isn't the best fix.
  if the output is "not important", and someone manages to create huge data, should still work.
  Yes, I'm aware of that "huge" is 2GB.

Maybe:

int to_reasonable_diagnostic_length(unsigned i)
{
  return (i <= INT_MAX) ? i : 255;
}

or

int unint_to_int_pin(unsigned i)
{
  return (i <= INT_MAX) ? i : INT_MAX;
}

and then apply those.

 - Jay
Comment 1 Andrew Pinski 2010-07-20 22:50:52 UTC
unsigned int and int are passed exactly the same for varargs and in fact the C standard says getting an unsigned version of the signed type for varargs is valid.
Comment 2 Andrew Pinski 2010-07-20 22:52:40 UTC
What compiler version is giving a warning for this case?  That is can you provide the output of "gcc -v" ?
Comment 3 Jay 2010-10-02 10:27:53 UTC
> which compiler produces this


I'm afraid I'm not sure and can't quickly/easily make it happen again. Sorry.
Comment 4 Andrew Pinski 2013-11-10 08:17:45 UTC
No feedback in 3 years so closing.