xref: /aosp_15_r20/external/compiler-rt/test/asan/TestCases/printf-2.c (revision 7c3d14c8b49c529e04be81a3ce6f5cc23712e4c6)
1*7c3d14c8STreehugger Robot // RUN: %clang_asan -O2 %s -o %t
2*7c3d14c8STreehugger Robot // We need replace_str=0, intercept_strlen=0 and replace_intrin=0 to avoid
3*7c3d14c8STreehugger Robot // reporting errors in strlen() and memcpy() called by printf().
4*7c3d14c8STreehugger Robot // RUN: %env_asan_opts=replace_str=0:intercept_strlen=0:replace_intrin=0:check_printf=1 not %run %t 2>&1 | FileCheck --check-prefix=CHECK-ON %s
5*7c3d14c8STreehugger Robot // RUN: %env_asan_opts=replace_str=0:intercept_strlen=0:replace_intrin=0:check_printf=0 %run %t 2>&1 | FileCheck --check-prefix=CHECK-OFF %s
6*7c3d14c8STreehugger Robot // RUN: %env_asan_opts=replace_str=0:intercept_strlen=0:replace_intrin=0 not %run %t 2>&1 | FileCheck --check-prefix=CHECK-ON %s
7*7c3d14c8STreehugger Robot 
8*7c3d14c8STreehugger Robot // FIXME: printf is not intercepted on Windows yet.
9*7c3d14c8STreehugger Robot // XFAIL: win32
10*7c3d14c8STreehugger Robot 
11*7c3d14c8STreehugger Robot #include <stdio.h>
12*7c3d14c8STreehugger Robot #include <stdlib.h>
13*7c3d14c8STreehugger Robot #include <string.h>
main()14*7c3d14c8STreehugger Robot int main() {
15*7c3d14c8STreehugger Robot   volatile char c = '0';
16*7c3d14c8STreehugger Robot   volatile int x = 12;
17*7c3d14c8STreehugger Robot   volatile float f = 1.239;
18*7c3d14c8STreehugger Robot   volatile char s[] = "34";
19*7c3d14c8STreehugger Robot   char *p = strdup((const char *)s);
20*7c3d14c8STreehugger Robot   free(p);
21*7c3d14c8STreehugger Robot   printf("%c %d %.3f %s\n", c, x, f, p);
22*7c3d14c8STreehugger Robot   return 0;
23*7c3d14c8STreehugger Robot   // Check that %s is sanitized.
24*7c3d14c8STreehugger Robot   // CHECK-ON: heap-use-after-free
25*7c3d14c8STreehugger Robot   // CHECK-ON-NOT: 0 12 1.239 34
26*7c3d14c8STreehugger Robot   // CHECK-OFF: 0 12 1.239
27*7c3d14c8STreehugger Robot }
28