Visitar URL original
[Fix] `utils`: respect encoding of surrogate pairs across chunks by Q1Xuan · Pull Request #559 · ljharb/qs · GitHub
Skip to content

[Fix] utils: respect encoding of surrogate pairs across chunks - #559

Merged
ljharb merged 1 commit into
ljharb:mainfrom
Q1Xuan:fix-encode-surrogate-boundary
Jun 8, 2026
Merged

ljharb merged 1 commit into
ljharb:mainfrom
Q1Xuan:fix-encode-surrogate-boundary

Conversation

@Q1Xuan

@Q1Xuan Q1Xuan commented Jun 7, 2026

Copy link
Copy Markdown

Summary

  • avoid splitting UTF-16 surrogate pairs across the encoder's 1024-code-unit chunks
  • add a regression test for a surrogate pair that lands on the chunk boundary
  • rebuild the browser bundle

Bug

utils.encode chunks long strings before percent-encoding. When a high surrogate lands at the end of a chunk and the low surrogate is in the next chunk, the pair is encoded as two invalid code points instead of one Unicode character.

Example before this change:

var s = Array(1024).join('a') + '😀';
qs.stringify({ x: s }); // ended with %F0%9F%90%80%F2%90%80%80

After this change, the output correctly ends with %F0%9F%98%80.

Tests

  • npx tape "test/**/*.js"
  • npx tape test/utils.js
  • npx eclint check lib/utils.js test/utils.js
  • npx eslint lib/utils.js test/utils.js (no errors; existing no-continue warnings remain)
  • verified dist/qs.js with the boundary reproduction

Co-authored-by: QingXuan <16735394+q1ngxuan@user.noreply.gitee.com>
Co-authored-by: Jordan Harband <ljharb@gmail.com>
@ljharb
ljharb force-pushed the fix-encode-surrogate-boundary branch 2 times, most recently from eed1b55 to 59da434 Compare June 8, 2026 05:57

@ljharb ljharb left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reverted the dist change (only releases change that) and added some tests

@ljharb ljharb changed the title Fix encoding of surrogate pairs across chunks [Fix] utils: respect encoding of surrogate pairs across chunks Jun 8, 2026
@ljharb
ljharb merged commit 59da434 into ljharb:main Jun 8, 2026
632 of 878 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants