From e73d1f13b62f2e97a879342e5fbc27431e8802c2 Mon Sep 17 00:00:00 2001 From: Kuba Mracek Date: Wed, 29 Nov 2017 19:43:11 +0000 Subject: [PATCH] [asan] Don't crash on fclose(NULL) It's explicitly forbidden to call fclose with NULL, but at least on Darwin, this succeeds and doesn't segfault. To maintain binary compatibility, ASan should survice fclose(NULL) as well. Differential Revision: https://reviews.llvm.org/D40053 llvm-svn: 319347 --- .../sanitizer_common_interceptors.inc | 2 +- compiler-rt/test/asan/TestCases/Darwin/fclose.c | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) create mode 100644 compiler-rt/test/asan/TestCases/Darwin/fclose.c diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc b/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc index 613774c724bf..c8a30bdc61e7 100644 --- a/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc +++ b/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc @@ -273,7 +273,7 @@ UNUSED static const FileMetadata *GetInterceptorMetadata( MetadataHashMap::Handle h(interceptor_metadata_map, (uptr)addr, /* remove */ false, /* create */ false); - if (h.exists()) { + if (addr && h.exists()) { CHECK(!h.created()); CHECK(h->type == CommonInterceptorMetadata::CIMT_FILE); return &h->file; diff --git a/compiler-rt/test/asan/TestCases/Darwin/fclose.c b/compiler-rt/test/asan/TestCases/Darwin/fclose.c new file mode 100644 index 000000000000..7807122bdcc1 --- /dev/null +++ b/compiler-rt/test/asan/TestCases/Darwin/fclose.c @@ -0,0 +1,13 @@ +// RUN: %clang_asan %s -o %t +// RUN: %run %t 2>&1 | FileCheck %s + +#include +#include + +int main(int argc, const char * argv[]) { + fclose(NULL); + fprintf(stderr, "Finished.\n"); + return 0; +} + +// CHECK: Finished.