Skip to content

Serializer: reserve worst-case JSON escape space so values are not dropped - #3266

Merged
IvanNardi merged 1 commit into
ntop:devfrom
ManoharPaturi:fix-serializer-escape-reserve
Oct 3, 2026
Merged

IvanNardi merged 1 commit into
ntop:devfrom
ManoharPaturi:fix-serializer-escape-reserve

Conversation

@ManoharPaturi

Copy link
Copy Markdown
Contributor

Problem

The JSON serializer reserves escape space assuming at most 2x expansion, but ndpi_json_string_escape() can emit 6 bytes per input byte (\u00XX for control characters, 3-byte U+FFFD for invalid bytes):

  • src/lib/ndpi_serializer.c (ndpi_serialize_binary_raw): needed += 16 + klen + vlen; — no escape headroom at all
  • (ndpi_serialize_uint32_binary): needed += 24 + slen*2 /* account escape (x2) */;

When a value's escaped form exceeds the reserved space, the escape hits its internal bound and returns -1, which the consumers add into u_int32_t size_used — moving the write position backwards, dropping the value, and still returning success. The exported JSON is left truncated/unquoted.

Verified on a dev build: ndpi_serialize_string_string(&s, "key", <2047 '\x01's>) returns rc=0 and produces {"key"} — a record every downstream JSON consumer (ntopng flow export) fails to parse. Smaller control-heavy values vanish the same way once the buffer has grown organically.

Fix

Reserve the worst case (6x) at both sites. With the escape never hitting its bound, the unchecked += can no longer receive -1.

Verification

make unit serializer tests pass; the 2047-control-char case now serializes the full \u0001... sequence into valid JSON (verified in a scratch build).

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

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.

@IvanNardi

Copy link
Copy Markdown
Member

@ManoharPaturi, could you rebase, please?

@ManoharPaturi
ManoharPaturi force-pushed the fix-serializer-escape-reserve branch from 95d376e to 545b208 Compare October 3, 2026 17:45
@ManoharPaturi

ManoharPaturi commented Oct 3, 2026 •

Copy link
Copy Markdown
Contributor Author

@IvanNardi Rebased onto the current dev branch and force-pushed. The diff is unchanged only the two reservation-size adjustments in ndpi_serializer.c. The failing checks were against the four-day-old head; the full matrix is re-running now, and I will follow up if anything still fails.

@sonarqubecloud

sonarqubecloud Bot commented Oct 3, 2026

Copy link
Copy Markdown

@ManoharPaturi

Copy link
Copy Markdown
Contributor Author

For clarity on the checks: the failing "Tarball/Package/Symbols checks (ubuntu-latest)" job also fails on the current dev branch itself (run 37127699665), so it appears to be pre-existing and unrelated to this change. The remaining matrix jobs are still running on the rebased head; I will keep an eye on them.

@IvanNardi

Copy link
Copy Markdown
Member

For clarity on the checks: the failing "Tarball/Package/Symbols checks (ubuntu-latest)" job also fails on the current dev branch itself (run 37127699665), so it appears to be pre-existing and unrelated to this change. The remaining matrix jobs are still running on the rebased head; I will keep an eye on them.

don't worry about that; the rebase was needed to fix the gcc-4.9 job (completely unrelated to your change)

@IvanNardi IvanNardi 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.

LGTM, thank you!

@IvanNardi
IvanNardi merged commit bcec445 into ntop:dev Oct 3, 2026
27 of 28 checks passed
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.

3 participants