Skip to content

encodeCbor() throws on objects with non-ASCII keys #7255

Description

@tomas-zijdemans

encodeCbor() under-allocates its buffer when an object key contains multi-byte UTF-8 characters, then throws while writing:

import { encodeCbor } from "@std/cbor";

encodeCbor({ "é☃é☃é☃é☃": 1 });
// RangeError: offset is out of bounds
The cause is in calcEncodingSize(): object keys are sized as y.length bytes (cbor/_common_encode.ts:58), but encodeString() writes UTF-8, which needs up to 3 bytes per UTF-16 code unit. String values already account for this with x.length * 3 on line 34. Keys don't.

The fix looks like a one-liner: size keys the same way the value branch does. Happy to PR it.

Spotted by @bartlomieju while reviewing #7250.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions