univalue: respect token end pointer #35281

Open
ferminquant wants to merge 1 commit from fix-univalue-token-bounds into master AGit
ferminquant commented 2026-05-13 14:25:26 +00:00 (Migrated from github.com)

Fixes #28260.

Avoid reading past the supplied end pointer in two places:

  • matching JSON keywords with strncmp;
  • validating number prefixes with firstDigit[1].

Also update the UniValue unit test and parse_univalue fuzz target to exercise bounded input.

Fixes #28260. Avoid reading past the supplied end pointer in two places: - matching JSON keywords with strncmp; - validating number prefixes with firstDigit[1]. Also update the UniValue unit test and parse_univalue fuzz target to exercise bounded input.
DrahtBot commented 2026-05-13 14:25:34 +00:00 (Migrated from github.com)

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

External sites

Reviews

See the guideline and AI policy for information on the review process.

Type Reviewers
Concept ACK sedited
Stale ACK carloantinarella

If your review is incorrectly listed, please copy-paste <!--meta-tag:bot-skip--> into the comment that the bot should ignore.

Conflicts

No conflicts as of last run.

<!--e57a25ab6845829454e8d69fc972939a--> The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. <!--006a51241073e994b41acfe9ec718e94--> ### External sites * Code coverage & benchmarks: https://corecheck.dev/bitcoin/bitcoin/pulls/35281 * Ralph review <img src=https://review.fish.foo/assets/bitcoin/bitcoin/35281.png alt="Ralph review status"> https://git.fish.foo/bitcoin/bitcoin/pulls/35281#author-ralph <!--021abf342d371248e50ceaed478a90ca--> ### Reviews See [the guideline](https://github.com/bitcoin/bitcoin/blob/master/CONTRIBUTING.md#code-review) and [AI policy](https://github.com/bitcoin/bitcoin/blob/master/doc/AI_POLICY.md) for information on the review process. | Type | Reviewers | | ---- | --------- | | Concept ACK | [sedited](https://github.com/bitcoin/bitcoin/pull/35281#pullrequestreview-4635181464) | | Stale ACK | [carloantinarella](https://github.com/bitcoin/bitcoin/pull/35281#issuecomment-4663531919) | If your review is incorrectly listed, please copy-paste <code>&lt;!--meta-tag:bot-skip--&gt;</code> into the comment that the bot should ignore. <!--174a7506f384e20aa4161008e828411d--> ### Conflicts No conflicts as of last run. <!--5faf32d7da4f0f540f40219e4f7537a3-->
maflcko (Migrated from github.com) reviewed 2026-05-13 15:56:01 +00:00
maflcko (Migrated from github.com) commented 2026-05-13 15:56:01 +00:00

why the ??

why the `?`?
DrahtBot commented 2026-05-13 16:06:38 +00:00 (Migrated from github.com)

🚧 At least one of the CI tasks failed.
Task iwyu: https://github.com/bitcoin/bitcoin/actions/runs/25806359727/job/75815150297
LLM reason (✨ experimental): CI failed because the IWYU (include-what-you-use) check flagged missing/wrong includes and intentionally returned non-zero exit status (“Failure generated from IWYU”).

Hints

Try to run the tests locally, according to the documentation. However, a CI failure may still
happen due to a number of reasons, for example:

  • Possibly due to a silent merge conflict (the changes in this pull request being
    incompatible with the current code in the target branch). If so, make sure to rebase on the latest
    commit of the target branch.

  • A sanitizer issue, which can only be found by compiling with the sanitizer and running the
    affected test.

  • An intermittent issue.

Leave a comment here, if you need help tracking down a confusing failure.

<!--85328a0da195eb286784d51f73fa0af9--> 🚧 At least one of the CI tasks failed. <sub>Task `iwyu`: https://github.com/bitcoin/bitcoin/actions/runs/25806359727/job/75815150297</sub> <sub>LLM reason (✨ experimental): CI failed because the IWYU (include-what-you-use) check flagged missing/wrong includes and intentionally returned non-zero exit status (“Failure generated from IWYU”).</sub> <details><summary>Hints</summary> Try to run the tests locally, according to the documentation. However, a CI failure may still happen due to a number of reasons, for example: * Possibly due to a silent merge conflict (the changes in this pull request being incompatible with the current code in the target branch). If so, make sure to rebase on the latest commit of the target branch. * A sanitizer issue, which can only be found by compiling with the sanitizer and running the affected test. * An intermittent issue. Leave a comment here, if you need help tracking down a confusing failure. </details>
ferminquant (Migrated from github.com) reviewed 2026-05-14 01:16:22 +00:00
ferminquant (Migrated from github.com) commented 2026-05-14 01:16:22 +00:00

You're right, it's not needed. I fixed it in a new commit.

