1// RUN: %clang_asan -O2 %s -o %t
2// RUN: %env_asan_opts=check_printf=1 not %run %t 2>&1 | FileCheck --check-prefix=CHECK-ON %s
3// RUN: not %run %t 2>&1 | FileCheck --check-prefix=CHECK-ON %s
4
5// FIXME: sprintf is not intercepted on Windows yet. But this test can
6// pass if sprintf calls memmove, which is intercepted, so we can't XFAIL it.
7// UNSUPPORTED: target={{.*windows-msvc.*}}
8
9#include <stdio.h>
10int main() {
11 volatile char c = '0';
12 volatile int x = 12;
13 volatile float f = 1.239;
14 volatile char s[] = "34";
15 volatile char buf[2];
16 fputs(s: "before sprintf\n", stderr);
17 sprintf(s: (char *)buf, format: "%c %d %.3f %s\n", c, x, f, s);
18 fputs(s: "after sprintf", stderr);
19 fputs(s: (const char *)buf, stderr);
20 return 0;
21 // Check that size of output buffer is sanitized.
22 // CHECK-ON: before sprintf
23 // CHECK-ON-NOT: after sprintf
24 // CHECK-ON: stack-buffer-overflow
25 // CHECK-ON-NOT: 0 12 1.239 34
26}
27

source code of compiler-rt/test/asan/TestCases/printf-4.c