Skip to content

Commit

Permalink
Bug 1879997 - use CountLeadingZeroes32 r=spidermonkey-reviewers,arai
Browse files Browse the repository at this point in the history
  • Loading branch information
hotsphink committed Feb 22, 2024
1 parent aa561a6 commit eb36536
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions js/src/vm/CharacterEncoding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -286,11 +286,6 @@ static bool InflateUTF8ToUTF16(JSContext* cx, const UTF8Chars& src,
break;
}
} else {
// Non-ASCII code unit. Determine its length in bytes (n).
uint32_t n = 1;
while (v & (0x80 >> n)) {
n++;
}

#define INVALID(report, arg, n2) \
do { \
Expand All @@ -315,6 +310,14 @@ static bool InflateUTF8ToUTF16(JSContext* cx, const UTF8Chars& src,
} \
} while (0)

// Non-ASCII code unit. Determine its length in bytes (n).
//
// Avoid undefined behavior from passing in 0
// (https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html#index-_005f_005fbuiltin_005fclz)
// by turning on the low bit so that 0xff will set n=31-24=7, which will
// be detected as an invalid character.
uint32_t n = mozilla::CountLeadingZeroes32(~int8_t(src[i]) | 0x1) - 24;

// Check the leading byte.
if (n < 2 || n > 4) {
INVALID(ReportInvalidCharacter, i, 1);
Expand Down

0 comments on commit eb36536

Please sign in to comment.