You're right, it's not needed. I fixed it in a new commit.
carloantinarella (Migrated from github.com) reviewed 2026-06-09 07:20:20 +00:00
carloantinarella (Migrated from github.com) commented 2026-06-08 16:58:47 +00:00

Would it be the case to add also few tests for fractions and exp numbers?

Would it be the case to add also few tests for fractions and exp numbers?
ferminquant (Migrated from github.com) reviewed 2026-06-09 17:56:56 +00:00
ferminquant (Migrated from github.com) commented 2026-06-09 17:56:56 +00:00

Added in commit 28f609c7bb — eight tests covering fractions, exponents, and combined, using the same size/end-pointer pattern as the existing tests in this function.

Added in commit 28f609c7bb — eight tests covering fractions, exponents, and combined, using the same size/end-pointer pattern as the existing tests in this function.
carloantinarella commented 2026-06-09 20:01:12 +00:00 (Migrated from github.com)

tACK 28f609c7bb78c233356470b9399c35b3183563a6

Thanks for the added tests.
One comment: my suggestion is to squash the two commits into a single one, as they do not seem to represent different logical steps.

tACK 28f609c7bb78c233356470b9399c35b3183563a6 Thanks for the added tests. One comment: my suggestion is to squash the two commits into a single one, as they do not seem to represent different logical steps.
sedited (Migrated from github.com) reviewed 2026-07-06 11:31:57 +00:00
sedited (Migrated from github.com) left a comment

Concept ACK

I think most of these unit tests are redundant however. Also not sure we should be introducing a new utility for this. I think the only cases actually adding a bit of coverage is the leading -01 case. Maybe that should just be covered by some more test fixture files?

EDIT:

It would also be good to mention that this is fixing two separate out of bounds reads.

Concept ACK I think most of these unit tests are redundant however. Also not sure we should be introducing a new utility for this. I think the only cases actually adding a bit of coverage is the leading `-01` case. Maybe that should just be covered by some more test fixture files? EDIT: It would also be good to mention that this is fixing two separate out of bounds reads.
sedited (Migrated from github.com) commented 2026-07-06 10:58:08 +00:00

I would prefer if this used the more straight forward std::string_view raw_view{raw, end} than doing pointer arithmetic.

I would prefer if this used the more straight forward `std::string_view raw_view{raw, end}` than doing pointer arithmetic.
sedited (Migrated from github.com) commented 2026-07-06 11:24:04 +00:00

Is this really worthwhile? Seems like these kind of cases are better covered in the fuzz test anyway.

Is this really worthwhile? Seems like these kind of cases are better covered in the fuzz test anyway.
sedited (Migrated from github.com) commented 2026-07-06 11:24:22 +00:00

Isn't this covered in the test fixtures already?

Isn't this covered in the test fixtures already?
sedited (Migrated from github.com) commented 2026-07-06 11:22:52 +00:00

Isn't this tested already in the fixture files?

Isn't this tested already in the fixture files?
sedited commented 2026-08-04 15:28:06 +00:00 (Migrated from github.com)

@ferminquant can you respond to the review here? It's been a month.

@ferminquant can you respond to the review here? It's been a month.
ferminquant commented 2026-08-06 21:28:45 +00:00 (Migrated from github.com)

Thanks for the review. Done in commit 7f633b3beaa7778d0e2dcc87b9cd2baf5490c939:

  • Reduced the unit coverage to the minimal bounded-end regressions and removed the extra helper.
  • Updated the std::string_view construction.
  • Updated the PR description to mention the two independent out-of-bounds reads.
  • Squashed the two commits.

The focused UniValue test builds and passes locally.

Thanks for the review. Done in commit 7f633b3beaa7778d0e2dcc87b9cd2baf5490c939: - Reduced the unit coverage to the minimal bounded-end regressions and removed the extra helper. - Updated the std::string_view construction. - Updated the PR description to mention the two independent out-of-bounds reads. - Squashed the two commits. The focused UniValue test builds and passes locally.
maflcko (Migrated from github.com) reviewed 2026-08-07 06:16:41 +00:00
maflcko (Migrated from github.com) commented 2026-08-07 06:16:30 +00:00

Seems fine, but this should either be a correctly-sized buffer (without null-term), or it should be moved to a fixture file.

Seems fine, but this should either be a correctly-sized buffer (without null-term), or it should be moved to a fixture file.
maflcko (Migrated from github.com) commented 2026-08-07 06:12:38 +00:00

This should be a string view on a buffer, like in the fuzz test

This should be a string view on a buffer, like in the fuzz test
maflcko (Migrated from github.com) commented 2026-08-04 15:38:18 +00:00

can be removed as well, given the fuzz test, and the other tests.

