Path: csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!border3.nntp.dca.giganews.com!Xl.tags.giganews.com!border1.nntp.dca.giganews.com!nntp.giganews.com!local2.nntp.dca.giganews.com!nntp.earthlink.com!news.earthlink.com.POSTED!not-for-mail NNTP-Posting-Date: Wed, 23 Nov 2011 23:47:46 -0600 Message-ID: <4ECDDA82.4C71@mindspring.com> Date: Thu, 24 Nov 2011 00:47:46 -0500 From: pete Reply-To: pfiland@mindspring.com Organization: PF X-Mailer: Mozilla 3.04Gold (WinNT; I) MIME-Version: 1.0 Newsgroups: comp.lang.c Subject: Re: check integer overflow with GCC 4.5.2 References: <649a64d1-3c3b-43d1-be20-a3b465118267@m10g2000vbc.googlegroups.com> Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Lines: 51 X-Usenet-Provider: http://www.giganews.com NNTP-Posting-Host: 4.156.228.94 X-Trace: sv3-1QhhoeytraafHEu4IDvGCLf2SDovjuw0iMG7aKnoaNi6ZVhT7cJG7OoBe3xvKT10hmnpEO84NDGFBdO!SNLWa/SN3Y07Ko2km1A6TFLfrcHgDIpMYP4Qg0sa90qg6ZoNcvxKQX7I+PVEvXAyiTKEZXefj0K4!C8N3Kd9pPA== X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly X-Postfilter: 1.3.40 X-Original-Bytes: 2270 Xref: x330-a1.tempe.blueboxinc.net comp.lang.c:13837 sailer wrote: > > 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 How about maybe: #include if (b == 0 || INT_MAX / b >= a) -- pete