[compiler-rt] r350227 - [sanitizer_common] Rewrite more Posix tests to use asserts

Michal Gorny via llvm-commits llvm-commits at lists.llvm.org
Wed Jan 2 09:36:46 PST 2019


Author: mgorny
Date: Wed Jan  2 09:36:46 2019
New Revision: 350227

URL: http://llvm.org/viewvc/llvm-project?rev=350227&view=rev
Log:
[sanitizer_common] Rewrite more Posix tests to use asserts

Rewrite the tests for Posix functions that silently 'return 1'
or 'exit(1)' on error, to instead verbosely report the error using
assert.  This is based on requests made in review of D56136.

Differential Revision: https://reviews.llvm.org/D56149

Modified:
    compiler-rt/trunk/test/sanitizer_common/TestCases/Posix/devname.cc
    compiler-rt/trunk/test/sanitizer_common/TestCases/Posix/devname_r.cc
    compiler-rt/trunk/test/sanitizer_common/TestCases/Posix/fgetln.cc
    compiler-rt/trunk/test/sanitizer_common/TestCases/Posix/fgets.cc
    compiler-rt/trunk/test/sanitizer_common/TestCases/Posix/fputs_puts.cc
    compiler-rt/trunk/test/sanitizer_common/TestCases/Posix/lstat.cc

Modified: compiler-rt/trunk/test/sanitizer_common/TestCases/Posix/devname.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/sanitizer_common/TestCases/Posix/devname.cc?rev=350227&r1=350226&r2=350227&view=diff
==============================================================================
--- compiler-rt/trunk/test/sanitizer_common/TestCases/Posix/devname.cc (original)
+++ compiler-rt/trunk/test/sanitizer_common/TestCases/Posix/devname.cc Wed Jan  2 09:36:46 2019
@@ -1,6 +1,7 @@
 // RUN: %clangxx -O0 -g %s -o %t && %run %t 2>&1 | FileCheck %s
 // UNSUPPORTED: linux, solaris
 
