From 1e6d280b9bbbb249517c8bfbc9b3a811d58452f0 Mon Sep 17 00:00:00 2001 From: Ze-Zheng Wu Date: Sat, 22 Jun 2024 12:00:44 +0800 Subject: [PATCH] fix: out-of-bounds access of code units --- src/playground/utils.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/playground/utils.ts b/src/playground/utils.ts index 0d19e48cf..3e0804384 100644 --- a/src/playground/utils.ts +++ b/src/playground/utils.ts @@ -315,7 +315,7 @@ function getUtf8ByteLength(codeUnit: string) { if (code < 65536) { return 3; } - throw `Bad UTF-16 code unit: ${codeUnit}`; + throw new Error(`Bad UTF-16 code unit "${codeUnit}" with code ${code}`); } /** @@ -333,7 +333,7 @@ export function spanInBytesToSpanInCodeUnits( // Scan through the string, looking for the start of the substring let bytePos = 0; - while (bytePos < startInBytes) { + while (bytePos < startInBytes && currCodeUnitIndex < str.length) { const byteLength = getUtf8ByteLength(str.charAt(currCodeUnitIndex)); bytePos += byteLength; ++currCodeUnitIndex; @@ -348,7 +348,7 @@ export function spanInBytesToSpanInCodeUnits( spanInCodeUnits[0] = currCodeUnitIndex; // Now scan through the following string to find the end - while (bytePos < endInBytes) { + while (bytePos < endInBytes && currCodeUnitIndex < str.length) { const byteLength = getUtf8ByteLength(str.charAt(currCodeUnitIndex)); bytePos += byteLength; ++currCodeUnitIndex;