diff options
-rw-r--r-- | compiler-rt/test/asan/TestCases/Posix/fgets_fputs.cc | 40 | ||||
-rw-r--r-- | compiler-rt/test/sanitizer_common/TestCases/Posix/fgets.cc | 4 |
2 files changed, 27 insertions, 17 deletions
diff --git a/compiler-rt/test/asan/TestCases/Posix/fgets_fputs.cc b/compiler-rt/test/asan/TestCases/Posix/fgets_fputs.cc index 6f06963936b..a126c78c741 100644 --- a/compiler-rt/test/asan/TestCases/Posix/fgets_fputs.cc +++ b/compiler-rt/test/asan/TestCases/Posix/fgets_fputs.cc @@ -1,41 +1,51 @@ // RUN: %clangxx_asan -g %s -o %t -// RUN: not %run %t 2>&1 | FileCheck %s --check-prefix=CHECK-FGETS +// RUN: echo data > %t-testdata +// RUN: not %run %t 1 %t-testdata 2>&1 | FileCheck %s --check-prefix=CHECK-FGETS // RUN: not %run %t 2 2>&1 | FileCheck %s --check-prefix=CHECK-FPUTS -// RUN: not %run %t 3 3 2>&1 | FileCheck %s --check-prefix=CHECK-PUTS +// RUN: not %run %t 3 2>&1 | FileCheck %s --check-prefix=CHECK-PUTS +#include <assert.h> #include <stdio.h> #include <stdlib.h> #include <string.h> -int test_fgets() { - FILE *fp = fopen("/etc/passwd", "r"); +int test_fgets(const char *testfile) { char buf[2]; + FILE *fp = fopen(testfile, "r"); + assert(fp); fgets(buf, sizeof(buf) + 1, fp); // BOOM fclose(fp); return 0; } int test_fputs() { - FILE *fp = fopen("/dev/null", "w"); char buf[1] = {'x'}; // Note: not nul-terminated - fputs(buf, fp); // BOOM - return fclose(fp); + FILE *fp = fopen("/dev/null", "w"); + assert(fp); + fputs(buf, fp); // BOOM + fclose(fp); + return 0; } -void test_puts() { +int test_puts() { char *p = strdup("x"); free(p); puts(p); // BOOM + return 0; } int main(int argc, char *argv[]) { - if (argc == 1) - test_fgets(); - else if (argc == 2) - test_fputs(); - else - test_puts(); - return 0; + assert(argc >= 2); + int testno = argv[1][0] - '0'; + if (testno == 1) { + assert(argc == 3); + return test_fgets(argv[2]); + } + if (testno == 2) + return test_fputs(); + if (testno == 3) + return test_puts(); + return 1; } // CHECK-FGETS: {{.*ERROR: AddressSanitizer: stack-buffer-overflow}} diff --git a/compiler-rt/test/sanitizer_common/TestCases/Posix/fgets.cc b/compiler-rt/test/sanitizer_common/TestCases/Posix/fgets.cc index 4eda4c1fcc9..8dde5cd1a84 100644 --- a/compiler-rt/test/sanitizer_common/TestCases/Posix/fgets.cc +++ b/compiler-rt/test/sanitizer_common/TestCases/Posix/fgets.cc @@ -2,12 +2,12 @@ #include <stdio.h> -int main(void) { +int main(int argc, char **argv) { FILE *fp; char buf[2]; char *s; - fp = fopen("/etc/passwd", "r"); + fp = fopen(argv[0], "r"); if (!fp) return 1; |