asan: fix crash in strdup on malloc failure
authorDmitry Vyukov <dvyukov@google.com>
Mon, 20 Feb 2023 10:58:15 +0000 (11:58 +0100)
committerDmitry Vyukov <dvyukov@google.com>
Mon, 20 Feb 2023 14:34:33 +0000 (15:34 +0100)
There are some programs that try to handle all malloc failures.
Let's allow testing of such programs.

Reviewed By: melver

Differential Revision: https://reviews.llvm.org/D144374

compiler-rt/lib/asan/asan_interceptors.cpp

index 8170082..a4084c8 100644 (file)
@@ -453,7 +453,9 @@ INTERCEPTOR(char*, strdup, const char *s) {
   }
   GET_STACK_TRACE_MALLOC;
   void *new_mem = asan_malloc(length + 1, &stack);
-  REAL(memcpy)(new_mem, s, length + 1);
+  if (new_mem) {
+    REAL(memcpy)(new_mem, s, length + 1);
+  }
   return reinterpret_cast<char*>(new_mem);
 }
 
@@ -469,7 +471,9 @@ INTERCEPTOR(char*, __strdup, const char *s) {
   }
   GET_STACK_TRACE_MALLOC;
   void *new_mem = asan_malloc(length + 1, &stack);
-  REAL(memcpy)(new_mem, s, length + 1);
+  if (new_mem) {
+    REAL(memcpy)(new_mem, s, length + 1);
+  }
   return reinterpret_cast<char*>(new_mem);
 }
 #endif // ASAN_INTERCEPT___STRDUP