Fix buffer overflow/infinite loop from indent handling#709
Merged
Conversation
If indent * nest depth is large enough to overflow an int, it causes the required output buffer size to be underestimated leading to a buffer overflow. The offending arithmetic is upgraded to a ptrdiff_t and indent is artificially capped. An overflow is still technically possible given a high enough recursion depth but not without first consuming petabytes of RAM. If indent is negative, it causes a size_t to underflow to some number a little bellow size_t max. If that underflow isn't accidentally rectified by a subsequent overflow (if -indent * (nest_depth + 1) > current_buffer_size) then the buffer up-sizer gets stuck in an infinite loop trying to find a power of two that fits in a size_t but is greater that size_t max / 2. It's hard to tell if `ujson.dumps(..., indent=-1)` was ever an intentional feature but I don't feel comfortable breaking it in a security fix. For now, the dubious *any negative indent -> pad colons but add no indentation or newlines* behaviour is preserved but internally the indent is clipped to -1 and all subsequent indentation code paths are skipped over.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #709 +/- ##
==========================================
+ Coverage 91.08% 91.22% +0.14%
==========================================
Files 7 7
Lines 1951 1971 +20
==========================================
+ Hits 1777 1798 +21
+ Misses 174 173 -1 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
hugovk
approved these changes
Mar 11, 2026
Member
hugovk
left a comment
There was a problem hiding this comment.
Thanks, I think an error is better than silently capping.
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.
If
indent * nest_depthis large enough to overflow an int, it causes the required output buffer size to be underestimated leading to a buffer overflow.The offending arithmetic is upgraded to a
ptrdiff_tandindentis artificially capped. An overflow is still technically possible given a high enough recursion depth but not without first consuming petabytes of RAM.If indent is negative, it causes a
size_tto underflow to some number a little bellowsize_t max. If that underflow isn't accidentally rectified by a subsequent overflow (if-indent * (nest_depth + 1) > current_buffer_size) then the buffer up-sizer gets stuck in an infinite loop trying to find a power of two that fits in asize_tbut is greater thatsize_t max / 2.It's hard to tell if
ujson.dumps(..., indent=-1)was ever an intentional feature but I don't feel comfortable breaking it in a security fix. For now, the dubious any negative indent -> pad colons but add no indentation or newlines behaviour is preserved but internally the indent is clipped to -1 and all subsequent indentation code paths are skipped over.Fixes #700