+#include <assert.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <sys/stat.h>
@@ -9,11 +10,8 @@ int main(void) {
   struct stat st;
   char *name;
 
-  if (stat("/dev/null", &st))
-    exit(1);
-
-  if (!(name = devname(st.st_rdev, S_ISCHR(st.st_mode) ? S_IFCHR : S_IFBLK)))
-    exit(1);
+  assert(!stat("/dev/null", &st));
+  assert((name = devname(st.st_rdev, S_ISCHR(st.st_mode) ? S_IFCHR : S_IFBLK)));
 
   printf("%s\n", name);
 

Modified: compiler-rt/trunk/test/sanitizer_common/TestCases/Posix/devname_r.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/sanitizer_common/TestCases/Posix/devname_r.cc?rev=350227&r1=350226&r2=350227&view=diff
==============================================================================
--- compiler-rt/trunk/test/sanitizer_common/TestCases/Posix/devname_r.cc (original)
+++ compiler-rt/trunk/test/sanitizer_common/TestCases/Posix/devname_r.cc Wed Jan  2 09:36:46 2019
@@ -4,6 +4,7 @@
 #include <sys/cdefs.h>
 #include <sys/stat.h>
 
+#include <assert.h>
 #include <stdio.h>
 #include <stdlib.h>
 
@@ -12,17 +13,14 @@ int main(void) {
   char name[100];
   mode_t type;
 
-  if (stat("/dev/null", &st))
-    exit(1);
+  assert(!stat("/dev/null", &st));
 
   type = S_ISCHR(st.st_mode) ? S_IFCHR : S_IFBLK;
 
 #if defined(__NetBSD__)
-  if (devname_r(st.st_rdev, type, name, sizeof(name)))
-    exit(1);
+  assert(!devname_r(st.st_rdev, type, name, sizeof(name)));
 #else
-  if (!devname_r(st.st_rdev, type, name, sizeof(name)))
-    exit(1);
+  assert(devname_r(st.st_rdev, type, name, sizeof(name)));
 #endif
 
   printf("%s\n", name);

Modified: compiler-rt/trunk/test/sanitizer_common/TestCases/Posix/fgetln.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/sanitizer_common/TestCases/Posix/fgetln.cc?rev=350227&r1=350226&r2=350227&view=diff
==============================================================================
--- compiler-rt/trunk/test/sanitizer_common/TestCases/Posix/fgetln.cc (original)
+++ compiler-rt/trunk/test/sanitizer_common/TestCases/Posix/fgetln.cc Wed Jan  2 09:36:46 2019
@@ -1,24 +1,20 @@
 // RUN: %clangxx -O0 -g %s -o %t && %run %t
 // UNSUPPORTED: linux
 
+#include <assert.h>
 #include <stdio.h>
 #include <stdlib.h>
 
 int main(void) {
-  FILE *fp;
-  size_t len;
-  char *s;
-
-  fp = fopen("/etc/hosts", "r");
-  if (!fp)
-    exit(1);
+  FILE *fp = fopen("/etc/hosts", "r");
+  assert(fp);
 
-  s = fgetln(fp, &len);
+  size_t len;
+  char *s = fgetln(fp, &len);
 
   printf("%.*s\n", (int)len, s);
 
-  if (fclose(fp) == EOF)
-    exit(1);
+  assert(!fclose(fp));
 
   return 0;
 }

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=350227&r1=350226&r2=350227&view=diff
==============================================================================
--- compiler-rt/trunk/test/sanitizer_common/TestCases/Posix/fgets.cc (original)
+++ compiler-rt/trunk/test/sanitizer_common/TestCases/Posix/fgets.cc Wed Jan  2 09:36:46 2019
@@ -1,20 +1,16 @@
 // RUN: %clangxx -g %s -o %t && %run %t
 
+#include <assert.h>
 #include <stdio.h>
 
 int main(int argc, char **argv) {
-  FILE *fp;
-  char buf[2];
-  char *s;
-
-  fp = fopen(argv[0], "r");
-  if (!fp)
-    return 1;
+  FILE *fp = fopen(argv[0], "r");
+  assert(fp);
 
-  s = fgets(buf, sizeof(buf), fp);
-  if (!s)
-    return 2;
+  char buf[2];
+  char *s = fgets(buf, sizeof(buf), fp);
+  assert(s);
 
-  fclose(fp);
+  assert(!fclose(fp));
   return 0;
 }

Modified: compiler-rt/trunk/test/sanitizer_common/TestCases/Posix/fputs_puts.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/sanitizer_common/TestCases/Posix/fputs_puts.cc?rev=350227&r1=350226&r2=350227&view=diff
==============================================================================
--- compiler-rt/trunk/test/sanitizer_common/TestCases/Posix/fputs_puts.cc (original)
+++ compiler-rt/trunk/test/sanitizer_common/TestCases/Posix/fputs_puts.cc Wed Jan  2 09:36:46 2019
@@ -1,18 +1,12 @@
 // RUN: %clangxx -g %s -o %t && %run %t | FileCheck %s
 // CHECK: {{^foobar$}}
 
+#include <assert.h>
 #include <stdio.h>
 
 int main(void) {
-  int r;
-
-  r = fputs("foo", stdout);
-  if (r < 0)
-    return 1;
-
-  r = puts("bar");
-  if (r < 0)
-    return 1;
+  assert(fputs("foo", stdout) >= 0);
+  assert(puts("bar") >= 0);
 
   return 0;
 }

Modified: compiler-rt/trunk/test/sanitizer_common/TestCases/Posix/lstat.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/sanitizer_common/TestCases/Posix/lstat.cc?rev=350227&r1=350226&r2=350227&view=diff
==============================================================================
--- compiler-rt/trunk/test/sanitizer_common/TestCases/Posix/lstat.cc (original)
+++ compiler-rt/trunk/test/sanitizer_common/TestCases/Posix/lstat.cc Wed Jan  2 09:36:46 2019
@@ -1,16 +1,14 @@
 // RUN: %clangxx -O0 -g %s -o %t && %run %t
 
+#include <assert.h>
 #include <stdlib.h>
 #include <sys/stat.h>
 
 int main(void) {
   struct stat st;
 
-  if (lstat("/dev/null", &st))
-    exit(1);
-
-  if (!S_ISCHR(st.st_mode))
-    exit(1);
+  assert(!lstat("/dev/null", &st));
+  assert(S_ISCHR(st.st_mode));
 
   return 0;
 }




More information about the llvm-commits mailing list