1// RUN: %clangxx_asan -g %s -o %t
2// RUN: not %run %t 2>&1 | FileCheck %s --check-prefix=CHECK-FWRITE
3// RUN: not %run %t 1 2>&1 | FileCheck %s --check-prefix=CHECK-FREAD
4
5#include <stdio.h>
6#include <stdlib.h>
7
8int test_fread() {
9 FILE *f = fopen(filename: "/dev/zero", modes: "r");
10 char buf[2];
11 fread(ptr: buf, size: sizeof(buf), n: 2, stream: f); // BOOM
12 fclose(stream: f);
13 return 0;
14}
15
16int test_fwrite() {
17 FILE *f = fopen(filename: "/dev/null", modes: "w");
18 char buf[2];
19 fwrite(ptr: buf, size: sizeof(buf), n: 2, s: f); // BOOM
20 return fclose(stream: f);
21}
22
23int main(int argc, char *argv[]) {
24 if (argc > 1)
25 test_fread();
26 else
27 test_fwrite();
28 return 0;
29}
30
31// CHECK-FREAD: {{.*ERROR: AddressSanitizer: stack-buffer-overflow}}
32// CHECK-FREAD: #{{.*}} in {{(wrap_|__interceptor_)?}}fread
33// CHECK-FWRITE: {{.*ERROR: AddressSanitizer: stack-buffer-overflow}}
34// CHECK-FWRITE: #{{.*}} in {{(wrap_|__interceptor_)?}}fwrite
35

source code of compiler-rt/test/asan/TestCases/Posix/fread_fwrite.cpp