Skip to content

validate utf-8 continuation bytes in utf8ToCodepoint - #1702

Open
SABITHSAHEB wants to merge 3 commits into
open-source-parsers:masterfrom
SABITHSAHEB:writer-utf8-continuation-bytes
Open

validate utf-8 continuation bytes in utf8ToCodepoint#1702
SABITHSAHEB wants to merge 3 commits into
open-source-parsers:masterfrom
SABITHSAHEB:writer-utf8-continuation-bytes

Conversation

@SABITHSAHEB

Copy link
Copy Markdown
Contributor
  1. utf8ToCodepoint consumed the bytes after a multibyte lead without checking they are continuation bytes (10xxxxxx), so a broken sequence swallowed the ASCII that followed it. With the default emitUTF8=off, serializing a string containing "\xE0AB" produced "�" and dropped the valid A and B; "\xF0XYZ" produced a bogus surrogate pair with X, Y, Z gone.
  2. a well-formed 4-byte sequence decoding past U+10FFFF was still emitted, as an invalid surrogate pair.
    Validate each trailing byte and cap at U+10FFFF, returning U+FFFD without advancing past bytes that are not part of the sequence. This is the lenient replacement behaviour rather than a hard error, so valid UTF-8 output is unchanged.

@SABITHSAHEB

Copy link
Copy Markdown
Contributor Author

any update?

@baylesj

baylesj commented Aug 22, 2026

Copy link
Copy Markdown
Contributor

@greptileai

@greptile-apps

greptile-apps Bot commented Aug 22, 2026

Copy link
Copy Markdown

Greptile Summary

The PR validates UTF-8 continuation bytes during JSON string serialization and rejects four-byte sequences above U+10FFFF.

  • Preserves valid bytes following malformed multibyte prefixes.
  • Consumes valid continuation prefixes as part of a single replacement character.
  • Adds regression coverage for malformed and out-of-range sequences.

Confidence Score: 5/5

The PR appears safe to merge.

No blocking failure remains; the previously reported continuation-prefix issue is fixed by consuming the valid prefix once while preserving the offending byte for the caller’s next iteration.

Important Files Changed

Filename Overview
src/lib_json/json_writer.cpp Validates continuation bytes, preserves the first offending byte for subsequent decoding, and rejects code points above U+10FFFF.
src/test_lib_json/main.cpp Adds regression tests covering malformed prefixes, preserved ASCII suffixes, out-of-range code points, and valid multibyte input.

Reviews (2): Last reviewed commit: "consume the valid continuation prefix of..." | Re-trigger Greptile

@coveralls

coveralls commented Aug 22, 2026

Copy link
Copy Markdown

Coverage Report for CI Build 32591467250

Coverage increased (+0.03%) to 89.935%

Details

  • Coverage increased (+0.03%) from the base build.
  • Patch coverage: 6 of 6 lines across 1 file are fully covered (100%).
  • No coverage regressions found.

Uncovered Changes

No uncovered changes found.

Coverage Regressions

No coverage regressions found.


Coverage Stats

Coverage Status
Relevant Lines: 2745
Covered Lines: 2620
Line Coverage: 95.45%
Relevant Branches: 2650
Covered Branches: 2232
Branch Coverage: 84.23%
Branches in Coverage %: Yes
Coverage Strength: 23787.12 hits per line

💛 - Coveralls

Comment thread src/lib_json/json_writer.cpp Outdated
When a multibyte sequence has valid continuation bytes followed by a
non-continuation byte, leave the decoder on the last valid trailing byte
so the whole malformed prefix is replaced by a single U+FFFD instead of
one per byte. "\xE2\x82A" now serializes as "\ufffdA".
@SABITHSAHEB

Copy link
Copy Markdown
Contributor Author

@baylesj addressed the one item from the review above (valid continuation prefix was being replaced byte by byte). The truncated-at-end-of-input case goes through the pre-existing length checks, so I left that path alone.

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