Skip to content

Commit 7341d0a

Browse files
committed
[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 llvm-svn: 350227
1 parent a3429b3 commit 7341d0a

File tree

6 files changed

+26
-46
lines changed

6 files changed

+26
-46
lines changed

compiler-rt/test/sanitizer_common/TestCases/Posix/devname.cc

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// RUN: %clangxx -O0 -g %s -o %t && %run %t 2>&1 | FileCheck %s
22
// UNSUPPORTED: linux, solaris
33

4+
#include <assert.h>
45
#include <stdio.h>
56
#include <stdlib.h>
67
#include <sys/stat.h>
@@ -9,11 +10,8 @@ int main(void) {
910
struct stat st;
1011
char *name;
1112

12-
if (stat("/dev/null", &st))
13-
exit(1);
14-
15-
if (!(name = devname(st.st_rdev, S_ISCHR(st.st_mode) ? S_IFCHR : S_IFBLK)))
16-
exit(1);
13+
assert(!stat("/dev/null", &st));
14+
assert((name = devname(st.st_rdev, S_ISCHR(st.st_mode) ? S_IFCHR : S_IFBLK)));
1715

1816
printf("%s\n", name);
1917

compiler-rt/test/sanitizer_common/TestCases/Posix/devname_r.cc

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <sys/cdefs.h>
55
#include <sys/stat.h>
66

7+
#include <assert.h>
78
#include <stdio.h>
89
#include <stdlib.h>
910

@@ -12,17 +13,14 @@ int main(void) {
1213
char name[100];
1314
mode_t type;
1415

15-
if (stat("/dev/null", &st))
16-
exit(1);
16+
assert(!stat("/dev/null", &st));
1717

1818
type = S_ISCHR(st.st_mode) ? S_IFCHR : S_IFBLK;
1919

2020
#if defined(__NetBSD__)
21-
if (devname_r(st.st_rdev, type, name, sizeof(name)))
22-
exit(1);
21+
assert(!devname_r(st.st_rdev, type, name, sizeof(name)));
2322
#else
24-
if (!devname_r(st.st_rdev, type, name, sizeof(name)))
25-
exit(1);
23+
assert(devname_r(st.st_rdev, type, name, sizeof(name)));
2624
#endif
2725

2826
printf("%s\n", name);
Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,20 @@
11
// RUN: %clangxx -O0 -g %s -o %t && %run %t
22
// UNSUPPORTED: linux
33

4+
#include <assert.h>
45
#include <stdio.h>
56
#include <stdlib.h>
67

78
int main(void) {
8-
FILE *fp;
9-
size_t len;
10-
char *s;
11-
12-
fp = fopen("/etc/hosts", "r");
13-
if (!fp)
14-
exit(1);
9+
FILE *fp = fopen("/etc/hosts", "r");
10+
assert(fp);
1511

16-
s = fgetln(fp, &len);
12+
size_t len;
13+
char *s = fgetln(fp, &len);
1714

1815
printf("%.*s\n", (int)len, s);
1916

20-
if (fclose(fp) == EOF)
21-
exit(1);
17+
assert(!fclose(fp));
2218

2319
return 0;
2420
}
Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,16 @@
11
// RUN: %clangxx -g %s -o %t && %run %t
22

3+
#include <assert.h>
34
#include <stdio.h>
45

56
int main(int argc, char **argv) {
6-
FILE *fp;
7-
char buf[2];
8-
char *s;
9-
10-
fp = fopen(argv[0], "r");
11-
if (!fp)
12-
return 1;
7+
FILE *fp = fopen(argv[0], "r");
8+
assert(fp);
139

14-
s = fgets(buf, sizeof(buf), fp);
15-
if (!s)
16-
return 2;
10+
char buf[2];
11+
char *s = fgets(buf, sizeof(buf), fp);
12+
assert(s);
1713

18-
fclose(fp);
14+
assert(!fclose(fp));
1915
return 0;
2016
}
Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,12 @@
11
// RUN: %clangxx -g %s -o %t && %run %t | FileCheck %s
22
// CHECK: {{^foobar$}}
33

4+
#include <assert.h>
45
#include <stdio.h>
56

67
int main(void) {
7-
int r;
8-
9-
r = fputs("foo", stdout);
10-
if (r < 0)
11-
return 1;
12-
13-
r = puts("bar");
14-
if (r < 0)
15-
return 1;
8+
assert(fputs("foo", stdout) >= 0);
9+
assert(puts("bar") >= 0);
1610

1711
return 0;
1812
}
Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
// RUN: %clangxx -O0 -g %s -o %t && %run %t
22

3+
#include <assert.h>
34
#include <stdlib.h>
45
#include <sys/stat.h>
56

67
int main(void) {
78
struct stat st;
89

9-
if (lstat("/dev/null", &st))
10-
exit(1);
11-
12-
if (!S_ISCHR(st.st_mode))
13-
exit(1);
10+
assert(!lstat("/dev/null", &st));
11+
assert(S_ISCHR(st.st_mode));
1412

1513
return 0;
1614
}

0 commit comments

Comments
 (0)