Skip to content

fix overflow check in parse_number_token#1171

Open
Ramya-9353 wants to merge 2 commits into
boostorg:developfrom
Ramya-9353:pointer-token-overflow
Open

fix overflow check in parse_number_token#1171
Ramya-9353 wants to merge 2 commits into
boostorg:developfrom
Ramya-9353:pointer-token-overflow

Conversation

@Ramya-9353

Copy link
Copy Markdown
Contributor
value{{"foo", {1, 2, 3}}}.find_pointer(
    "/foo/" + std::to_string(SIZE_MAX) + "9", ec);
// ec == error::not_found, expected error::token_overflow

parse_number_token flags std::size_t overflow with new_result < result, the
addition-overflow idiom, but the accumulation is result * 10 + d. Once
result * 10 wraps, the wrapped value plus d can land at or above result,
so a pointer token an order of magnitude past max() slips through and resolves
to a bogus (out-of-range) index, reporting error::not_found rather than
error::token_overflow. Replaced with the pre-check
result > (std::size_t(-1) - d) / 10.

The regression test appends a digit to max() and expects token_overflow; it
fails before the change and passes after. The existing overflow test only covers
max() + 1, which happens to wrap below result and so was already caught.

@cppalliance-bot

cppalliance-bot commented Jul 13, 2026

Copy link
Copy Markdown

An automated preview of the documentation is available at https://1171.json.prtest2.cppalliance.org/libs/json/doc/html/index.html

If more commits are pushed to the pull request, the docs will rebuild at the same URL.

2026-07-17 10:18:50 UTC

@cppalliance-bot

cppalliance-bot commented Jul 13, 2026

Copy link
Copy Markdown

GCOVR code coverage report https://1171.json.prtest2.cppalliance.org/gcovr/index.html
LCOV code coverage report https://1171.json.prtest2.cppalliance.org/genhtml/index.html
Coverage Diff Report https://1171.json.prtest2.cppalliance.org/diff-report/index.html

Build time: 2026-07-17 10:32:06 UTC

@cppalliance-bot

Copy link
Copy Markdown

@codecov

codecov Bot commented Jul 13, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.91%. Comparing base (c57359d) to head (66df698).
⚠️ Report is 1 commits behind head on develop.

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff            @@
##           develop    #1171   +/-   ##
========================================
  Coverage    93.91%   93.91%           
========================================
  Files           91       91           
  Lines         9265     9268    +3     
========================================
+ Hits          8701     8704    +3     
  Misses         564      564           
Files with missing lines Coverage Δ
include/boost/json/impl/pointer.ipp 100.00% <100.00%> (ø)

Continue to review full report in Codecov by Harness.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update c57359d...66df698. Read the comment docs.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Comment thread include/boost/json/impl/pointer.ipp Outdated
Comment on lines +203 to +210
// guard against std::size_t overflow in result * 10 + d
if( result > (std::size_t(-1) - d) / 10 )
{
BOOST_JSON_FAIL(ec, error::token_overflow);
return {};
}

result = new_result;
result = result * 10 + d;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

How about something like this?

if( result > UINT64_MAX / 10 )
{
    BOOST_JSON_FAIL(ec, error::token_overflow);
    return {};
}
result *= 10;

if( result > UINT64_MAX - d )
{
    BOOST_JSON_FAIL(ec, error::token_overflow);
    return {};
}
result += d;

I've checked on Godbolt and produces less code.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done, pushed the two-step guard. One adjustment: result is std::size_t, so I used std::size_t(-1) as the bound rather than UINT64_MAX, which would never fire on 32-bit targets. Pointer suite still passes, 185 total, including the max()+'9' regression.

@cppalliance-bot

Copy link
Copy Markdown

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.

3 participants