Skip to content

fix(cbor): correctly size object keys with multi-byte characters - #7287

Open
eeshsaxena wants to merge 1 commit into
denoland:mainfrom
eeshsaxena:fix/cbor-multibyte-object-keys
Open

fix(cbor): correctly size object keys with multi-byte characters#7287
eeshsaxena wants to merge 1 commit into
denoland:mainfrom
eeshsaxena:fix/cbor-multibyte-object-keys

Conversation

@eeshsaxena

Copy link
Copy Markdown

Closes #7255.

encodeCbor() throws on objects whose keys contain multi-byte UTF-8 characters:

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

encodeCbor({ "é☃é☃é☃é☃": 1 });
// RangeError: offset is out of bounds

calcEncodingSize() sizes object keys as y.length bytes, but keys are written as UTF-8 by encodeString(), which can use up to 3 bytes per UTF-16 code unit. String values already account for this (x.length * 3); keys did not, so the buffer was under-allocated and the write ran off the end.

Sized keys as y.length * 3 to match the string-value branch. Thanks to @bartlomieju for pinpointing the cause in the #7255 discussion.

Added a test mirroring the existing string-preallocation test, using a multi-byte key. It fails on main (RangeError) and passes with the change.

deno test cbor/encode_cbor_test.ts
# 25 passed

calcEncodingSize() sized object keys as y.length bytes, but keys are written
as UTF-8 which can use up to 3 bytes per UTF-16 code unit. A key with
multi-byte characters under-allocated the buffer, so encodeCbor() threw
RangeError: offset is out of bounds while writing.

Size keys as y.length * 3, matching how string values are already sized.

Closes denoland#7255
@CLAassistant

CLAassistant commented Aug 11, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@github-actions github-actions Bot added the cbor label Aug 11, 2026
@codecov

codecov Bot commented Aug 11, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95.03%. Comparing base (ca58f94) to head (74f08b6).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #7287      +/-   ##
==========================================
- Coverage   95.03%   95.03%   -0.01%     
==========================================
  Files         617      617              
  Lines       51637    51638       +1     
  Branches     9359     9359              
==========================================
  Hits        49075    49075              
  Misses       2021     2021              
- Partials      541      542       +1     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

encodeCbor() throws on objects with non-ASCII keys

2 participants