Skip to content

Commit

Permalink
Normative: throw when trying to write to a detached buffer (#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
bakkot authored Jan 25, 2024
1 parent db3947c commit 67c5b29
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 5 deletions.
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"test": "node --test"
},
"dependencies": {
"@tc39/ecma262-biblio": "^2.1.2663",
"@tc39/ecma262-biblio": "2.1.2672",
"ecmarkup": "^18.0.0",
"jsdom": "^21.1.1",
"prismjs": "^1.29.0"
Expand Down
6 changes: 6 additions & 0 deletions playground/polyfill-core.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,9 @@ export function base64ToUint8Array(string, options, into) {
if (!['loose', 'strict', 'stop-before-partial'].includes(lastChunkHandling)) {
throw new TypeError('expected lastChunkHandling to be either "loose", "strict", or "stop-before-partial"');
}
if (into && 'detached' in into.buffer && into.buffer.detached) {
throw new TypeError('toBase64Into called on array backed by detached buffer');
}

let maxLength = into ? into.length : 2 ** 53 - 1;

Expand Down Expand Up @@ -260,6 +263,9 @@ export function hexToUint8Array(string, into) {
if (/[^0-9a-fA-F]/.test(string)) {
throw new SyntaxError('string should only contain hex characters');
}
if (into && 'detached' in into.buffer && into.buffer.detached) {
throw new TypeError('fromHexInto called on array backed by detached buffer');
}

let maxLength = into ? into.length : 2 ** 53 - 1;

Expand Down
2 changes: 2 additions & 0 deletions spec.html
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ <h1>Uint8Array.fromBase64Into ( _string_, _into_ [ , _options_ ] )</h1>
1. If _lastChunkHandling_ is *undefined*, set _lastChunkHandling_ to *"loose"*.
1. If _lastChunkHandling_ is not one of *"loose"*, *"strict"*, or *"stop-before-partial"*, throw a *TypeError* exception.
1. Let _taRecord_ be MakeTypedArrayWithBufferWitnessRecord(_into_, ~seq-cst~).
1. If IsTypedArrayOutOfBounds(_taRecord_) is *true*, throw a *TypeError* exception.
1. Let _byteLength_ be TypedArrayByteLength(_taRecord_).
1. Let _maxLength_ be _byteLength_.
1. Let _result_ be ? FromBase64(_string_, _alphabet_, _lastChunkHandling_, _maxLength_).
Expand Down Expand Up @@ -121,6 +122,7 @@ <h1>Uint8Array.fromHexInto ( _string_, _into_ )</h1>
1. If _string_ is not a String, throw a *TypeError* exception.
1. Perform ? ValidateUint8Array(_into_).
1. Let _taRecord_ be MakeTypedArrayWithBufferWitnessRecord(_into_, ~seq-cst~).
1. If IsTypedArrayOutOfBounds(_taRecord_) is *true*, throw a *TypeError* exception.
1. Let _byteLength_ be TypedArrayByteLength(_taRecord_).
1. Let _maxLength_ be _byteLength_.
1. Let _result_ be ? FromHex(_string_, _maxLength_).
Expand Down

0 comments on commit 67c5b29

Please sign in to comment.