Skip to content

Commit 9a050f9

Browse files
committed
[ASan] Support {f}puts(NULL) on Darwin
On Darwin, the man page states that "both fputs() and puts() print `(null)' if str is NULL." rdar://48227136 Reviewed By: Lekensteyn Differential Revision: https://reviews.llvm.org/D64773 llvm-svn: 366342
1 parent aa5cdaf commit 9a050f9

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1241,7 +1241,8 @@ INTERCEPTOR_WITH_SUFFIX(int, fputs, char *s, void *file) {
12411241
// libc file streams can call user-supplied functions, see fopencookie.
12421242
void *ctx;
12431243
COMMON_INTERCEPTOR_ENTER(ctx, fputs, s, file);
1244-
COMMON_INTERCEPTOR_READ_RANGE(ctx, s, REAL(strlen)(s) + 1);
1244+
if (!SANITIZER_MAC || s)
1245+
COMMON_INTERCEPTOR_READ_RANGE(ctx, s, REAL(strlen)(s) + 1);
12451246
return REAL(fputs)(s, file);
12461247
}
12471248
#define INIT_FPUTS COMMON_INTERCEPT_FUNCTION(fputs)
@@ -1254,7 +1255,8 @@ INTERCEPTOR(int, puts, char *s) {
12541255
// libc file streams can call user-supplied functions, see fopencookie.
12551256
void *ctx;
12561257
COMMON_INTERCEPTOR_ENTER(ctx, puts, s);
1257-
COMMON_INTERCEPTOR_READ_RANGE(ctx, s, REAL(strlen)(s) + 1);
1258+
if (!SANITIZER_MAC || s)
1259+
COMMON_INTERCEPTOR_READ_RANGE(ctx, s, REAL(strlen)(s) + 1);
12581260
return REAL(puts)(s);
12591261
}
12601262
#define INIT_PUTS COMMON_INTERCEPT_FUNCTION(puts)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// On Darwin, the man page states that "both fputs() and puts() print `(null)'
2+
// if str is NULL."
3+
//
4+
// RUN: %clangxx -g %s -o %t && %run %t | FileCheck %s
5+
// CHECK: {{^\(null\)---\(null\)$}}
6+
7+
#include <assert.h>
8+
#include <stdio.h>
9+
10+
int main(void) {
11+
assert(fputs(NULL, stdout) >= 0);
12+
fputs("---", stdout);
13+
assert(puts(NULL) >= 0);
14+
15+
return 0;
16+
}

0 commit comments

Comments
 (0)