Bug 1396361 - Avoid crashing when some system library calls malloc_zone_free(zone, NULL). r?njn draft
authorMike Hommey <mh+mozilla@glandium.org>
Mon, 04 Sep 2017 07:32:42 +0900
changeset 658342 f1f549d125701d09cc0c70388c5b5a09dda01f6b
parent 657984 a46a5879b8781ae9ea99f37b5d34a891f0f75047
child 729612 602a1e1e5ba0f4ed2b7b0839afbefb180517c03a
push id77726
push userbmo:mh+mozilla@glandium.org
push dateSun, 03 Sep 2017 22:36:42 +0000
reviewersnjn
bugs1396361
milestone57.0a1
Bug 1396361 - Avoid crashing when some system library calls malloc_zone_free(zone, NULL). r?njn Some system libraries call malloc_zone_free directly instead of free, and sometimes they do that with the wrong zone. When that happens, we circle back, trying to find the right zone, and call malloc_zone_free with the right one, but when we can't find one, we crash, which matches what the system free() would do. Except in one case where the pointer we're being passed is NULL, in which case we can't trace it back to any zone, but shouldn't crash (system free() explicitly doesn't crash in that case).
memory/build/zone.c
--- a/memory/build/zone.c
+++ b/memory/build/zone.c
@@ -148,16 +148,19 @@ zone_realloc(malloc_zone_t *zone, void *
 static void
 other_zone_free(malloc_zone_t* original_zone, void* ptr)
 {
   // Sometimes, system libraries call malloc_zone_* functions with the wrong
   // zone (e.g. CoreFoundation does). In that case, we need to find the real
   // one. We can't call libSystem's free directly because we're exporting
   // free from libmozglue and we'd pick that one, so we manually find the
   // right zone and free with it.
+  if (!ptr) {
+    return;
+  }
   malloc_zone_t* zone = malloc_zone_from_ptr(ptr);
   // The system allocator crashes voluntarily by default when a pointer can't
   // be traced back to a zone. Do the same.
   MOZ_RELEASE_ASSERT(zone);
   MOZ_RELEASE_ASSERT(zone != original_zone);
   return malloc_zone_free(zone, ptr);
 }