Different error behaviour for invalid characters in "from{Base64,Hex}Into"
anba opened this issue · 1 comments
FromHex checks that all characters are valid hex-characters, whereas FromBase64 ignores trailing garbage when maxLength has been reached. It's probably best to change FromBase64 to validate the complete input, otherwise this can happen:
let s = /* base64 string with trailing garbage */;
Uint8Array.fromBase64Into(s, smallTypedArray); // No error.
Uint8Array.fromBase64Into(s, largeTypedArray); // Throws error when trailing garbage is processed.Implementations which want to avoid additional allocations will need to process the input string two times anyway, because as currently spec'ed, FromBase64 stores the processed bytes into an intermediary List before storing them into the output Uint8Array. (The intermediary List will need to be heap-allocated, at least for large string inputs.)
Good catch, but the fix is to change FromHex, not FromBase64. The intention is that implementations should only need to read the string up through the point where the buffer is full and then stop, rather than needing to read the entire string. That's important to avoid quadratic behavior when the input is much larger than the output buffer.