[llvm-branch-commits] [compiler-rt] [sanitizer_common] Check full source string length in strlcpy (PR #239763)
Vitaly Buka via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Fri Nov 6 11:48:35 PST 2026
https://github.com/vitalybuka updated https://github.com/llvm/llvm-project/pull/239763
>From 64a11b8ec6811e9e621897c6032f185a3b4119a3 Mon Sep 17 00:00:00 2001
From: Vitaly Buka <vitalybuka at google.com>
Date: Fri, 6 Nov 2026 11:44:51 -0800
Subject: [PATCH] [spr] update
Created using spr 2.0.0-beta.2
---
.../sanitizer_common_interceptors.inc | 14 ++++-----
.../TestCases/Posix/strlcpy_oob.cpp | 31 +++++++++++++++++++
2 files changed, 37 insertions(+), 8 deletions(-)
create mode 100644 compiler-rt/test/sanitizer_common/TestCases/Posix/strlcpy_oob.cpp
diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc b/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc
index 2e5a7fb5e14ad..9a8e92635653f 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc
@@ -7722,17 +7722,15 @@ INTERCEPTOR(int, open_by_handle_at, int mount_fd, struct file_handle* handle,
#if SANITIZER_INTERCEPT_STRLCPY
INTERCEPTOR(SIZE_T, strlcpy, char *dst, char *src, SIZE_T size) {
void *ctx;
- SIZE_T res;
+ SIZE_T srclen = 0;
COMMON_INTERCEPTOR_ENTER(ctx, strlcpy, dst, src, size);
if (src) {
- // Keep strnlen as macro argument, as macro may ignore it.
- COMMON_INTERCEPTOR_READ_STRING(
- ctx, src, Min(internal_strnlen(src, size), size - 1) + 1);
+ srclen = internal_strlen(src);
+ COMMON_INTERCEPTOR_READ_RANGE(ctx, src, srclen + 1);
}
- res = REAL(strlcpy)(dst, src, size);
- if (size > 0)
- COMMON_INTERCEPTOR_COPY_STRING(ctx, dst, src, Min(res, size - 1));
- return res;
+ if (dst && src && size > 0)
+ COMMON_INTERCEPTOR_COPY_STRING(ctx, dst, src, Min(srclen, size - 1));
+ return REAL(strlcpy)(dst, src, size);
}
INTERCEPTOR(SIZE_T, strlcat, char *dst, char *src, SIZE_T size) {
diff --git a/compiler-rt/test/sanitizer_common/TestCases/Posix/strlcpy_oob.cpp b/compiler-rt/test/sanitizer_common/TestCases/Posix/strlcpy_oob.cpp
new file mode 100644
index 0000000000000..dbe5191fd42ea
--- /dev/null
+++ b/compiler-rt/test/sanitizer_common/TestCases/Posix/strlcpy_oob.cpp
@@ -0,0 +1,31 @@
+// RUN: %clangxx -O0 -g %s -o %t
+// RUN: not %run %t src_zero 2>&1 | FileCheck %s --check-prefix=SRC-ZERO
+// RUN: not %run %t src_small 2>&1 | FileCheck %s --check-prefix=SRC-SMALL
+
+// REQUIRES: asan || msan
+// UNSUPPORTED: target={{.*linux.*}} && !glibc-2.38, target={{.*solaris.*}}
+
+#include <string.h>
+
+#include "sanitizer_common/sanitizer_specific.h"
+
+int main(int argc, char **argv) {
+ if (argc < 2)
+ return 1;
+
+ alignas(8) char src[8] = "abcd";
+ char dst[8] = {};
+ make_mem_bad(src + 3, 5);
+
+ if (strcmp(argv[1], "src_zero") == 0) {
+ // SRC-ZERO: strlcpy
+ // SRC-ZERO: SUMMARY: {{Address|Memory}}Sanitizer:
+ return strlcpy(dst, src, 0) == 99;
+ }
+ if (strcmp(argv[1], "src_small") == 0) {
+ // SRC-SMALL: strlcpy
+ // SRC-SMALL: SUMMARY: {{Address|Memory}}Sanitizer:
+ return strlcpy(dst, src, 2) == 99;
+ }
+ return 1;
+}
More information about the llvm-branch-commits
mailing list