From 6d620166b40c8565845b7459f6c4947ba30737ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Breitbart?= Date: Thu, 24 Oct 2019 17:52:03 +0200 Subject: [PATCH 1/2] fix typo in decoder and add a test case --- src/common/input/TextDecoder.test.ts | 12 ++++++++++++ src/common/input/TextDecoder.ts | 2 +- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/common/input/TextDecoder.test.ts b/src/common/input/TextDecoder.test.ts index cda74a18..ed42ce05 100644 --- a/src/common/input/TextDecoder.test.ts +++ b/src/common/input/TextDecoder.test.ts @@ -7,6 +7,8 @@ import { assert } from 'chai'; import { StringToUtf32, stringFromCodePoint, Utf8ToUtf32, utf32ToString } from 'common/input/TextDecoder'; import { encode } from 'utf8'; +declare const console: any; + // convert UTF32 codepoints to string function toString(data: Uint32Array, length: number): string { if ((String as any).fromCodePoint) { @@ -214,6 +216,16 @@ describe('text encodings', () => { } assert(decoded, 'Ä€𝄞Ö𝄞€Ü𝄞€'); }); + it('test break after 3 bytes - issue #2495', () => { + const decoder = new Utf8ToUtf32(); + const target = new Uint32Array(5); + const utf8Data = fromByteString('\xf0\xa0\x9c\x8e'); + let written = decoder.decode(utf8Data.slice(0, 3), target); + assert.equal(written, 0); + written = decoder.decode(utf8Data.slice(3), target); + assert.equal(written, 1); + assert(toString(target, written), '𠜎'); + }); }); }); }); diff --git a/src/common/input/TextDecoder.ts b/src/common/input/TextDecoder.ts index 7e141e02..397d25a7 100644 --- a/src/common/input/TextDecoder.ts +++ b/src/common/input/TextDecoder.ts @@ -194,7 +194,7 @@ export class Utf8ToUtf32 { target[size++] = cp; } } else { - if (codepoint < 0x010000 || codepoint > 0x10FFFF) { + if (cp < 0x010000 || cp > 0x10FFFF) { // illegal codepoint } else { target[size++] = cp; From 30d3d4602707f2382572436706fa012e8e8d8115 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Breitbart?= Date: Thu, 24 Oct 2019 18:04:44 +0200 Subject: [PATCH 2/2] make linter happy --- src/common/input/TextDecoder.test.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/common/input/TextDecoder.test.ts b/src/common/input/TextDecoder.test.ts index ed42ce05..92b0e03a 100644 --- a/src/common/input/TextDecoder.test.ts +++ b/src/common/input/TextDecoder.test.ts @@ -7,7 +7,6 @@ import { assert } from 'chai'; import { StringToUtf32, stringFromCodePoint, Utf8ToUtf32, utf32ToString } from 'common/input/TextDecoder'; import { encode } from 'utf8'; -declare const console: any; // convert UTF32 codepoints to string function toString(data: Uint32Array, length: number): string {