Skip to content

cJSON_Print: add CJSON_PRINT_LIMIT to bound the output buffer size - #1128

Open
gsmarcil wants to merge 1 commit into
DaveGamble:masterfrom
gsmarcil:fix-print-output-limit
Open

gsmarcil wants to merge 1 commit into
DaveGamble:masterfrom
gsmarcil:fix-print-output-limit

Conversation

@gsmarcil

@gsmarcil gsmarcil commented Nov 5, 2026

Copy link
Copy Markdown

cJSON_Print(), cJSON_PrintUnformatted() and cJSON_PrintBuffered() escape each control character as a 6-byte \uXXXX sequence and grow the output buffer without an upper bound, so a string of N control characters prints as about 6N bytes. On memory-constrained targets this can exhaust the heap.

Add an optional compile-time limit, following CJSON_NESTING_LIMIT:

  • CJSON_PRINT_LIMIT (default 0 = no limit, so behaviour is unchanged).
  • ensure() refuses to grow an allocating print buffer beyond the limit and caps the doubling growth at the limit; the print functions then return NULL and free their buffer.
  • cJSON_PrintPreallocated() is unaffected; it is bounded by the caller's buffer.
  • New tests in tests/print_limit.c.

CVE-2026-39148
Reported by Rachid Marcil (gsmarcil).

cJSON_Print(), cJSON_PrintUnformatted() and cJSON_PrintBuffered() escape
each control character as a 6-byte \uXXXX sequence and grow the output
buffer without an upper bound, so a string of N control characters
prints as about 6N bytes. On memory-constrained targets this can exhaust
the heap.

Add an optional compile-time limit, following CJSON_NESTING_LIMIT:

- CJSON_PRINT_LIMIT (default 0 = no limit, so behaviour is unchanged).
- ensure() refuses to grow an allocating print buffer beyond the limit
  and caps the doubling growth at the limit; the print functions then
  return NULL and free their buffer.
- cJSON_PrintPreallocated() is unaffected; it is bounded by the
  caller's buffer.
- New tests in tests/print_limit.c.

CVE-2026-39148
Reported by Rachid Marcil (gsmarcil).
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.

1 participant