Skip to content

Apply [Clamp] to Blob.slice() and discard Blob types with U+007F - #8793

Merged
littledivy merged 2 commits into
mainfrom
littledivy/2026-11-21-wpt-blob-slice
Nov 23, 2026
Merged

littledivy merged 2 commits into
mainfrom
littledivy/2026-11-21-wpt-blob-slice

Conversation

@littledivy

Copy link
Copy Markdown
Member

The FileAPI spec declares Blob.slice()'s start and end as [Clamp] long long. workerd took them as int, which truncates fractions, turns Infinity into 0 and throws a TypeError outside the int32 range. So slice(1.5) started at byte 1 instead of 2, and slice(0, Infinity) returned an empty Blob. The type normalization also kept types containing U+007F, though the spec discards any type with a character outside U+0020 to U+007E.

In this patch slice() takes doubles and rounds each bound half to even, with NaN as 0, before the usual negative-offset and clamp steps. normalizeType() also discards U+007F. No call that works today starts throwing: values beyond int32 now clamp instead of throwing, fractional and infinite bounds give the spec's result, and a type containing U+007F reads back as "". We accept these without a compatibility flag, since the old results match no browser.

The six fractional-bound expected failures in Blob-slice and the U+007F ones in Blob-constructor, Blob-slice and File-constructor are removed, and blob-test gets cases for both.

//src/wpt:FileAPI@, //src/wpt:FileAPI@all-autogates and //src/workerd/api/tests:blob-test@ each passed 100 of 100 runs with --runs_per_test=100.

@littledivy
littledivy requested review from a team as code owners November 21, 2026 17:55

@clanky-the-clanker clanky-the-clanker Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Superseded by the review of 777c5538.

This review, of 1420906e

✅ Approved — The [Clamp] handling and the U+007F check are correct and covered by the new tests. No issues found.

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


✅ All checks passed

📋 Code Quality: ✅ LGTM

  • relativeOffset handles NaN, ties-to-even rounding, negative offsets and clamping to [0, size], so the size_t cast is safe.
  • normalizeType() now rejects bytes outside 0x20–0x7E, which includes U+007F; bytes of 0x80 and above are rejected as before.
  • The tests cover the new behaviour, and the WPT expectations that the change fixes are removed.

📝 Documentation: ✅ LGTM

The new comment on relativeOffset describes the current behaviour accurately and links to the spec algorithm.


Reviewed commit: 1420906e

Comment thread src/workerd/api/blob.c++ Resolved
The FileAPI spec declares Blob.slice()'s start and end as [Clamp] long long, so fractional values round to the nearest integer with ties to even, NaN becomes 0, and out-of-range values clamp. workerd took them as int, which truncates fractions, turns Infinity into 0 and throws a TypeError outside the int32 range. So `slice(1.5)` started at byte 1 instead of 2, and `slice(0, Infinity)` returned an empty Blob.

In this patch slice() takes doubles and converts each one to an offset in the Blob with round-half-to-even, NaN as 0, and a clamp to [0, size] after negative values count back from the end. No call that works today starts throwing: fractional and infinite arguments give the spec's result, and values beyond int32 now clamp instead of throwing. We accept that without a compatibility flag, since the old results ignored the spec's conversion and match no browser. The six Blob-slice WPT expected failures for fractional bounds are removed, and blob-test gets a case for the conversion.
The Blob and File constructors and Blob.slice() set the type to the empty string when it contains a character outside U+0020 to U+007E. normalizeType() only discarded characters below U+0020 and above U+007F, so a type containing U+007F (DEL) was kept.

In this patch normalizeType() also discards U+007F. No call starts throwing; such a type now reads back as "" instead of the lowercased string, and we accept that without a compatibility flag because the old value is one the spec and browsers never produce. The U+007F expected failures in the Blob-constructor, Blob-slice and File-constructor WPTs are removed, and blob-test checks the constructor and slice().
@littledivy
littledivy force-pushed the littledivy/2026-11-21-wpt-blob-slice branch from 1420906 to 777c553 Compare November 23, 2026 15:45

@clanky-the-clanker clanky-the-clanker Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

✅ Approved — The [Clamp] conversion for slice() bounds and the U+007F check in type normalization are correct and covered by tests; no issues found.

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


✅ All checks passed

📋 Code Quality: ✅ LGTM

  • relativeOffset() handles NaN, ties-to-even rounding, negative offsets and clamping to [0, size], so the size_t cast is safe. end is raised to at least start.
  • normalizeType() rejects bytes outside 0x20–0x7E, which covers U+007F and bytes of 0x80 and above.
  • The slice signature in blob.h matches the implementation. The tests cover the new behaviour, and the WPT expected failures that this change fixes are removed.

📝 Documentation: ✅ LGTM

The comment on relativeOffset() describes the current behaviour accurately and links to the spec's slice-blob algorithm, as the reviewer asked.


Reviewed commit: 777c5538

@littledivy
littledivy merged commit 1cae2bf into main Nov 23, 2026
36 of 38 checks passed
@littledivy
littledivy deleted the littledivy/2026-11-21-wpt-blob-slice branch November 23, 2026 17:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants