Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | "Philip Lantz" <prl@canterey.us> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: check integer overflow with GCC 4.5.2 |
| Date | 2011-11-24 01:26 -0800 |
| Organization | A noiseless patient Spider |
| Message-ID | <op.v5f1uhu7lahckh@cantereyd> (permalink) |
| References | <649a64d1-3c3b-43d1-be20-a3b465118267@m10g2000vbc.googlegroups.com> <gLOdnQCRNMn6mVPTnZ2dnUVZ8nudnZ2d@giganews.com> <15275059-737c-40b0-b9b8-d875104d7f1f@m7g2000vbc.googlegroups.com> |
On Thu, 24 Nov 2011 01:08:28, sailer wrote:
> On Nov 24, 4:46 pm, Vincenzo Mercuri wrote:
>> sailer ha scritto:> 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;
>> > }
>>
>> [...]
>>
>> Since you are concerned with "signed int"s you should check against
>> both INT_MAX and INT_MIN. I just wrote the following function
>> "overflow()"
>> (sorry for the poor choice of its name), that should do the trick
>> (it takes the two factors and returns 1 if an overflow will occur, 0 if
>> not):
>>
>> #include <stdio.h>
>> #include <limits.h>
>>
>> int overflow(int a, int b)
>> {
>> if( b > 0 ) {
>> if( (a > 0 && b <= INT_MAX / a) ||
>> (a <= 0 && a >= INT_MIN / b) )
>> return 0;
>> else return 1;
>> } else { /* b <= 0 */
>> if( (a > 0 && b >= INT_MIN / a) ||
>> (a <= 0 && (b == 0 || a >= INT_MAX / b)) )
>> return 0;
>> else return 1;
>> }
>>
>> }
>>
>> void result(int a, int b)
>> {
>> printf("\na = %d, b = %d\n", a, b), overflow(a, b) ?
>> printf("[overflow detected]\n") :
>> printf("[result = %d]\n", a * b);
>>
>> }
>>
>> int main(void)
>> {
>> printf("INT_MAX = %d, INT_MIN = %d\n", INT_MAX, INT_MIN);
>> result(INT_MAX, INT_MAX);
>> result(INT_MIN, INT_MIN);
>> result(INT_MIN, INT_MAX);
>> result(4000000, -80000);
>> result(20000, 20000);
>> result(0, 4);
>> result(3, 0);
>> result(0, 0);
>> return 0;
>>
>> }
>
> Thanks all for your help, currently I'm using "__attribute__
> ((noinline))" to bypass the issue.
I doubt that this avoids the undefined behavior in ovf, so you still
may find that it does not work reliably.
Philip
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
check integer overflow with GCC 4.5.2 sailer <xiangdong.ji@gmail.com> - 2011-11-23 20:25 -0800
Re: check integer overflow with GCC 4.5.2 pete <pfiland@mindspring.com> - 2011-11-24 00:47 -0500
Re: check integer overflow with GCC 4.5.2 sailer <xiangdong.ji@gmail.com> - 2011-11-23 22:29 -0800
Re: check integer overflow with GCC 4.5.2 "Philip Lantz" <prl@canterey.us> - 2011-11-24 01:23 -0800
Re: check integer overflow with GCC 4.5.2 sailer <xiangdong.ji@gmail.com> - 2011-11-27 23:14 -0800
Re: check integer overflow with GCC 4.5.2 James Kuyper <jameskuyper@verizon.net> - 2011-11-28 07:04 -0500
Re: check integer overflow with GCC 4.5.2 Noob <root@127.0.0.1> - 2011-11-29 14:12 +0100
Re: check integer overflow with GCC 4.5.2 Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2011-11-29 16:16 +0200
Re: check integer overflow with GCC 4.5.2 88888 Dihedral <dihedral88888@googlemail.com> - 2011-11-29 06:28 -0800
Re: check integer overflow with GCC 4.5.2 Robert Wessel <robertwessel2@yahoo.com> - 2011-11-24 00:28 -0600
Re: check integer overflow with GCC 4.5.2 Vincenzo Mercuri <vincenzo.mercuri@yahoo.it> - 2011-11-24 09:46 +0100
Re: check integer overflow with GCC 4.5.2 sailer <xiangdong.ji@gmail.com> - 2011-11-24 01:08 -0800
Re: check integer overflow with GCC 4.5.2 "Philip Lantz" <prl@canterey.us> - 2011-11-24 01:26 -0800
Re: check integer overflow with GCC 4.5.2 sailer <xiangdong.ji@gmail.com> - 2011-11-27 23:09 -0800
Re: check integer overflow with GCC 4.5.2 "io_x" <a@b.c.invalid> - 2011-11-24 14:19 +0100
Re: check integer overflow with GCC 4.5.2 gwowen <gwowen@gmail.com> - 2011-11-24 09:10 -0800
Re: check integer overflow with GCC 4.5.2 sailer <xiangdong.ji@gmail.com> - 2011-11-27 23:21 -0800
Re: check integer overflow with GCC 4.5.2 Keith Thompson <kst-u@mib.org> - 2011-11-28 16:31 -0800
Re: check integer overflow with GCC 4.5.2 Ian Collins <ian-news@hotmail.com> - 2011-11-29 13:53 +1300
Re: check integer overflow with GCC 4.5.2 sailer <xiangdong.ji@gmail.com> - 2011-11-27 23:34 -0800
Re: check integer overflow with GCC 4.5.2 pete <pfiland@mindspring.com> - 2011-11-28 18:59 -0500
Re: check integer overflow with GCC 4.5.2 sailer <xiangdong.ji@gmail.com> - 2011-11-29 19:44 -0800
Re: check integer overflow with GCC 4.5.2 James Kuyper <jameskuyper@verizon.net> - 2011-11-30 11:08 -0500
Re: check integer overflow with GCC 4.5.2 Keith Thompson <kst-u@mib.org> - 2011-11-30 09:01 -0800
Re: check integer overflow with GCC 4.5.2 sailer <xiangdong.ji@gmail.com> - 2011-11-29 19:46 -0800
csiph-web