Skip to content

Editorial: assert the FromBase64 multiple-of-3 byte invariant#3875

Open
ljharb wants to merge 1 commit into
tc39:mainfrom
ljharb:ljharb/frombase64-multiple-of-three-assertion
Open

Editorial: assert the FromBase64 multiple-of-3 byte invariant#3875
ljharb wants to merge 1 commit into
tc39:mainfrom
ljharb:ljharb/frombase64-multiple-of-three-assertion

Conversation

@ljharb
Copy link
Copy Markdown
Member

@ljharb ljharb commented May 19, 2026

FromBase64 only appends bytes via DecodeFullLengthBase64Chunk (always 3 bytes at a time) inside step 10.l, and step 10.i preempts further reading when continuing to the next full chunk would exceed maxLength. Together these guarantee that, at the top of each iteration of the Repeat loop, the number of elements in bytes is a multiple of 3; partial outputs (1 or 2 bytes) only ever occur at end-of-stream via DecodeFinalBase64Chunk (step 10.b.i and step 10.e).

This invariant is currently implicit; making it an explicit Assert clarifies the algorithm's structure for both readers and implementers (e.g., it justifies why step 10.i's two conditions are exhaustive)

@ljharb ljharb requested a review from a team May 19, 2026 12:32
Comment thread spec.html Outdated
ljharb added a commit to ljharb/ecma262 that referenced this pull request May 19, 2026
)

`FromBase64` only appends bytes via `DecodeFullLengthBase64Chunk`
(always 3 bytes at a time)
inside step 10.l, and step 10.i preempts further reading when continuing to the next full chunk would exceed `maxLength`.
Together these guarantee that, at the top of each iteration of the Repeat loop, the number of elements in `bytes` is a multiple of 3;
partial outputs (1 or 2 bytes) only ever occur at end-of-stream via `DecodeFinalBase64Chunk` (step 10.b.i and step 10.e).

This invariant is currently implicit;
making it an explicit Assert clarifies the algorithm's structure for both readers and implementers
(e.g., it justifies why step 10.i's two conditions are exhaustive)
@ljharb ljharb force-pushed the ljharb/frombase64-multiple-of-three-assertion branch from 7f9fbe3 to 8a9213a Compare May 19, 2026 12:37
@github-actions
Copy link
Copy Markdown

The rendered spec preview for this PR is available as a single page at https://tc39.es/ecma262/pr/3875 and as multiple pages at https://tc39.es/ecma262/pr/3875/multipage .

@ljharb ljharb requested a review from michaelficarra May 20, 2026 12:05
@michaelficarra michaelficarra requested a review from a team May 20, 2026 12:12
)

`FromBase64` only appends bytes via `DecodeFullLengthBase64Chunk`
(always 3 bytes at a time)
inside step 10.l, and step 10.i preempts further reading when continuing to the next full chunk would exceed `maxLength`.
Together these guarantee that, at the top of each iteration of the Repeat loop, the number of elements in `bytes` is a multiple of 3;
partial outputs (1 or 2 bytes) only ever occur at end-of-stream via `DecodeFinalBase64Chunk` (step 10.b.i and step 10.e).

This invariant is currently implicit;
making it an explicit Assert clarifies the algorithm's structure for both readers and implementers
(e.g., it justifies why step 10.i's two conditions are exhaustive)
@ljharb ljharb force-pushed the ljharb/frombase64-multiple-of-three-assertion branch from 8a9213a to f3d8a11 Compare May 21, 2026 10:59
@linusg linusg added the ready to merge Editors believe this PR needs no further reviews, and is ready to land. label Jun 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

editorial change ready to merge Editors believe this PR needs no further reviews, and is ready to land.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants