aspose file tools*
The moose likes Beginning Java and the fly likes Avoiding integer overflow Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Avoiding integer overflow" Watch "Avoiding integer overflow" New topic
Author

Avoiding integer overflow

David McCombs
Ranch Hand

Joined: Oct 17, 2006
Posts: 212
I have been taking a secure coding class this winter and one of the topics is integer overflows. A post by Jim Yingst yesterday
brought up this issues.

It seems that Java ignores this problem, is there any way to avoid this, especially using mathematical operations?


"Should array indices start at 0 or 1? My compromise of 0.5 was rejected without, I thought, proper consideration."- Stan Kelly-Bootle
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
You can use BigInteger. But I'm not sure if there's an easy way to detect overflow if your using numerical types.
David McCombs
Ranch Hand

Joined: Oct 17, 2006
Posts: 212
Thanks, that is what I thought.

It seems odd that a language that focused on eliminating some of the problems of the past(i.e. buffer overflows) would not implement protection against integer overflow.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by David McCombs:
...A post by Jim Yingst yesterday
brought up this issues...

Yeah, I'm a little embarrassed this never occurred to me. I know I've coded compareTo to return the difference without a second thought. (I'm pretty sure the numeric values I was using were always small, but now I have to lay awake nights wondering...)


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
 
Don't get me started about those stupid light bulbs.
 
subject: Avoiding integer overflow