Skip to content

Commit 92ef515

Browse files
authored
refactor: cleaner bounds check in utf8.js deLoose (#54)
1 parent aff31b3 commit 92ef515

1 file changed

Lines changed: 4 additions & 4 deletions

File tree

utf8.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ function deLoose(str, loose, res) {
2727

2828
// Recheck if the string was encoded correctly
2929
let start = 0
30-
const last = res.length - 2
31-
// Search for EFBFBD
32-
while (start < last) {
30+
const last = res.length - 3
31+
// Search for EFBFBD (3-byte sequence)
32+
while (start <= last) {
3333
const pos = res.indexOf(0xef, start)
34-
if (pos === -1) break
34+
if (pos === -1 || pos > last) break
3535
start = pos + 1
3636
if (res[pos + 1] === 0xbf && res[pos + 2] === 0xbd) {
3737
// Found a replacement char in output, need to recheck if we encoded the input correctly

0 commit comments

Comments
 (0)