Reject non-representable floats when reading a JSONB float into an integer#2645
Merged
Conversation
…teger A JSONB FLOAT/FLOAT5 payload read into an integer field was cast straight to the target type via static_cast<T>(tmp). When the decoded double is NaN, +/-Inf (e.g. a JSON5 "NaN"/"Infinity" sentinel), or finite but outside T's range, that cast is undefined behavior. A fuzzer reached it through a reflected struct with an integer field (UBSan: "nan is outside the range of representable values of type 'int'" at jsonb/read.hpp). Guard the conversion: reject any value not representable as T before casting. The upper bound is exclusive at max(T) + 1 (an exact power of two in double) so the 64-bit boundary, where static_cast<double>(max(T)) rounds up past the true maximum, cannot slip through. Genuinely integral floats still convert. Adds a jsonb_test regression suite covering NaN/Inf, out-of-range, the 64-bit boundary, exact extremes, and truncation of fractional values.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes undefined behavior when reading a JSONB
FLOAT/FLOAT5payload into an integer field.The integral
from<JSONB, T>reader decodes the payload into adoubleand then casts it straight to the target withstatic_cast<T>(tmp). When the decoded value isNaN,±Inf(e.g. a JSON5NaN/Infinitysentinel), or finite but outsideT's range, that float→integer cast is undefined behavior ([conv.fpint]).A fuzzer reached it through a reflected struct with an integer field:
Fix
Guard the conversion: reject any value not representable as
Tbefore the cast, returningerror_code::parse_number_failure. Genuinely integral floats still convert (the reader's existing "accept floats that happen to be integral" behavior, including truncation of fractional values, is preserved).The upper bound is exclusive at
max(T) + 1, computed as(max(T) / 2 + 1) * 2.0, which is an exact power of two indouble. This avoids the classic pitfall wherestatic_cast<double>(max(T))rounds up past the true maximum for 64-bit integer types: a naivetmp <= static_cast<double>(max)check would admit exactly2^63forint64_tand then invoke the very UB we're guarding against.NaNcompares false against both bounds, so it is rejected by the same check.Only JSONB is affected. CBOR errors on a float payload read into an integer, and BEVE explicitly disallows float→integer cross-conversion, so neither coerces and neither has this issue.
Tests
Adds a
jsonb_testregression suite (float_to_int_coercion_tests) covering:NaN/+Inf/-Infpayloads into an integer → error (not UB)1e300) intoint32/int64/uint64→ error2^63intoint64_trejected without UB; the largest representable double< 2^63still convertsINT32_MAX,INT32_MIN) round-trip through the float payloadVerified locally under
-fsanitize=undefined,address: pre-fix aborts atjsonb/read.hpp:297, post-fix all cases pass with no sanitizer diagnostics.jsonb_testpasses (148 tests, 567 asserts).