Skip to content

Fall back to object encoding for out-of-range BJData ndarray elements - #5438

Open
22elix3r wants to merge 1 commit into
nlohmann:developfrom
22elix3r:fix/5403-bjdata-ndarray-range
Open

Fall back to object encoding for out-of-range BJData ndarray elements#5438
22elix3r wants to merge 1 commit into
nlohmann:developfrom
22elix3r:fix/5403-bjdata-ndarray-range

Conversation

@22elix3r

Copy link
Copy Markdown
Contributor

Summary

to_bjdata() validated that ndarray _ArrayData_ elements were the right number kind (integer vs float) but not that they fit the named _ArrayType_. An element such as 256 with _ArrayType_ = \"uint8\" was static_cast-truncated to 0 and round-tripped as a different value with no error.

Related Issue

Fixes #5403

Changes Made

  • Range-check each integer _ArrayData_ element against the BJData wire type (uint8/int8/uint16/…/char/byte) before writing
  • If any element does not fit, fall back to plain object encoding (same path as a wrong kind, unknown type, or overflowed _ArraySize_)
  • Document the range requirement next to the existing kind check in bjdata.md

Testing

Commands executed:

  • python3 tools/amalgamate/amalgamate.py -c tools/amalgamate/config_json.json -s .
  • cmake -S . -B build -DJSON_BuildTests=ON -DCMAKE_BUILD_TYPE=Debug -DJSON_FastTests=ON
  • cmake --build build --target test-bjdata_cpp11
  • ./build/tests/test-bjdata_cpp11 --no-skip -tc='*5403*'
  • ./build/tests/test-bjdata_cpp11 --no-skip -tc=BJData -sc='parsing values,array,*out-of-range*,*not matching*'

Results:

Notes

In-range extrema (0/255 for uint8, -128/127 for int8, …) still encode as packed ndarrays. Floating-point types are unchanged (IEEE truncation of double to single is expected).

  • The changes are described in detail, both the what and why.
  • If applicable, an existing issue is referenced.
  • The Code coverage remained at 100%. A test case for every new line of code.
  • If applicable, the documentation is updated.
  • The source code is amalgamated by running make amalgamate.

write_bjdata_ndarray only checked that _ArrayData_ elements were the
right number kind, then static_cast-truncated values that did not fit
the named type (256 as uint8 became 0). Range-check each element and
encode as a plain object when it does not fit.

Signed-off-by: elix3r <157088510+22elix3r@users.noreply.github.com>
@22elix3r
22elix3r requested a review from nlohmann as a code owner August 27, 2026 03:29
Copilot AI lite review requested due to automatic review settings August 27, 2026 03:29

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@github-actions

Copy link
Copy Markdown

🔴 Amalgamation check failed! 🔴

The source code has not been amalgamated and/or formatted correctly.

📎 A ready-to-apply patch is attached to the failed workflow run as the amalgamation-patch artifact. Download it, then apply it locally from the repository root with:

git apply amalgamation.patch

This does not require installing astyle yourself.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

to_bjdata() silently truncates out-of-range _ArrayData_ elements (e.g. 256 as uint8 becomes 0)

2 participants