Skip to content

(protobuf) Fix writeString(char[],int,int) writing enum values twice - #772

Open
pjfanning wants to merge 1 commit into
FasterXML:3.xfrom
pjfanning:protobuf-enum-fixes
Open

(protobuf) Fix writeString(char[],int,int) writing enum values twice#772
pjfanning wants to merge 1 commit into
FasterXML:3.xfrom
pjfanning:protobuf-enum-fixes

Conversation

@pjfanning

@pjfanning pjfanning commented Sep 7, 2026

Copy link
Copy Markdown
Member

The EnumLookup sizing fix that was originally in this PR has been split out into #773.

ProtobufGenerator.writeString(char[],int,int) writes enum values twice

if (_currField.wireType != WireType.LENGTH_PREFIXED) {
    _writeEnum(new String(text, offset, clen));
    // no return -- falls through
}

The String overload returns after _writeEnum(); the char[] one did not, so it emitted the enum index and then fell through and emitted the same value again as a length-prefixed String. Writing "WEB" to an enum field produced 7 bytes instead of 2, i.e. corrupt output.

Pre-dates the 2.x -> 3.x rename and is present on 2.21 too; targeting 3.x for now, happy to redo against 2.21 and merge forward if you prefer.

Test

WriteEnumAsCharArrayTest — the char[] and String overloads must produce identical bytes for an enum-valued field, and the result must round-trip. Confirmed it fails with expected: <2> but was: <7> if the return is removed again.

Full protobuf module test suite passes.

🤖 Generated with Claude Code

@github-actions

github-actions Bot commented Sep 7, 2026

Copy link
Copy Markdown

🧪 Code Coverage Report

Coverage Type Coverage Change
📝 Instructions 76.78% 📈 +0.12%
🔀 Branches 68.39% 📈 +0.13%

… twice

The `String` overload returns after `_writeEnum()`; the `char[]` one did
not, so it emitted the enum index and then fell through and emitted the
same value again as a length-prefixed String. Writing "WEB" to an enum
field produced 7 bytes instead of 2, i.e. corrupt output.

Adds a regression test asserting the `char[]` and `String` overloads
produce identical bytes for an enum-valued field.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@pjfanning pjfanning changed the title Fix two Protobuf enum defects (double-write via char[], EnumLookup hash sizing) (protobuf) Fix writeString(char[],int,int) writing enum values twice Sep 7, 2026
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