Skip to content

Fix data corruption from unrecognized entity in StrPair::GetStr() (#1082)#1086

Open
steps-re wants to merge 1 commit into
leethomason:masterfrom
steps-re:fix/unrecognized-entity-stale-byte
Open

Fix data corruption from unrecognized entity in StrPair::GetStr() (#1082)#1086
steps-re wants to merge 1 commit into
leethomason:masterfrom
steps-re:fix/unrecognized-entity-stale-byte

Conversation

@steps-re

@steps-re steps-re commented Jul 7, 2026

Copy link
Copy Markdown

Fixes #1082.

Bug

In StrPair::GetStr(), values are un-escaped in place with a read pointer p and a write pointer q. After a recognized entity like & expands, output is shorter than input, so q trails p. The unrecognized-entity branch then advanced both pointers (++p; ++q;) without writing *q, so a stale earlier byte replaced the &. An unknown entity's & was silently corrupted:

<a>&amp;&bogus;</a>   // GetText() returned "&abogus;"  (stale 'a'), should be "&&bogus;"

Fix

Write *q = *p; before advancing in the unrecognized-entity branch — identical to the numeric-character-reference fallback a few lines above. Correctly preserves the & in the q == p case too.

Verification

  • Standalone repro confirms &abogus; before, correct &&bogus; after.
  • Added a case to xmltest.cpp next to the existing entity-transformation tests: fails on unfixed code (Pass 529, Fail 1), passes with the fix.
  • Full xmltest: 530/530 pass, no regressions.

When an unrecognized entity (e.g. &bogus;) follows a recognized entity
that was expanded to a shorter string, the write pointer q lags behind
the read pointer p. The unrecognized-entity branch advanced both
pointers without writing *q, so a stale buffer byte replaced the '&'
in the output (e.g. "&amp;&bogus;" parsed to "&abogus;" instead of
"&&bogus;"). Copy *p to *q before advancing, matching the numeric
character-reference fallback a few lines above.

Adds a focused xmltest assertion. Fixes leethomason#1082.

Signed-off-by: Mike German <mike@stepsventures.com>
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.

Data Corruption via Unrecognized Entity in StrPair::GetStr()

1 participant