Skip to content

Fix silent wraparound when reading uint64 > INT64_MAX as signed integer in JSON - #132

Open
clockblocker wants to merge 1 commit into
mainfrom
fix-json-uint64-signed-overflow
Open

clockblocker wants to merge 1 commit into
mainfrom
fix-json-uint64-signed-overflow

Conversation

@clockblocker

Copy link
Copy Markdown
Owner

Summary

Fixes a silent integer wraparound bug in rfl::json::Reader::to_basic_type when reading JSON integers in (INT64_MAX, UINT64_MAX] into signed integer targets (or rfl::Generic).

Root Cause

In yyjson, yyjson_is_int(val) returns true for both YYJSON_SUBTYPE_SINT and YYJSON_SUBTYPE_UINT, while yyjson_get_sint(val) returns val->uni.i64 directly without checking the subtype. As a result:

  • rfl::json::read<int64_t>("18446744073709551615") silently succeeded and returned -1 (and 9223372036854775808 returned INT64_MIN).
  • Narrow signed targets like int32_t and int8_t also silently accepted 18446744073709551615 as -1 because -1 passed the min()/max() bounds check added in Reject out-of-range JSON integer values munich-data-engineering/reflect-cpp#746.
  • rfl::json::read<rfl::Generic>("18446744073709551615") matched the int64_t alternative and silently produced int64_t(-1).

Fix

  • include/rfl/json/Reader.hpp: Dispatch yyjson_is_uint and yyjson_is_sint separately in the signed-integral branch of to_basic_type, checking yyjson_get_uint against static_cast<uint64_t>(std::numeric_limits<Target>::max()) instead of bit-reinterpreting u64 as i64.
  • tests/json/test_integers.cpp: Added test_signed_overflow_from_large_uint64 covering INT64_MAX + 1, UINT64_MAX into int64_t/int32_t/int8_t, boundary INT64_MAX/INT64_MIN, and rfl::Generic.

Verification

  • Strict standalone header check (-std=c++20 -Wall -Wextra -Wpedantic -Wshadow -Wconversion -Werror) on include/rfl/json/Reader.hpp: passed.
  • tests/json/test_integers.cpp under -fsanitize=undefined -fno-sanitize-recover=all -Wall -Werror: 3/3 passed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant