printbuf_memset: maintain the NUL-termination invariant by ManoharPaturi · Pull Request #980 · json-c/json-c · GitHub
Skip to content

printbuf_memset: maintain the NUL-termination invariant - #980

Merged
hawicz merged 1 commit into
json-c:masterfrom
ManoharPaturi:fix-printbuf-memset-nul
Sep 29, 2026
Merged

hawicz merged 1 commit into
json-c:masterfrom
ManoharPaturi:fix-printbuf-memset-nul

Conversation

@ManoharPaturi

Copy link
Copy Markdown
Contributor

Problem

printbuf_memset() violates the invariant every other printbuf operation maintains — buf[bpos] == '\0' — because it never NUL-terminates at the new position and doesn't reserve space for the terminator (printbuf.c):

if (pb->size < size_needed) { printbuf_extend(pb, size_needed); ... }
memset(pb->buf + offset, charvalue, len);
if (pb->bpos < size_needed) pb->bpos = size_needed;
/* no buf[bpos] = '\0' */

Any consumer reading pb->buf as a C string immediately after an indent() (which calls printbuf_memset) reads uninitialized bytes. Latent today because serializers append before reading, but it is a foot-gun for any new consumer.

Fix

Reserve size_needed + 1 and NUL-terminate at the new bpos, matching printbuf_memappend's contract.

Verification

29/29 ctest tests pass (printbuf + serialization suites).

Copilot AI balanced review requested due to automatic review settings September 29, 2026 16:14

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@hawicz
hawicz merged commit b469bea into json-c:master Sep 29, 2026
27 checks passed
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