diff options
author | Peter Wu <peter@lekensteyn.nl> | 2018-06-12 09:27:18 +0000 |
---|---|---|
committer | Peter Wu <peter@lekensteyn.nl> | 2018-06-12 09:27:18 +0000 |
commit | c7ce5b706fea58c929b9fa6e6fc9239dd0c4e74c (patch) | |
tree | c7f4f036574bbb53258e620f329f75e1c0464f3b /test/asan | |
parent | 31049741a2a2c58ebe04fc2e13b62ae8398945f4 (diff) |
[ASAN] fix fgets and fgets_fputs tests failure
Some systems (Android) might not have /etc/passwd. Fixes r334450.
git-svn-id: https://llvm.org/svn/llvm-project/compiler-rt/trunk@334487 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'test/asan')
-rw-r--r-- | test/asan/TestCases/Posix/fgets_fputs.cc | 40 |
1 files changed, 25 insertions, 15 deletions
diff --git a/test/asan/TestCases/Posix/fgets_fputs.cc b/test/asan/TestCases/Posix/fgets_fputs.cc index 6f0696393..a126c78c7 100644 --- a/test/asan/TestCases/Posix/fgets_fputs.cc +++ b/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}} |