[whatwg/encoding] UTF-8 / UTF-16 do not reset state when returning error (Issue #359)

ChALkeR created an issue (whatwg/encoding#359)

### What is the issue with the Encoding Standard?

Same as #358 but for Unicode BOM

I don't argue that they _should_, but there is definitely some sort of issue there

---

Platform status is highly inconsistent:

```js
import { test } from 'node:test'

const r = (d, ...a) => {
  try {
    return d.decode(...a).length
  } catch {}
  return 'e'
}

const a = new TextDecoder('utf8', { fatal: true })
console.log('',
  r(a, Uint8Array.of(0xef, 0xbb, 0xbf, 0xff), { stream: true }),
  r(a, Uint8Array.of(0xef, 0xbb, 0xbf), { stream: true }), // error does not stick in Chrome/Safari
)

const b = new TextDecoder('utf8', { fatal: true })
console.log('',
  r(b, Uint8Array.of(0xef, 0xbb, 0xbf, 0xef), { stream: true }),
  r(b, Uint8Array.of(0xef, 0xbb, 0xbf), { stream: true }), // error sticks in Chrome / Safari
  r(b, Uint8Array.of(0xef, 0xbb, 0xbf), { stream: true }),
  r(b, Uint8Array.of(0xbb, 0xbf), { stream: true })
)

const c = new TextDecoder('utf8', { fatal: true })
console.log('',
  r(c, Uint8Array.of(0xef, 0xbb, 0xbf), { stream: true }),
  r(c, Uint8Array.of(0xff), { stream: true }),
  r(c, Uint8Array.of(0xef, 0xbb, 0xbf), { stream: true }),
)
```

Chrome: first error did not get stuck, second error got stuck, bom got reset in one case but not the other
```
 e 0
 0 e e e
 0 e 1
```

WebKit: first error did not get stuck, second error got stuck, bom did not get reset both times
```
 e 1
 0 e e e
 0 e 1
```

Firefox, Servo, Deno, Static Hermes: errors do not stick, bom does not get reset
```
 e 1
 0 e 1 e
 0 e 1
```

Node.js: errors do not stick, bom got reset in one case but not the other
```
 e 0
 0 e 1 e
 0 e 1
```

Bun: just broken
```
 e 0
 0 0 0 1
 0 e 0
```

-- 
Reply to this email directly or view it on GitHub:
https://github.com/whatwg/encoding/issues/359
You are receiving this because you are subscribed to this thread.

Message ID: <whatwg/encoding/issues/359@github.com>

Received on Thursday, 22 January 2026 05:42:36 UTC