[compiler-rt] r334487 - [ASAN] fix fgets and fgets_fputs tests failure

Peter Wu via llvm-commits llvm-commits at lists.llvm.org
Tue Jun 12 02:27:18 PDT 2018


Author: lekensteyn
Date: Tue Jun 12 02:27:18 2018
New Revision: 334487

URL: http://llvm.org/viewvc/llvm-project?rev=334487&view=rev
Log:
[ASAN] fix fgets and fgets_fputs tests failure

Some systems (Android) might not have /etc/passwd. Fixes r334450.

Modified:
    compiler-rt/trunk/test/asan/TestCases/Posix/fgets_fputs.cc
    compiler-rt/trunk/test/sanitizer_common/TestCases/Posix/fgets.cc

Modified: compiler-rt/trunk/test/asan/TestCases/Posix/fgets_fputs.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/asan/TestCases/Posix/fgets_fputs.cc?rev=334487&r1=334486&r2=334487&view=diff
==============================================================================
--- compiler-rt/trunk/test/asan/TestCases/Posix/fgets_fputs.cc (original)
+++ compiler-rt/trunk/test/asan/TestCases/Posix/fgets_fputs.cc Tue Jun 12 02:27:18 2018
@@ -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}}

Modified: compiler-rt/trunk/test/sanitizer_common/TestCases/Posix/fgets.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/sanitizer_common/TestCases/Posix/fgets.cc?rev=334487&r1=334486&r2=334487&view=diff
==============================================================================
--- compiler-rt/trunk/test/sanitizer_common/TestCases/Posix/fgets.cc (original)
+++ compiler-rt/trunk/test/sanitizer_common/TestCases/Posix/fgets.cc Tue Jun 12 02:27:18 2018
@@ -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;
 




More information about the llvm-commits mailing list