[v20.5.0] Buffer.allocUnsafe and TextDecoder.decode doesn't play well.
Version
v20.5.0
Platform
Linux dell 6.4.0-1-amd64 #1 SMP PREEMPT_DYNAMIC Debian 6.4.4-1 (2023-07-23) x86_64 GNU/Linux
Subsystem
No response
What steps will reproduce the bug?
Here is some code to reproduce and explore the bug.
Both Buffer.from([0x80]) and Buffer.allocUnsafe(1)[0] = 0x80 will cause unstable behaviour in TextDecoder.decode
let error1Count = 0 let error2Count = 0 for (let index = 0; index < 20000; index++) { try { const buffer = Buffer.from([0x80]) // this is unstable // const buffer = Buffer.allocUnsafe(1) // this is unstable // const buffer = Buffer.alloc(1) // this is stable buffer[0] = 0x80 const data = new TextDecoder('utf-8', {fatal: true}).decode(buffer) error1Count ++ // then data is the '\uFFFD' replacement character } catch {} try { // this is stable const data = new TextDecoder('utf-8', {fatal: true}).decode(new Uint8Array([0x80])) error2Count ++ } catch {} } console.log(error1Count, error2Count)
How often does it reproduce? Is there a required condition?
Sometimes I have to run the code snippet a few times before the bug is seen.
What is the expected behavior? Why is that the expected behavior?
Since I'm running the TextDecoder with {fatal: true} I expect the invalid input to ALWAYS throw an error.
What do you see instead?
A few times (pretty much random) instead of throwing an error it outputs the \uFFFD replacement character. This should not be possible when {fatal: true} is used.
Additional information
I have only tested on Linux Debian with Node v20.5.0.