Skip to content

Commit c334869

Browse files
authored
[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_`.
1 parent 539b15b commit c334869

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

compiler-rt/lib/asan/asan_interceptors.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,9 @@ INTERCEPTOR(char *, strcpy, char *to, const char *from) {
584584
INTERCEPTOR(char*, strdup, const char *s) {
585585
void *ctx;
586586
ASAN_INTERCEPTOR_ENTER(ctx, strdup);
587+
// Allowing null input is Windows-specific
588+
if (SANITIZER_WINDOWS && UNLIKELY(!s))
589+
return nullptr;
587590
if (UNLIKELY(!TryAsanInitFromRtl()))
588591
return internal_strdup(s);
589592
uptr length = internal_strlen(s);
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// RUN: %clang_cl_asan %s -Fe%t.exe
2+
// RUN: %run %t.exe | FileCheck %s
3+
4+
// CHECK: Success
5+
6+
#include <malloc.h>
7+
#include <stdio.h>
8+
#include <string.h>
9+
10+
int main() {
11+
// Null input is valid to strdup on Windows.
12+
char *nullStr = _strdup(nullptr);
13+
free(nullStr);
14+
puts("Success");
15+
return 0;
16+
}

0 commit comments

Comments
 (0)