From 9a050f92bb29000a1a961f7c9ca80d7ec13dc310 Mon Sep 17 00:00:00 2001 From: Julian Lettner Date: Wed, 17 Jul 2019 16:09:25 +0000 Subject: [PATCH] [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 --- .../sanitizer_common/sanitizer_common_interceptors.inc | 6 ++++-- .../sanitizer_common/TestCases/Darwin/fputs_puts_null.cc | 16 ++++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) create mode 100644 compiler-rt/test/sanitizer_common/TestCases/Darwin/fputs_puts_null.cc diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc b/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc index 5b68c01..9f5a91a 100644 --- a/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc +++ b/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc @@ -1241,7 +1241,8 @@ INTERCEPTOR_WITH_SUFFIX(int, fputs, char *s, void *file) { // libc file streams can call user-supplied functions, see fopencookie. void *ctx; COMMON_INTERCEPTOR_ENTER(ctx, fputs, s, file); - COMMON_INTERCEPTOR_READ_RANGE(ctx, s, REAL(strlen)(s) + 1); + if (!SANITIZER_MAC || s) + COMMON_INTERCEPTOR_READ_RANGE(ctx, s, REAL(strlen)(s) + 1); return REAL(fputs)(s, file); } #define INIT_FPUTS COMMON_INTERCEPT_FUNCTION(fputs) @@ -1254,7 +1255,8 @@ INTERCEPTOR(int, puts, char *s) { // libc file streams can call user-supplied functions, see fopencookie. void *ctx; COMMON_INTERCEPTOR_ENTER(ctx, puts, s); - COMMON_INTERCEPTOR_READ_RANGE(ctx, s, REAL(strlen)(s) + 1); + if (!SANITIZER_MAC || s) + COMMON_INTERCEPTOR_READ_RANGE(ctx, s, REAL(strlen)(s) + 1); return REAL(puts)(s); } #define INIT_PUTS COMMON_INTERCEPT_FUNCTION(puts) diff --git a/compiler-rt/test/sanitizer_common/TestCases/Darwin/fputs_puts_null.cc b/compiler-rt/test/sanitizer_common/TestCases/Darwin/fputs_puts_null.cc new file mode 100644 index 0000000..705ca2e --- /dev/null +++ b/compiler-rt/test/sanitizer_common/TestCases/Darwin/fputs_puts_null.cc @@ -0,0 +1,16 @@ +// On Darwin, the man page states that "both fputs() and puts() print `(null)' +// if str is NULL." +// +// RUN: %clangxx -g %s -o %t && %run %t | FileCheck %s +// CHECK: {{^\(null\)---\(null\)$}} + +#include +#include + +int main(void) { + assert(fputs(NULL, stdout) >= 0); + fputs("---", stdout); + assert(puts(NULL) >= 0); + + return 0; +} -- 2.7.4