Avoid overflow.

This commit is contained in:
Nuno Cruces
2025-06-12 12:32:44 +01:00
parent 69a2881a10
commit 0bdce8aa68
4 changed files with 9 additions and 4 deletions

Binary file not shown.

View File

@@ -1325,11 +1325,11 @@
)
(i32.const 0)
(i32.le_u
(local.get $0)
(i32.add
(local.get $1)
(i32.sub
(local.get $0)
(local.get $3)
)
(local.get $1)
)
)
)

View File

@@ -113,7 +113,7 @@ void *memchr(const void *v, int c, size_t n) {
// That's a match, unless it is beyond the end of the object.
// Recall that we decremented n, so less-than-or-equal-to is correct.
size_t ctz = __builtin_ctz(mask);
return ctz <= n + align ? (char *)w + ctz : NULL;
return ctz - align <= n ? (char *)w + ctz : NULL;
}
}
// Decrement n; if it overflows we're done.
@@ -166,6 +166,8 @@ size_t strlen(const char *s) {
// At least one bit will be set, unless we cleared them.
// Knowing this helps the compiler.
__builtin_assume(mask || align);
// If the mask is zero because of alignment,
// it's as if we didn't find anything.
if (mask) {
// Find the offset of the first one bit (little-endian).
return (char *)w - s + __builtin_ctz(mask);
@@ -280,6 +282,8 @@ static char *__strchrnul(const char *s, int c) {
// At least one bit will be set, unless we cleared them.
// Knowing this helps the compiler.
__builtin_assume(mask || align);
// If the mask is zero because of alignment,
// it's as if we didn't find anything.
if (mask) {
// Find the offset of the first one bit (little-endian).
return (char *)w + __builtin_ctz(mask);

View File

@@ -57,6 +57,7 @@ int bcmp(const void *v1, const void *v2, size_t n) {
#endif // __OPTIMIZE_SIZE__
__attribute__((always_inline))
static v128_t __tolower8x16(v128_t v) {
__i8x16 i = v;
i = i + wasm_i8x16_splat(INT8_MAX - ('Z'));