-
Notifications
You must be signed in to change notification settings - Fork 155
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Struct.NumberField.getString() downcasts to int #94
Comments
It does appear that this logic does not support 64-bit values at all. Changing the toString is only one small part of it; note that NumberField.longValue also calls intValue and upcasts it to a long. It's unclear to me if we can just switch all of these classes to actually treat the contained value as a long. Thoughts? |
AFAICT, NumberField.longValue is always overridden in the places that matter, e.g. IntegerAlias, Signed64, Unsigned64, SignedLong, UnsignedLong. Perhaps you could make all those methods abstract to avoid confusion? |
And calling longValue() will still be wrong if the number is a float. Looks like it's more correct to make the method abstract. |
The shortest fix seems to be to copy over the toString() method of Signed64 / Unsigned64 / SignedLong / UnsignedLong into IntegerAlias and Unsigned32. The delicate series of overrides of longValue() and toString() in derived classes seems to handle every other case. Alternately we can make longValue() and toString() abstract so that the code is easier to read but lengthier. |
Since NumberField.toString() downcasts to int, it needs to be overridden for number fields bigger than int. This was handled in other places but was missed for Unsigned32 and IntegerAlias.
Override toString() in Unsigned32 and IntegerAlias to fix #94.
If somebody does a String.valueOf(myStruct.foo)... where foo is a u_int64_t field... instead of String.valueOf(myStruct.foo.get()) which will be simple and safe, it looks to me like the code is doing an unintentional downcast to int.
I'm seeing the issue in Struct.NumberField:
the intValue() call will be to Struct.IntegerAlias.intValue():
which does the downcast. Perhaps call longValue() instead?
The text was updated successfully, but these errors were encountered: