| 1 | // RUN: %clangxx_msan -g %s -o %t |
| 2 | // RUN: %run %t |
| 3 | // RUN: not %run %t 2 2>&1 | FileCheck %s --check-prefix=CHECK-FPUTS |
| 4 | // RUN: not %run %t 3 3 2>&1 | FileCheck %s --check-prefix=CHECK-PUTS |
| 5 | |
| 6 | #include <stdio.h> |
| 7 | #include <stdlib.h> |
| 8 | #include <string.h> |
| 9 | |
| 10 | int test_fgets() { |
| 11 | FILE *fp = fopen(filename: "/dev/zero" , modes: "r" ); |
| 12 | char c; |
| 13 | |
| 14 | if (!fgets(s: &c, n: 1, stream: fp)) |
| 15 | return 1; |
| 16 | |
| 17 | if (c == '1') // No error |
| 18 | return 2; |
| 19 | |
| 20 | fclose(stream: fp); |
| 21 | return 0; |
| 22 | } |
| 23 | |
| 24 | int test_fputs() { |
| 25 | FILE *fp = fopen(filename: "/dev/null" , modes: "w" ); |
| 26 | char buf[2]; |
| 27 | fputs(s: buf, stream: fp); // BOOM |
| 28 | return fclose(stream: fp); |
| 29 | } |
| 30 | |
| 31 | void test_puts() { |
| 32 | char buf[2]; |
| 33 | puts(s: buf); // BOOM |
| 34 | } |
| 35 | |
| 36 | int main(int argc, char *argv[]) { |
| 37 | if (argc == 1) |
| 38 | test_fgets(); |
| 39 | else if (argc == 2) |
| 40 | test_fputs(); |
| 41 | else |
| 42 | test_puts(); |
| 43 | return 0; |
| 44 | } |
| 45 | |
| 46 | // CHECK-FPUTS: Uninitialized bytes in fputs at offset 0 inside |
| 47 | // CHECK-PUTS: Uninitialized bytes in puts at offset 0 inside |
| 48 | |