COMPGROUPS.NET | Search | Post Question | Groups | Stream | About | Register

### checking for overflow

• Email
• Follow

```Hi,
Is this code which checks if a and b when multiplied overflow correct?

/*begin code*/
#include <stdio.h>
#include <limits.h>

int check_overflow(unsigned long a,unsigned long b)
{
unsigned long  t=ULONG_MAX/b;
unsigned long t1;
t1=ULONG_MAX-t*b;
if ( (a > t) || ((a == t) && (t1 > 0)) ) return 1;
return 0;
/* return ( (a > t) || ((a == t) && (t1 > 0)) );*/
}
/*end code*/

Arin
```
 0

See related articles to this posting

```On Sat, 5 Jun 2004, Arin Chaudhuri wrote:

> Hi,
> Is this code which checks if a and b when multiplied overflow correct?
>
> /*begin code*/
> #include <stdio.h>
> #include <limits.h>
>
> int check_overflow(unsigned long a,unsigned long b)
> {
>     unsigned long  t=ULONG_MAX/b;
>     unsigned long t1;
>     t1=ULONG_MAX-t*b;
>     if ( (a > t) || ((a == t) && (t1 > 0)) ) return 1;
>     return 0;
>     /* return ( (a > t) || ((a == t) && (t1 > 0)) );*/
> }
> /*end code*/
>
> Arin
>
My mistake.
Checking (a>t) should be sufficient.
Sorry.

#include <stdio.h>
#include <limits.h>

int check_overflow(unsigned long a,unsigned long b)
{
unsigned long  t=ULONG_MAX/b;
return (t>a);
}

Arin

```
 0
Reply achaudh (2) 6/5/2004 11:29:16 AM

```On Sat, 5 Jun 2004, Arin Chaudhuri wrote:
[snip]

I am having a bad day, I meant

#include <stdio.h>
#include <limits.h>

int check_overflow(unsigned long a,unsigned long b)
{
unsigned long  t=ULONG_MAX/b;
return (t<a);
}

Arin

```
 0
Reply achaudh (2) 6/5/2004 11:31:24 AM

```Arin Chaudhuri wrote:
> On Sat, 5 Jun 2004, Arin Chaudhuri wrote:
> [snip]
>
> I am having a bad day, I meant
>
>
>  #include <stdio.h>
>  #include <limits.h>
>
>  int check_overflow(unsigned long a,unsigned long b)
>  {
>      unsigned long  t=ULONG_MAX/b;
>      return (t<a);
>  }

This looks almost right: It returns 1 if the product
of `a' and `b' would exceed `ULONG_MAX', or 0 if it would
not.  However, it will malfunction if `b' is zero.  A
possible fix:

int check_overflow(unsigned long a, unsigned long b) {
return b > 0 && a > ULONG_MAX / b;
}

--
Eric.Sosman@sun.com

```
 0
Reply Eric.Sosman (4552) 6/7/2004 2:11:24 PM

3 Replies
53 Views

Similar Articles

12/19/2013 6:43:38 PM
page loaded in 20299 ms. (0)

Similar Artilces:

When adding two values of type time_t, how can I check for overflow? Maybe I'm just brain-cramped today, but I can't figure out how to do it. Thanks! -- ======================================================================== Ian Pilcher i.pilcher@comcast.net ======================================================================== Ian Pilcher wrote: > When adding two values of type time_t, how can I check for overflow? > Maybe I'm just brain-cramped today, but I can't figure out how to do it. > > Thanks! > Shouldn'...

Abstract: Overflow checking is not done in C. This article proposes a solution to close this hole in the language that has almost no impact in the run time behavior. 1: The situation now -------------------- Any of the four operations on signed integers can overflow. The result of the operation is meaningless, what can have a catastrophic impact on the operations that follow. There are important security issues associated with overflows. The only way to catch overflows now is to use cumbersome C expressions that force modifications of source code, and are very slow since done in C. 2: Th...

How would Ariane 5 have behaved if overflow checking were not turned off?
I have followed the (quite lenghty) on a topic, IIRC, about bitwise operators, which eventually lead to people mentioning the Ariane 5 case. Since then I have been wondering. If compiler checking where actually turned on, what would have happened? How could it avoid the disaster? Right now I think of three possibilities, the two former seem very unlikely to me. a) It would, even if the problem went undetected during testing, have made the developers actually develop code handling this exceptional possibility. b) The compiler default action for unhandled overflows have saved the day; c) De...

Check for overflow. Optimization issues in Blackfin compiler
Hello, If I calculate a product P = x * y and I want to check that adding P to a sum doesn't result in an overflow, is this the way to do it or is there a more efficient way: if (((sum ^ P) & MIN_32) == 0) { if ((tmp ^ sum) & MIN_32) { tmp = (sum < 0) ? MIN_32 : MAX_32; flagOverflow = 1; } } where tmp = sum + P Also, I have been told that the blackfin compiler does a crappy job at optimizing code which uses macros (macros for fixedpoi...

check integer overflow with GCC 4.5.2
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 elimi...

check one two check check
check check one twoo Stifler wrote: > check check one twoo You're supposed to say that into a microphone. Here it goes like this: "this is a test, please ignore." -- _______________________________________________________________________ Christopher R. Carlen Principal Laser/Optical Technologist Sandia National Laboratories CA USA crcarle@sandia.gov -- NOTE: Remove "BOGUS" from email address to reply. ...

Check All Check Boxes
Hi, I need to create a function in javascript to check or uncheck all checkboxes in a form. From what I understand, I can do this either by specifying the name of the check box fields such as: function checkAll(list) { var o = document.getElementById(list); var c = o.checked; var f = document.forms.f; var a = f.item(list); for (var i = 0; i < a.length; i++) a[i].checked = c; } or I can do this by looping through all of the fields in the form and determining whether they are check boxes such as: function selectAll(){ var frm = document.for...

Updating checkboxes / Checking what was checked
I have a lot of check boxes. This is an update of the check boxes, I want something was checked, then to do an insert (which is currently working), if something is no longer checked...delete the checkbox join/link that is in the database. So, the insert/checked is working, but the "unchecking" is not working. I don't know how to compare an array of what was not checked. Here is my code: if (is_array(\$_POST['commentsid'])) { foreach (\$_POST['commentsid'] as \$id) { ########## ## Finding out if it was previously checked ## if not, insert the data #...

_ropen level check checking
How can level check be incorporated into recio ( _ropen, _rreadn, ... ) procedure code? the _Ropnfbk( ) procedure will return a pointer to open feedback infomation: /****************************************************************/ /* O P E N F E E D B A C K A R E A */ /* */ /* This structure maps the Open feedback area information. */ /* */ /****************************************************************/ typedef _Packed struct _...

check
Hello, anyone wants to try my program. It generates on every run 8 rows of six numbers. So, you play almost all the numbers, (except one - never mind ;-)) ) It is difficult to play all the numbers only with the pen, difficult to find what numbers you didn't play. So this program makes easy to do it. I have more programs to upload in the future, stay tuned. Good luck Argi http://lotto-free.blogspot.com/ Argi said: > Hello, anyone wants to try my program. > It generates on every run 8 rows of six numbers. > So, you play almost all the numbers, (except one - never mind ;-))...