From 0bdce8aa689706a9c28f97eb5d3f2453d1f1ecbf Mon Sep 17 00:00:00 2001 From: Nuno Cruces Date: Thu, 12 Jun 2025 12:32:44 +0100 Subject: [PATCH] Avoid overflow. --- sqlite3/libc/libc.wasm | Bin 6300 -> 6300 bytes sqlite3/libc/libc.wat | 6 +++--- sqlite3/libc/string.h | 6 +++++- sqlite3/libc/strings.h | 1 + 4 files changed, 9 insertions(+), 4 deletions(-) diff --git a/sqlite3/libc/libc.wasm b/sqlite3/libc/libc.wasm index 821c6985bd441fe9ad1f23bbaf1f1b51db7f17f8..70fa201c6232a09d77a7c90c827ec84d347ef095 100755 GIT binary patch delta 17 YcmbPZILC0qR1OyAYz4;6GdY+A05l~8;s5{u delta 17 YcmbPZILC0qR1OwK1?H^HGdY+A05fI;;Q#;t diff --git a/sqlite3/libc/libc.wat b/sqlite3/libc/libc.wat index 2188a97..2349c96 100644 --- a/sqlite3/libc/libc.wat +++ b/sqlite3/libc/libc.wat @@ -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) ) ) ) diff --git a/sqlite3/libc/string.h b/sqlite3/libc/string.h index a040674..37c8718 100644 --- a/sqlite3/libc/string.h +++ b/sqlite3/libc/string.h @@ -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); diff --git a/sqlite3/libc/strings.h b/sqlite3/libc/strings.h index 4cd92c9..4f44eda 100644 --- a/sqlite3/libc/strings.h +++ b/sqlite3/libc/strings.h @@ -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'));