From 4c0cdec1384fb35e1387d29e78bc2baadaf8f44f Mon Sep 17 00:00:00 2001 From: Kuba Brecka Date: Tue, 21 Jul 2015 14:23:27 +0000 Subject: [PATCH] [asan] Fix the freopen interceptor to allow NULL instead of a filename According to man freopen, passing NULL instead of a filename is valid, however the current implementation of the interceptor assumes this parameter is non-NULL. Let's fix that and add a test case. Differential Revision: http://reviews.llvm.org/D11389 llvm-svn: 242787 --- .../sanitizer_common_interceptors.inc | 4 ++-- compiler-rt/test/asan/TestCases/Posix/freopen.cc | 12 ++++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) create mode 100644 compiler-rt/test/asan/TestCases/Posix/freopen.cc diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc b/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc index c7dc47532e2d..6ff0ae1b231e 100644 --- a/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc +++ b/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc @@ -4671,7 +4671,7 @@ INTERCEPTOR(__sanitizer_FILE *, freopen, const char *path, const char *mode, __sanitizer_FILE *fp) { void *ctx; COMMON_INTERCEPTOR_ENTER(ctx, freopen, path, mode, fp); - 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); COMMON_INTERCEPTOR_FILE_CLOSE(ctx, fp); __sanitizer_FILE *res = REAL(freopen)(path, mode, fp); @@ -4702,7 +4702,7 @@ INTERCEPTOR(__sanitizer_FILE *, freopen64, const char *path, const char *mode, __sanitizer_FILE *fp) { void *ctx; COMMON_INTERCEPTOR_ENTER(ctx, freopen64, path, mode, fp); - 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); COMMON_INTERCEPTOR_FILE_CLOSE(ctx, fp); __sanitizer_FILE *res = REAL(freopen64)(path, mode, fp); diff --git a/compiler-rt/test/asan/TestCases/Posix/freopen.cc b/compiler-rt/test/asan/TestCases/Posix/freopen.cc new file mode 100644 index 000000000000..d2cd853387fb --- /dev/null +++ b/compiler-rt/test/asan/TestCases/Posix/freopen.cc @@ -0,0 +1,12 @@ +// RUN: %clangxx_asan -O0 %s -o %t && %run %t + +#include +#include + +int main() { + FILE *fp = fopen("/dev/null", "w"); + assert(fp); + freopen(NULL, "a", fp); + fclose(fp); + return 0; +}