Skip to content

Commit 99504aa

Browse files
committed
Intercept strlcpy and strlcat for msan on Clang 17 (#12674)
1 parent d966c29 commit 99504aa

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

Zend/zend_string.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@
2323
# include "valgrind/callgrind.h"
2424
#endif
2525

26+
#if __has_feature(memory_sanitizer)
27+
# include <sanitizer/msan_interface.h>
28+
#endif
29+
2630
ZEND_API zend_new_interned_string_func_t zend_new_interned_string;
2731
ZEND_API zend_string_init_interned_func_t zend_string_init_interned;
2832
ZEND_API zend_string_init_existing_interned_func_t zend_string_init_existing_interned;
@@ -490,3 +494,27 @@ ZEND_API zend_string *zend_string_concat3(
490494

491495
return res;
492496
}
497+
498+
/* strlcpy and strlcat are not intercepted by msan, so we need to do it ourselves. */
499+
#if __has_feature(memory_sanitizer)
500+
static size_t (*libc_strlcpy)(char *__restrict, const char *__restrict, size_t);
501+
size_t strlcpy(char *__restrict dest, const char *__restrict src, size_t n)
502+
{
503+
if (!libc_strlcpy) {
504+
libc_strlcpy = dlsym(RTLD_NEXT, "strlcpy");
505+
}
506+
size_t result = libc_strlcpy(dest, src, n);
507+
__msan_unpoison_string(dest);
508+
return result;
509+
}
510+
static size_t (*libc_strlcat)(char *__restrict, const char *__restrict, size_t);
511+
size_t strlcat (char *__restrict dest, const char *restrict src, size_t n)
512+
{
513+
if (!libc_strlcat) {
514+
libc_strlcat = dlsym(RTLD_NEXT, "strlcat");
515+
}
516+
size_t result = libc_strlcat(dest, src, n);
517+
__msan_unpoison_string(dest);
518+
return result;
519+
}
520+
#endif

0 commit comments

Comments
 (0)