Skip to content

writeout: stream JSON strings instead of buffering them - #23535

Closed
Huge wants to merge 2 commits into
curl:masterfrom
Huge:fix/json-large-writeout-strings
Closed

Huge wants to merge 2 commits into
curl:masterfrom
Huge:fix/json-large-writeout-strings

Conversation

@Huge

@Huge Huge commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor

jsonWriteString() capped the escaped string at 100000 bytes and collected it in a dynbuf. For a response header needing more than 100000 escaped bytes, %{header_json} dropped the value and left a trailing comma (e.g. ["small",]), which is invalid JSON. A failed allocation lost the value the same way.

Instead of picking a different limit or collecting large headers in memory, this:

  1. Moves the escaping of one character (a byte, or a whole valid UTF-8 sequence) out of jsonquoted() into a helper jsonescape(). The output of existing callers is unchanged.
  2. Lets jsonWriteString() escape into a 128-byte stack buffer and write it to the stream whenever it is full.

There is no size limit and no heap allocation, the quotes are always written, and an unbuffered stream such as stderr gets one write per chunk rather than one per byte. A UTF-8 sequence is never split between chunks. Test 1671 now verifies a large escaped header value.

Disclaimer: Found the issue by test-driving an LM-powered vulnerability scanner I work on heavily.

@Huge Huge changed the title writeout: retain large escaped JSON strings writeout: stream JSON strings instead of buffering them Oct 21, 2026
Comment thread tests/data/test2120 Outdated
JSON write-out preserves a large escaped header value
</name>
<command option="no-output">
http://%HOSTIP:%HTTPPORT/%TESTNUMBER -sS -w '%{header_json}\n' -o %LOGDIR/%TESTNUMBER.out

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I suggest integrating this into an existing test.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Integrated into test 1671 and removed test 2120.

@Huge
Huge force-pushed the fix/json-large-writeout-strings branch from f536b5f to 1255ac0 Compare October 21, 2026 12:39

@bagder bagder left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I like it!

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.

🟡 Changes recommended

Per-byte writes regress unbuffered stderr performance; bounded stack buffering would preserve the benefits without that regression.

1 open issue
What changed in this PR

Removes the write-out JSON string buffer limit so large response headers remain intact and produce valid JSON.

Changes:

  • Shares per-byte escaping between buffered and streamed output.
  • Streams JSON strings without a dynamic buffer.
  • Extends test 1671 with a large escaped header value.
File Description
tests/​data/​test1671 Adds regression coverage for large escaped headers.
src/​tool_writeout_json.c Extracts shared escaping and streams JSON strings.

Review effort: Balanced

Comment thread src/tool_writeout_json.c Resolved
Huge added 2 commits October 25, 2026 07:28
Move the escaping of one byte, or of one valid UTF-8 sequence, out of
jsonquoted() into a helper so that a caller without a dynbuf can reuse
it. The output is unchanged.
jsonWriteString() collected the escaped string in a dynbuf, which needs a
size limit and can fail to allocate. Both cases lose the value and leave
an empty string behind, so %{header_json} could output ["small",] for a
header that needs more than 100000 bytes escaped.

Escape into a small stack buffer and write it whenever it is full. There
is no limit to pick and nothing left to fail, and an unbuffered stream
like stderr still gets one write per chunk rather than one per byte.
Also verify large escaped headers in test 1671.

Reported-by: BS.ehlas.cz
@Huge
Huge force-pushed the fix/json-large-writeout-strings branch from 1255ac0 to e00c1c9 Compare October 25, 2026 06:34
@bagder bagder removed the needs-info label Oct 27, 2026
@bagder
bagder requested a balanced review from Copilot October 27, 2026 16:56

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.

🟢 Approval recommended

The changes preserve escaping behavior, maintain safe buffer bounds, and cover the original size-limit failure with a regression test.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

@bagder bagder closed this in 705b02e Oct 27, 2026
@bagder

bagder commented Oct 27, 2026

Copy link
Copy Markdown
Member

Thanks, merged!

Comment thread src/tool_writeout_json.c Resolved
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Development

Successfully merging this pull request may close these issues.

4 participants