Skip to content

fix: read each property once when encoding with ignoreUndefined - #308

Open
shigma wants to merge 1 commit into
msgpack:mainfrom
shigma:fix/ignore-undefined-read-once
Open

shigma wants to merge 1 commit into
msgpack:mainfrom
shigma:fix/ignore-undefined-read-once

Conversation

@shigma

@shigma shigma commented Nov 29, 2026

Copy link
Copy Markdown

With ignoreUndefined: true, Encoder now reads each property of an object once. The size in the map header and the encoded entries come from the same values.

Before this change, countWithoutUndefined() read every property to compute the size, and encodeMap() read it again to write the entries. A getter or a proxy that returns undefined on one read but not on the other made the header disagree with the entries, and the output could not be decoded:

let reads = 0;
const object = {
  get foo() {
    reads++;
    return reads === 1 ? "bar" : undefined;
  },
};

decode(encode(object, { ignoreUndefined: true }));
// before: RangeError: Offset is outside the bounds of the DataView
// after:  { foo: "bar" }

Without ignoreUndefined, the size is keys.length and each property was already read once, so that path is unchanged apart from writing the header through the new writeMapHeader().

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