[compiler-rt] r319347 - [asan] Don't crash on fclose(NULL)

Kuba Mracek via llvm-commits llvm-commits at lists.llvm.org
Wed Nov 29 11:43:11 PST 2017


Author: kuba.brecka
Date: Wed Nov 29 11:43:11 2017
New Revision: 319347

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


Added:
    compiler-rt/trunk/test/asan/TestCases/Darwin/fclose.c
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=319347&r1=319346&r2=319347&view=diff
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_common_interceptors.inc (original)
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_common_interceptors.inc Wed Nov 29 11:43:11 2017
@@ -273,7 +273,7 @@ UNUSED static const FileMetadata *GetInt
   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;

Added: compiler-rt/trunk/test/asan/TestCases/Darwin/fclose.c
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/asan/TestCases/Darwin/fclose.c?rev=319347&view=auto
==============================================================================
--- compiler-rt/trunk/test/asan/TestCases/Darwin/fclose.c (added)
+++ compiler-rt/trunk/test/asan/TestCases/Darwin/fclose.c Wed Nov 29 11:43:11 2017
@@ -0,0 +1,13 @@
+// RUN: %clang_asan %s -o %t
+// RUN: %run %t 2>&1 | FileCheck %s
+
+#include <stdio.h>
+#include <stdlib.h>
+
+int main(int argc, const char * argv[]) {
+  fclose(NULL);
+  fprintf(stderr, "Finished.\n");
+  return 0;
+}
+
+// CHECK: Finished.




More information about the llvm-commits mailing list