Skip to content

Commit f3bd255

Browse files
committed
Avoid undefined behavior.
1 parent 8953f82 commit f3bd255

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

libc-top-half/musl/src/string/strlen.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,16 @@ size_t strlen(const char *s)
1616
#if defined(__wasm_simd128__) && defined(__wasilibc_simd_string)
1717
// strlen must stop as soon as it finds the terminator.
1818
// Aligning ensures loads beyond the terminator are safe.
19+
// Casting through uintptr_t makes this implementation-defined,
20+
// rather than undefined behavior.
1921
uintptr_t align = (uintptr_t)s % sizeof(v128_t);
20-
const v128_t *v = (v128_t *)(s - align);
22+
const v128_t *v = (v128_t *)((uintptr_t)s - align);
2123

2224
for (;;) {
25+
const v128_t vv = *v;
2326
// Bitmask is slow on AArch64, all_true is much faster.
24-
if (!wasm_i8x16_all_true(*v)) {
25-
const v128_t cmp = wasm_i8x16_eq(*v, (v128_t){});
27+
if (!wasm_i8x16_all_true(vv)) {
28+
const v128_t cmp = wasm_i8x16_eq(vv, (v128_t){});
2629
// Clear the bits corresponding to alignment (little-endian)
2730
// so we can count trailing zeros.
2831
int mask = wasm_i8x16_bitmask(cmp) >> align << align;

0 commit comments

Comments
 (0)