This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: [gcc] no warning on int conversion overflow in non-c99 mode
- From: Chung-Ju Wu <jasonwucj at gmail dot com>
- To: A A <wempwer at gmail dot com>
- Cc: gcc <gcc-help at gcc dot gnu dot org>
- Date: Tue, 18 Feb 2014 12:09:52 +0800
- Subject: Re: [gcc] no warning on int conversion overflow in non-c99 mode
- Authentication-results: sourceware.org; auth=none
- References: <CAKmQUfaFkApENev+NrZD-yF-VXBR+X5fE_ZoOXxHvh_+QpKtFw at mail dot gmail dot com> <CAKmQUfYEQ_+7kT65ZVC7m+8PrzaOj0s55Q5_qabR6Ni_oCXcAA at mail dot gmail dot com>
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