Skip to content

Commit 0bdce8a

Browse files
committed
Avoid overflow.
1 parent 69a2881 commit 0bdce8a

File tree

4 files changed

+9
-4
lines changed

4 files changed

+9
-4
lines changed

sqlite3/libc/libc.wasm

0 Bytes
Binary file not shown.

sqlite3/libc/libc.wat

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1325,11 +1325,11 @@
13251325
)
13261326
(i32.const 0)
13271327
(i32.le_u
1328-
(local.get $0)
1329-
(i32.add
1330-
(local.get $1)
1328+
(i32.sub
1329+
(local.get $0)
13311330
(local.get $3)
13321331
)
1332+
(local.get $1)
13331333
)
13341334
)
13351335
)

sqlite3/libc/string.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ void *memchr(const void *v, int c, size_t n) {
113113
// That's a match, unless it is beyond the end of the object.
114114
// Recall that we decremented n, so less-than-or-equal-to is correct.
115115
size_t ctz = __builtin_ctz(mask);
116-
return ctz <= n + align ? (char *)w + ctz : NULL;
116+
return ctz - align <= n ? (char *)w + ctz : NULL;
117117
}
118118
}
119119
// Decrement n; if it overflows we're done.
@@ -166,6 +166,8 @@ size_t strlen(const char *s) {
166166
// At least one bit will be set, unless we cleared them.
167167
// Knowing this helps the compiler.
168168
__builtin_assume(mask || align);
169+
// If the mask is zero because of alignment,
170+
// it's as if we didn't find anything.
169171
if (mask) {
170172
// Find the offset of the first one bit (little-endian).
171173
return (char *)w - s + __builtin_ctz(mask);
@@ -280,6 +282,8 @@ static char *__strchrnul(const char *s, int c) {
280282
// At least one bit will be set, unless we cleared them.
281283
// Knowing this helps the compiler.
282284
__builtin_assume(mask || align);
285+
// If the mask is zero because of alignment,
286+
// it's as if we didn't find anything.
283287
if (mask) {
284288
// Find the offset of the first one bit (little-endian).
285289
return (char *)w + __builtin_ctz(mask);

sqlite3/libc/strings.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ int bcmp(const void *v1, const void *v2, size_t n) {
5757

5858
#endif // __OPTIMIZE_SIZE__
5959

60+
__attribute__((always_inline))
6061
static v128_t __tolower8x16(v128_t v) {
6162
__i8x16 i = v;
6263
i = i + wasm_i8x16_splat(INT8_MAX - ('Z'));

0 commit comments

Comments
 (0)