You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Serializer: reserve worst-case JSON escape space so values are not dropped - #3266
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
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).
@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.
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.
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)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
The JSON serializer reserves escape space assuming at most 2x expansion, but
ndpi_json_string_escape()can emit 6 bytes per input byte (\u00XXfor 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 allndpi_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>)returnsrc=0and 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 unitserializer tests pass; the 2047-control-char case now serializes the full\u0001...sequence into valid JSON (verified in a scratch build).