can be removed as well, given the fuzz test, and the other tests.
maflcko (Migrated from github.com) reviewed 2026-08-07 15:25:16 +00:00
maflcko (Migrated from github.com) commented 2026-08-07 15:25:16 +00:00

Well, this is adding back the null-byte

Well, this is adding back the null-byte
ferminquant commented 2026-08-07 15:36:19 +00:00 (Migrated from github.com)

Thanks, fixed in d107f950e8:

  • Kept fixture inputs as std::string_view without copying.
  • Removed no_nul_test().
  • Changed bounded_input_test() to use correctly sized, non-null-terminated buffers.

The focused UniValue test passes locally.

Thanks, fixed in d107f950e8: - Kept fixture inputs as std::string_view without copying. - Removed no_nul_test(). - Changed bounded_input_test() to use correctly sized, non-null-terminated buffers. The focused UniValue test passes locally.
maflcko (Migrated from github.com) reviewed 2026-08-07 16:20:41 +00:00
maflcko (Migrated from github.com) commented 2026-08-07 16:20:41 +00:00

Either this needs to be copied into a fresh buffer (so that truly no data follows), or the tests need to be adjusted to not have trailing whitespace.

Either this needs to be copied into a fresh buffer (so that truly no data follows), or the tests need to be adjusted to not have trailing whitespace.
DrahtBot commented 2026-09-09 16:29:24 +00:00 (Migrated from github.com)

🐙 This pull request conflicts with the target branch and needs rebase.

<!--cf906140f33d8803c4a75a2196329ecb--> 🐙 This pull request conflicts with the target branch and [needs rebase](https://github.com/bitcoin/bitcoin/blob/master/CONTRIBUTING.md#rebasing-changes).
sedited commented 2026-10-02 12:38:24 +00:00 (Migrated from github.com)

@ferminquant this has been in need of a rebase for a while. Can you do so?

@ferminquant this has been in need of a rebase for a while. Can you do so?
First-time contributor

Base: 65896ac58ea63e29ad19d41b492e37f8722ac715
Head: 045b0ef7b42d50975cedaa6deeb9a39c879af92f

🟠 Major

Pass a length when constructing the string view (src/univalue/lib/univalue_read.cpp:101, head)

std::string_view{raw, end} is not a valid C++20 pointer-and-length construction because end is a pointer, not a size. Since this project requires C++20, the parser translation unit will not compile. Pass the remaining length as the second argument, for example static_cast<std::size_t>(end - raw).

💡 Suggestion

Make bounded-input regression assertions deterministic (src/univalue/test/unitester.cpp:177, head)

The exact-size arrays provide memory-checker coverage, but with the old checks restored, assertions may depend on bytes outside the arrays: keyword matching may see null after n, and the -0 check reads past its array. Keep these tests and add shorter views over fully initialized backing buffers. For example, a one-byte view of null must fail, a two-byte view of -01 must parse as -0, and a one-byte view of - backed by -x must fail. These cases deterministically cover keyword matching, number lookahead, and the firstDigit >= end guard.

Review coverage was incomplete. The adversarial_glm review did not complete.

Full review report

<!-- forgejo-review-bot:bitcoin/bitcoin --> Base: `65896ac58ea63e29ad19d41b492e37f8722ac715` Head: `045b0ef7b42d50975cedaa6deeb9a39c879af92f` ##### 🟠 Major **Pass a length when constructing the string view** (src/univalue/lib/univalue_read.cpp:101, head) `std::string_view{raw, end}` is not a valid C++20 pointer-and-length construction because `end` is a pointer, not a size. Since this project requires C++20, the parser translation unit will not compile. Pass the remaining length as the second argument, for example `static_cast<std::size_t>(end - raw)`. ##### 💡 Suggestion **Make bounded-input regression assertions deterministic** (src/univalue/test/unitester.cpp:177, head) The exact-size arrays provide memory-checker coverage, but with the old checks restored, assertions may depend on bytes outside the arrays: keyword matching may see `null` after `n`, and the `-0` check reads past its array. Keep these tests and add shorter views over fully initialized backing buffers. For example, a one-byte view of `null` must fail, a two-byte view of `-01` must parse as `-0`, and a one-byte view of `-` backed by `-x` must fail. These cases deterministically cover keyword matching, number lookahead, and the `firstDigit >= end` guard. Review coverage was incomplete. The adversarial_glm review did not complete. [Full review report](<https://review.fish.foo/traces/35281-045b0ef7b42d50975cedaa6deeb9a39c879af92f-308-2.html>)
ferminquant commented 2026-10-02 17:31:05 +00:00 (Migrated from github.com)

@sedited Done, sorry for the delay.

@sedited Done, sorry for the delay.
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
bitcoin/bitcoin!35281
No description provided.