Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

check integer overflow with GCC 4.5.2

9 views
Skip to first unread message

Xiangdong

unread,
Nov 23, 2011, 11:48:21 PM11/23/11
to
Hi,

I have the following code which was used to check whether the result
of (a * b) is larger than the maximum integer value.

ovf(int a, int b, int *r, int *v)
{
*r = a * b;
if (b == 0 || a == *r / b)
*v = 0;
else
*v = 1;

}

int main()
{
int words;
int bytes;
int overflow;

//... whatever
ovf(words, sizeof(int), &bytes, &overflow);
if (overflow)
printf("overflow");

}

It doesn't work with GCC 4.5.2 if enabling -O2 or -O3, asm code
generated by GCC is as follows, call to ovf is eliminated, seems
that
GCC evaluates (a == *r/b) to TRUE at compile time, is there anyway
to
revise ovf to let it satisfy GCC? thanks in advance.

main:
pushl %ebp #
xorl %eax, %eax #
movl %esp, %ebp #,
popl %ebp #
ret
.size main, .-main
.ident "GCC: (GNU) 4.5.2"
.section .note.GNU-stack,"",@progbits

Andrew Haley

unread,
Nov 24, 2011, 8:16:53 AM11/24/11
to
Xiangdong <xiangd...@gmail.com> wrote:

> I have the following code which was used to check whether the result
> of (a * b) is larger than the maximum integer value.
>
> ovf(int a, int b, int *r, int *v)
> {
> *r = a * b;
> if (b == 0 || a == *r / b)
> *v = 0;
> else
> *v = 1;
>
> }

This can't work.

> int main()
> {
> int words;
> int bytes;
> int overflow;
>
> //... whatever
> ovf(words, sizeof(int), &bytes, &overflow);
> if (overflow)
> printf("overflow");
>
> }
>

> It doesn't work with GCC 4.5.2 if enabling -O2 or -O3, asm code
> generated by GCC is as follows, call to ovf is eliminated, seems
> that GCC evaluates (a == *r/b) to TRUE at compile time, is there
> anyway to revise ovf to let it satisfy GCC? thanks in advance.

Integer overflow is undefined, so GCC assumes that it doesn't happen.
Use unsigned ints instead of ints and you'll be fine.

Andrew.
0 new messages