denoland / denoland/std

encodeCbor() throws on objects with non-ASCII keys

Open Beginner friendly
#7,255 0 comments 1 reaction 0 assignees View on GitHub
bug needs triage
Dominant language
TypeScript
Stars
3.6k
Forks
681
PR merge metrics
No merged PRs in 30d

Description

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.

Contributor guide

Open the contributing guide

Research direction

Start in cbor/_common_encode.ts at calcEncodingSize() line 58 and compare object-key sizing with the string-value branch around line 34. Run the encodeCbor example with the non-ASCII key; done means it no longer throws while writing and successfully encodes the object.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
backend
Issue type
Bug
Difficulty
1/5
Estimated time
Under an hour
Activity status
Quiet
Clarity
Clearly specified
Newbie friendliness
88/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.