This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
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


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Avoiding integer overflow
 
Similar Threads
Integer unboxing and == operator question
how do i make sure that user does not enter a str for an int type
read in an integer
How to create an array of arrayList
Integer32(very Urgent)