Pretty: ensure recursionsLeft is not zero before decrementing it#283
Merged
thiagomacieira merged 1 commit intointel:mainfrom Mar 18, 2025
Merged
Conversation
thiagomacieira
approved these changes
Mar 17, 2025
Member
thiagomacieira
left a comment
There was a problem hiding this comment.
Looks ok, but it would be easier to just change container_to_pretty:
if (!recursionsLeft <= 0) {
Contributor
Author
|
Actually, I was not sure whether negative values of Here it seems |
532caf0 to
1a6c1c1
Compare
thiagomacieira
approved these changes
Mar 18, 2025
When value_to_pretty is called with recursionsLeft=0 and an Array or Map
type, container_to_pretty is called with recursionsLeft - 1 = -1. This
breaks the recursion limit check.
In practice, this can be triggered with a CBOR data containing 1023
Arrays, a Tag and many more Arrays:
$ python3 -c 'import sys;sys.stdout.buffer.write(b"\x9f" * 1023 + b"\xc0\x9f" + b"\x9f" * 100000 + b"\xff" * 101024)' | ./bin/cbordump
Segmentation fault (core dumped)
This segmentation fault is due to the stack growing too much, due to the
quantity of recursive calls.
Fix this by reporting a proper error when recursionsLeft <= 0, instead
of when recursionsLeft == 0. The same input now produces:
[_ [_ [_ ... [_ 0([
-: internal error: too many nested containers found in recursive function
_ <nesting too deep, recursion stopped>
Moreover, using fewer nested arrays works fine:
$ python3 -c 'import sys;sys.stdout.buffer.write(b"\x9f" * 1023 + b"\xc0\x9f" + b"\x9f" * 1024 + b"\xff" * 2048)' |./bin/cbordump
[_ [_ [_ ... [_ 0([_ <nesting too deep, recursion stopped>])] ... ]]]
Also modify the test when formatting CborTagType to ensure
value_to_pretty is never called with a negative recursionsLeft.
1a6c1c1 to
8a57e1a
Compare
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.
When
value_to_prettyis called withrecursionsLeft=0and an Array or Map type,container_to_prettyis called withrecursionsLeft - 1 = -1. This breaks the recursion limit check.In practice, this can be triggered with a CBOR data containing 1023 Arrays, a Tag and many more Arrays:
This segmentation fault is due to the stack growing too much, due to the quantity of recursive calls.
Fix this by reporting a proper error instead. The same input now produces:
Moreover, using fewer nested arrays works fine: