Skip to content

fix: handle zero-sized and undersized temp buffers in buffered JSON dump - #175

Merged
rgerhards merged 2 commits into
masterfrom
codex/propose-fix-for-json-dumping-vulnerability
Jul 23, 2026
Merged

fix: handle zero-sized and undersized temp buffers in buffered JSON dump#175
rgerhards merged 2 commits into
masterfrom
codex/propose-fix-for-json-dumping-vulnerability

Conversation

@rgerhards

@rgerhards rgerhards commented Jul 23, 2026

Copy link
Copy Markdown
Member

Motivation

  • Prevent out-of-bounds writes and undefined behavior when callers pass a zero or too-small temporary buffer to the buffered dump API, which could underflow the computed length passed to vsnprintf() and reuse a consumed va_list.

Description

  • Make buffer_printf() check the actual available space (available = buffer->size - buffer->filled) and skip calling vsnprintf() when available == 0, forcing the code path that uses a dynamically allocated buffer instead.
  • Ensure each formatting attempt initializes and finalizes its own va_list (va_start/va_end) so a consumed va_list is not reused.
  • Use available for success comparisons and update buffer->filled (not buffer->size) when writing retry data into the buffer.
  • Avoid flushing an empty user-supplied buffer by only calling buffer_flush() when buffer->filled > 0.
  • Document that fjson_object_dump_buffered() may be called with temp == NULL when size == 0.
  • Add a regression test (tests/test_dump_buffered.{c,test,expected}) and register it in tests/Makefile.am to cover both size == 0 and a very small nonzero size cases.

Testing

  • Compiled and ran the new regression test with a normal build and verified output matches tests/test_dump_buffered.expected using gcc and a local config.h (test passed).
  • Compiled and ran the same regression test under AddressSanitizer/UndefinedBehaviorSanitizer (-fsanitize=address,undefined) and observed no sanitizer errors and expected output (test passed).
  • Ran git diff --check to ensure no whitespace or diff issues (passed).
  • autoreconf -fi && ./configure && make check could not be executed in this environment because aclocal is not available (tooling limitation).

Codex Task


Summary by cubic

Safely handle zero-sized and undersized temp buffers in the buffered JSON dump to prevent out-of-bounds writes and undefined behavior. Falls back to dynamic allocation when needed and adds a regression test.

  • Bug Fixes
    • Skip vsnprintf() when available space is 0; force dynamic buffer path.
    • Start/end a fresh va_list for each formatting attempt.
    • Use available = size - filled for checks; update buffer->filled on retries.
    • Only flush when buffer->filled > 0 to avoid empty-buffer flushes.
    • Document that temp may be NULL when size == 0 in fjson_object_dump_buffered().
    • Add test_dump_buffered covering size == 0 and tiny nonzero buffers; register in tests/Makefile.am and include test_dump_buffered.expected in EXTRA_DIST.

Written for commit 2e0085a. Summary will update on new commits.

Review in cubic

@rgerhards
rgerhards force-pushed the codex/propose-fix-for-json-dumping-vulnerability branch from 94db948 to 5d416b9 Compare July 23, 2026 11:25
@rgerhards
rgerhards merged commit c9a7761 into master Jul 23, 2026
21 checks passed
@rgerhards
rgerhards deleted the codex/propose-fix-for-json-dumping-vulnerability branch July 23, 2026 12:22
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.

1 participant