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: Vincent Lefevre <vincent+gcc at vinc17 dot org>
- To: gcc-help at gcc dot gnu dot org
- Date: Tue, 18 Feb 2014 10:17:25 +0100
- 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> <CADj25HNDXXbaZbuq0VdqFVz8wg9fWZYk1h8AGZnEyXSHv7n67Q at mail dot gmail dot com>
On 2014-02-18 12:09:52 +0800, Chung-Ju Wu wrote:
> 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!!"
But it currently behaves in an inconsistent way: without -pedantic,
it signals some overflows, but not all of them.
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=59753
> 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.
I don't think this is necessarily the case. You could have code like:
#define VAL 2166136261u;
unsigned int i = VAL * VAL;
/* ... */
int ia = VAL;
where some value is used at various places in the code, and the
user doesn't remember that VAL doesn't fit in an int or forgot the
"unsigned". The unsigned type could have been given because it is
needed in some places like in VAL * VAL above (to have modular
arithmetic).
--
Vincent Lefèvre <vincent@vinc17.net> - Web: <https://www.vinc17.net/>
100% accessible validated (X)HTML - Blog: <https://www.vinc17.net/blog/>
Work: CR INRIA - computer arithmetic / AriC project (LIP, ENS-Lyon)