[compiler-rt] c334869 - [ASan] Change strdup interceptor to allow null input on Windows (#122803)

via llvm-commits llvm-commits at lists.llvm.org
Tue Jan 14 08:53:05 PST 2025


Author: Zack Johnson
Date: 2025-01-14T11:53:01-05:00
New Revision: c33486954bd1404495581e42ed62428fb7abeae9

URL: https://github.com/llvm/llvm-project/commit/c33486954bd1404495581e42ed62428fb7abeae9
DIFF: https://github.com/llvm/llvm-project/commit/c33486954bd1404495581e42ed62428fb7abeae9.diff

LOG: [ASan] Change strdup interceptor to allow null input on Windows (#122803)

[These are the MS
Docs](https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/strdup-wcsdup-mbsdup?view=msvc-170)
regarding `strdup`, but they don't explicitly mention this. The SAL
annotations on `strdup` do, though, with the input parameter being
marked `_In_opt_z_`.

Added: 
    compiler-rt/test/asan/TestCases/Windows/msvc/strdup_null_input.cpp

Modified: 
    compiler-rt/lib/asan/asan_interceptors.cpp

Removed: 
    


################################################################################
diff  --git a/compiler-rt/lib/asan/asan_interceptors.cpp b/compiler-rt/lib/asan/asan_interceptors.cpp
index e6c95dfb82c320..247ea1b92f1f42 100644
--- a/compiler-rt/lib/asan/asan_interceptors.cpp
+++ b/compiler-rt/lib/asan/asan_interceptors.cpp
@@ -584,6 +584,9 @@ INTERCEPTOR(char *, strcpy, char *to, const char *from) {
 INTERCEPTOR(char*, strdup, const char *s) {
   void *ctx;
   ASAN_INTERCEPTOR_ENTER(ctx, strdup);
+  // Allowing null input is Windows-specific
+  if (SANITIZER_WINDOWS && UNLIKELY(!s))
+    return nullptr;
   if (UNLIKELY(!TryAsanInitFromRtl()))
     return internal_strdup(s);
   uptr length = internal_strlen(s);

diff  --git a/compiler-rt/test/asan/TestCases/Windows/msvc/strdup_null_input.cpp b/compiler-rt/test/asan/TestCases/Windows/msvc/strdup_null_input.cpp
new file mode 100644
index 00000000000000..9fd34fe9a08dd1
--- /dev/null
+++ b/compiler-rt/test/asan/TestCases/Windows/msvc/strdup_null_input.cpp
@@ -0,0 +1,16 @@
+// RUN: %clang_cl_asan %s -Fe%t.exe
+// RUN: %run %t.exe | FileCheck %s
+
+// CHECK: Success
+
+#include <malloc.h>
+#include <stdio.h>
+#include <string.h>
+
+int main() {
+  // Null input is valid to strdup on Windows.
+  char *nullStr = _strdup(nullptr);
+  free(nullStr);
+  puts("Success");
+  return 0;
+}


        


More information about the llvm-commits mailing list