This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [gcc] no warning on int conversion overflow in non-c99 mode


2014-02-18 6:18 GMT+08:00 A A <wempwer@gmail.com>:
>
> cat << EOF | gcc -c -xc -pedantic -
>> char foo = 255;
>> signed int ia = 2166136261;
>> signed int ia1 = 2147483648;
>> EOF
> <stdin>:1:1: warning: overflow in implicit constant conversion [-Woverflow]
> <stdin>:2:1: warning: this decimal constant is unsigned only in ISO
> C90 [enabled by default]
> <stdin>:3:1: warning: this decimal constant is unsigned only in ISO
> C90 [enabled by default]
>
> It looks like in case of int gcc simply compares if lvalue has the
> same size as rvalue.  Integer constants conversion rules have changed
> between C90 and C99. In C90, 2166136261 is unsigned long int compiler
> which has size of 4, the same as int hence no warning. But in C99
> 2166136261 is long long int which is 8 bytes.

Yes, in C99 6.4.4.1, 2166136261 is implicit type of long long int.

But to my understanding, the warning comes from that you didn't explicitly
tell the constant type, nor cast it into an representable value for lvalue.
So gcc warns you:
  "Hey, there is an implicit constant type, and I am going to convert it
   into a signed int.  Be careful that there is an overflow!!"

If you use either
    signed int ia = 2166136261u;
    signed int ia1 = 2147483648u;
or
    signed int ia = (signed int) 2166136261;
    signed int ia1 = (signed int) 2147483648;

There is no warning message because it makes gcc understand
that you know what exactly you are doing.


Best regards,
jasonwucj


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]