[compiler-rt] r366342 - [ASan] Support `{f}puts(NULL)` on Darwin

Julian Lettner via llvm-commits llvm-commits at lists.llvm.org
Wed Jul 17 09:09:25 PDT 2019


Author: yln
Date: Wed Jul 17 09:09:25 2019
New Revision: 366342

URL: http://llvm.org/viewvc/llvm-project?rev=366342&view=rev
Log:
[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

Added:
    compiler-rt/trunk/test/sanitizer_common/TestCases/Darwin/fputs_puts_null.cc
Modified:
    compiler-rt/trunk/lib/sanitizer_common/sanitizer_common_interceptors.inc

Modified: compiler-rt/trunk/lib/sanitizer_common/sanitizer_common_interceptors.inc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_common_interceptors.inc?rev=366342&r1=366341&r2=366342&view=diff
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_common_interceptors.inc (original)
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_common_interceptors.inc Wed Jul 17 09:09:25 2019
@@ -1241,7 +1241,8 @@ INTERCEPTOR_WITH_SUFFIX(int, fputs, char
   // 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)

Added: compiler-rt/trunk/test/sanitizer_common/TestCases/Darwin/fputs_puts_null.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/sanitizer_common/TestCases/Darwin/fputs_puts_null.cc?rev=366342&view=auto
==============================================================================
--- compiler-rt/trunk/test/sanitizer_common/TestCases/Darwin/fputs_puts_null.cc (added)
+++ compiler-rt/trunk/test/sanitizer_common/TestCases/Darwin/fputs_puts_null.cc Wed Jul 17 09:09:25 2019
@@ -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 <assert.h>
+#include <stdio.h>
+
+int main(void) {
+  assert(fputs(NULL, stdout) >= 0);
+  fputs("---", stdout);
+  assert(puts(NULL) >= 0);
+
+  return 0;
+}




More information about the llvm-commits mailing list