-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
No checking when a hexadecimal integer constant is too large for its type #4350
Comments
|
As someone who works in hex and thinks in hex an awful lot, I spend a lot of time thinking about edge cases, correctness and overflows and I certainly want to be bothered with such details. In C, if I'm dealing with 32-bit values and I accidentally enter: uint32_t xyz32 = 0x1234567A8U;
uint64_t xyz64 = 0x123456781234567A8ULL; I'd be a bit upset if the compiler and static analyser didn't spot the obvious typos (GCC says When I read this issue, I wondered if it was because the type wasn't defined (which would be relatively unusual in my world of hexadecimal as it is commonly associated with microcontroller peripherals with individual bits set for different functions and the number of bits is critical). I assumed that if the type was defined then the C compiler would provide the warning message even if Nim didn't. However, having just tested this with a slightly modified version of @edubart's code, I still don't get an error message: let a: uint64 = 0x0123456789abcdefffffffffffffffff.uint64
let b: uint64 = 0xffffffffffffffff.uint64
echo $a
echo $b
echo (a == b) It looks like Nim silently edits the code to remove the numbers that were added erroneously and produces code that the C compiler is happy with (setting both On a positive note, however, this seems to only be a problem with 64-bit integers. The code: let c: uint32 = 0x1234567A8.uint32
let d: int32 = 0x1234567A8 Produces |
well I guess implementing a check is pretty easy. The literal doesn't even need to be parsed. More than 16 hex digits for 64 bit integer? → error |
I wasn't all that serious when I wrote this and left the bug open for a good reason. My apologies. |
The following code:
Outputs:
What I was expecting:
An error because 0x0123456789abcdefffffffffffffffff is not a valid number as it's too large.
The text was updated successfully, but these errors were encountered: