• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 283
  • Last Modified:

Java & C++: Stop when invalid/overflowed value is submitted.

Sometimes a client will get an error and submit an overflowed value to java.

For example, a value in C++ is submitted to Java through the network, the value is overflowed past 2147483647 lets say, sometimes ending up sending values like -321931298 etc because it went right past the max value. I would like to disgregard this value submitted if it is PAST the max 2147483647 number.

How can you detect overflowed numbers in C++ and Java? I need a idea/example/suggestion for both, (250 points each.)

Thanks!
0
Valleriani
Asked:
Valleriani
  • 4
  • 3
  • 2
  • +1
1 Solution
 
cupCommented:
Has it overflowed or has it wrapped?  Detecting overflows is normally a compiler switch but you have to look up the chip specificiation to see when it flags an overflow.  Going past maxint and wrapping may not set the overflow flag.  Sometimes this is intentional.

Which OS/Architecture/Compilers are you using?  The answer will vary depending on what you tell us.
0
 
VallerianiAuthor Commented:
VC++ 6 compiler


And Java just the standard javac compiler to compile java files.
0
 
phoffricCommented:
To recap to make sure I'm on the same page, converting to hex we have:
2147483647 = 0x7FFFFFFF
 321931298  = 0x13304822

Now 0x7FFFFFFF is the largest positive integer value of type signed long (if your architecture defines long as 4 bytes, as many do).

Since the value 321931298 fits into the valid range for signed long, then apparently there is some server side calculation that overflows. I gather that you are hoping that no such overflowing occurs. You didn't say whether negative numbers are valid or represent an overflow.

If invalid, then (0x8000 & number) != 0 represents a negative number (caused by an overflow), and you can detect that on the client side. (Of course before sending it, the server could also do this check and take appropriate action.)

But depending upon your calculations, and regardless of whether negative numbers are permitted, there is another overflow to consider. Suppose your calculations were done in integer type long long (64 bits), then if the upper 4 bytes != 0, then an overflow has occurred, and take appropriate action.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
objectsCommented:
you cannot check on the java end.
on the c++ side you need to check the value before submitting it to java

how is it getting passed to java?
0
 
objectsCommented:
using long on the java side may be safer
0
 
phoffricCommented:
I was wondering whether the client and server platforms are the same architecture. If not, then possibly there could be little-endian/big-endian issues.
0
 
VallerianiAuthor Commented:
(0x8000 & number) != 0 is actually what worked very well for this issue, thanks!

0x7FFFFFFF as well does good. The thing is howver i was also using numbers 'past' this, so this should do well!
0
 
phoffricCommented:
Great! Glad you were able to get going quickly.
0
 
VallerianiAuthor Commented:
Thanks! The (0x8000 & number) != 0 check helped me for those under values, because they were being cycled/wrapped but this ended up finding them for me :)
0
 
phoffricCommented:
Just be careful that this is the complete solution. Depending on your program, you could multiple cycled/wrapped computations, in which case the bad bit may still be 0. That is why I mentioned long long to catch even more extensive overflows.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 4
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now