aspose file tools
The moose likes Beginning Java and the fly likes Avoiding integer overflow Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply 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: 2341
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
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Avoiding integer overflow
 
Similar Threads
Integer32(very Urgent)
How to create an array of arrayList
how do i make sure that user does not enter a str for an int type
Integer unboxing and == operator question
read in an integer