[asan] fix fopen interceptor to not crash if path is NULL
authorKostya Serebryany <kcc@google.com>
Mon, 21 Dec 2015 19:22:26 +0000 (19:22 +0000)
committerKostya Serebryany <kcc@google.com>
Mon, 21 Dec 2015 19:22:26 +0000 (19:22 +0000)
llvm-svn: 256182

compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc
compiler-rt/test/sanitizer_common/TestCases/fopen_nullptr.c [new file with mode: 0644]

index e56f6bf..4639ddc 100644 (file)
@@ -4769,7 +4769,7 @@ INTERCEPTOR(int, __woverflow, __sanitizer_FILE *fp, int ch) {
 INTERCEPTOR(__sanitizer_FILE *, fopen, const char *path, const char *mode) {
   void *ctx;
   COMMON_INTERCEPTOR_ENTER(ctx, fopen, path, mode);
-  COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
+  if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
   COMMON_INTERCEPTOR_READ_RANGE(ctx, mode, REAL(strlen)(mode) + 1);
   __sanitizer_FILE *res = REAL(fopen)(path, mode);
   COMMON_INTERCEPTOR_FILE_OPEN(ctx, res, path);
diff --git a/compiler-rt/test/sanitizer_common/TestCases/fopen_nullptr.c b/compiler-rt/test/sanitizer_common/TestCases/fopen_nullptr.c
new file mode 100644 (file)
index 0000000..960dda3
--- /dev/null
@@ -0,0 +1,6 @@
+// Check that fopen(NULL, "r") is ok.
+// RUN: %clang -O2 %s -o %t && %run %t
+#include <stdio.h>
+const char *fn = NULL;
+FILE *f;
+int main() { f = fopen(fn, "r"); }