Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 20 additions & 6 deletions modules/data/text/decoder/textdecoder.c
Original file line number Diff line number Diff line change
Expand Up @@ -187,10 +187,17 @@ void xs_textdecoder_decode(xsMachine *the)
else if (first > 0xF4) // no valid next byte
clen = 0;

const uint8_t *s = &utf8[1];
while (clen-- > 0) {
uint8_t c = c_read8(src);
if ((lower <= c) && (c <= upper))
src++;
uint8_t c = *s++;
if ((lower <= c) && (c <= upper)) {
if (bufferLength) {
bufferLength--;
buffer++;
}
else
src++;
}
else
break;
}
Expand Down Expand Up @@ -321,10 +328,17 @@ void xs_textdecoder_decode(xsMachine *the)
else if (first > 0xF4) // no valid next byte
clen = 0;

const uint8_t *s = &utf8[1];
while (clen-- > 0) {
uint8_t c = c_read8(src);
if ((lower <= c) && (c <= upper))
src++;
uint8_t c = *s++;
if ((lower <= c) && (c <= upper)) {
if (bufferLength) {
bufferLength--;
buffer++;
}
else
src++;
}
else
break;
}
Expand Down
9 changes: 9 additions & 0 deletions tests/modules/data/text/decoder/decodestream.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,12 @@ assert.sameValue("\uFFFD", decoder.decode());

assert.sameValue("", decoder.decode(Uint8Array.of(0xF0, 0x9F, 0x92), {stream: true}));
assert.sameValue("\uFFFD", decoder.decode());

// illegal sequence spanning a buffered partial lead and a short final chunk:
// the recovery scan must consume from the buffered bytes before src and stop at the
// end of the input. The chunk is a view whose backing store holds continuation bytes
// (0x90) past its length, so the bytes seen are F0 80 90 90: the 0x80 is below the
// 0x90 lower bound for an F0 lead, so it and the two trailing 0x90 each decode to U+FFFD.
assert.sameValue("", decoder.decode(Uint8Array.of(0xF0, 0x80), {stream: true}));
assert.sameValue("\uFFFD\uFFFD\uFFFD\uFFFD", decoder.decode(new Uint8Array(8).fill(0x90).subarray(0, 2), {stream: true}));
assert.sameValue("", decoder.decode());