Skip to content

Commit 16f39ec

Browse files
authored
Intercept strlcpy and strlcat for msan on Clang 17 (#12674)
1 parent 7bcfac9 commit 16f39ec

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;
@@ -508,3 +512,27 @@ ZEND_API zend_string *zend_string_concat3(
508512

509513
return res;
510514
}
515+
516+
/* strlcpy and strlcat are not intercepted by msan, so we need to do it ourselves. */
517+
#if __has_feature(memory_sanitizer)
518+
static size_t (*libc_strlcpy)(char *__restrict, const char *__restrict, size_t);
519+
size_t strlcpy(char *__restrict dest, const char *__restrict src, size_t n)
520+
{
521+
if (!libc_strlcpy) {
522+
libc_strlcpy = dlsym(RTLD_NEXT, "strlcpy");
523+
}
524+
size_t result = libc_strlcpy(dest, src, n);
525+
__msan_unpoison_string(dest);
526+
return result;
527+
}
528+
static size_t (*libc_strlcat)(char *__restrict, const char *__restrict, size_t);
529+
size_t strlcat (char *__restrict dest, const char *restrict src, size_t n)
530+
{
531+
if (!libc_strlcat) {
532+
libc_strlcat = dlsym(RTLD_NEXT, "strlcat");
533+
}
534+
size_t result = libc_strlcat(dest, src, n);
535+
__msan_unpoison_string(dest);
536+
return result;
537+
}
538+
#endif

0 commit comments

Comments
 (0)