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.
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.