1// RUN: %clangxx_msan %s -o %t && not %run %t 2>&1 | FileCheck --check-prefix=ON %s
2// RUN: %clangxx_msan %s -o %t && MSAN_OPTIONS=intercept_strndup=0 %run %t 2>&1 | FileCheck --check-prefix=OFF --allow-empty %s
3
4// When built as C on Linux, strndup is transformed to __strndup.
5// RUN: %clangxx_msan -O3 -xc %s -o %t && not %run %t 2>&1 | FileCheck --check-prefix=ON %s
6
7// UNSUPPORTED: target={{.*windows-msvc.*}}
8
9#include <assert.h>
10#include <stdlib.h>
11#include <string.h>
12#include <sanitizer/msan_interface.h>
13
14int main(int argc, char **argv) {
15 char kString[4] = "abc";
16 __msan_poison(a: kString + 2, size: 1);
17 char *copy = strndup(string: kString, n: 4); // BOOM
18 assert(__msan_test_shadow(copy, 4) == 2); // Poisoning is preserved.
19 free(ptr: copy);
20 return 0;
21 // ON: Uninitialized bytes in {{(__)?}}strndup at offset 2 inside [{{.*}}, 4)
22 // ON: MemorySanitizer: use-of-uninitialized-value
23 // ON: #0 {{.*}}main {{.*}}strndup.cpp:[[@LINE-6]]
24 // ON-LABEL: SUMMARY
25 // ON: {{.*}}strndup.cpp:[[@LINE-8]]
26 // OFF-NOT: MemorySanitizer
27}
28
29

source code of compiler-rt/test/msan/strndup.cpp