Skip to content

RUST-2488 Fix lossy JavascriptCodeWithScope decoding - #689

Open
dxbjavid wants to merge 1 commit into
mongodb:mainfrom
dxbjavid:cws-lossy-scope-offset
Open

dxbjavid wants to merge 1 commit into
mongodb:mainfrom
dxbjavid:cws-lossy-scope-offset

Conversation

@dxbjavid

Copy link
Copy Markdown

When parse_utf8_lossy handles a JavaScriptCodeWithScope it works out where the scope document begins from the length of the lossily decoded code string, but that length can be larger than the bytes actually stored whenever the code isn't valid utf-8 (each bad byte decodes to a three-byte replacement character). The offset then overshoots and the conversion either bails out with a misleading "length overrun"/"document too short" error or picks up the wrong scope, which is unfortunate given Utf8Lossy is the path meant to tolerate invalid utf-8 in the first place. This computes scope_start from the raw code byte length returned by read_lenencode_bytes and only decodes the code afterwards. I've added a regression test that round-trips a code-with-scope whose code holds invalid utf-8.

@dxbjavid
dxbjavid requested a review from a team as a code owner August 26, 2026 10:47

@isabelatkinson isabelatkinson left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the fix! Changes look good. Can you please merge/rebase on main to pull in a lint fix for CI?

@isabelatkinson isabelatkinson changed the title fix scope offset in utf-8 lossy code-with-scope decoding RUST-2488 Fix lossy JavascriptCodeWithScope decoding Aug 31, 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.

2 participants