This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Avoiding integer overflow

 
David McCombs
Ranch Hand
Posts: 212
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 212
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic