encodeCbor() throws on objects with non-ASCII keys

Open Beginner friendly
#7,255 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

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

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.

Description

bug needs triage

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.

Dominant language
TypeScript
Stars
3.6k
Forks
685
Avg merge
10d 11m
Merged PRs (30d)
9

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from denoland/std

All issues in denoland/std

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.