Skip to content

Commit

Permalink
fix: out-of-bounds access of code units (#650)
Browse files Browse the repository at this point in the history
  • Loading branch information
Sec-ant authored Jun 22, 2024
1 parent 2bf4e44 commit d95fefd
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/playground/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
}

/**
Expand All @@ -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;
Expand All @@ -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;
Expand Down

0 comments on commit d95fefd

Please sign in to comment.