-
-
Notifications
You must be signed in to change notification settings - Fork 7.3k
fix(cbor): reject negative ints overflowing int64 #5039
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
base: develop
Are you sure you want to change the base?
Conversation
🔴 Amalgamation check failed! 🔴The source code has not been amalgamated. @thevilledev |
a17996e to
47d033b
Compare
CBOR encodes negative integers as "-1 - n" where n is uint64_t. When n > INT64_MAX, casting to int64_t caused undefined behavior and silent data corruption. Large negative values were incorrectly parsed as positive integers (e.g., -9223372036854775809 became 9223372036854775807). Add bounds check for to reject values that exceed int64_t representable range, returning parse_error instead of silently corrupting data. Added regression test cases to verify. Signed-off-by: Ville Vesilehto <[email protected]>
47d033b to
aeff909
Compare
nlohmann
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me.
|
It would be great to have a second opinion here. @gregmarr |
gregmarr
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm unsure that the tests are testing what they should be. It should be testing getting a result of int64 max, and getting a result of int64 max + 1 failing, but it looks like it's testing putting the bit patterns for those values in the CBOR data and then checking for the results from those bit patterns.
I should be sleeping now, maybe it will make more sense tomorrow.
So the tests are specifically for CBOR type
I ran the tests against For positive integers (type But of course welcome to other ideas how to test this. I guess we could check that there's no sign flip, i.e. negative results are never positive? |
|
Like I said, don't review code when you should be sleeping. :) I mostly deal with floating point, where "min" refers to the smallest possible positive number, rather than integers, where it refers to the negative number with the largest magnitude, so I often mix up the integer min unless I really stop and think about it, and I was definitely not doing that properly. I agree that we should be testing |
Add test for "n=0" case (result=-1) to cover the smallest magnitude boundary. Update comments to explain CBOR 0x3B encoding and why "result=0" is not possible. Clarify that n is an unsigned integer in the formula "result = -1 - n" to help understanding the tests. Signed-off-by: Ville Vesilehto <[email protected]>
|
np, no worries! I clarified the tests a bit, hopefully covering the boundaries fully now. Let me know what you think 👍 |
CBOR encodes negative integers as "-1 - n" where n is uint64_t. When n > INT64_MAX, casting to int64_t caused undefined behavior and silent data corruption. Large negative values were incorrectly parsed as positive integers (e.g., -9223372036854775809 became 9223372036854775807).
Add bounds check for to reject values that exceed int64_t representable range, returning parse_error instead of silently corrupting data.
Added regression test cases to verify.
make amalgamate.