65faa39
commit 01bffc013cdad1e0c45db7aa57efb2bee61f3338
65faa39
Author: Siddhesh Poyarekar <siddhesh@sourceware.org>
65faa39
Date:   Fri Oct 29 14:53:55 2021 +0530
65faa39
65faa39
    Handle NULL input to malloc_usable_size [BZ #28506]
65faa39
    
65faa39
    Hoist the NULL check for malloc_usable_size into its entry points in
65faa39
    malloc-debug and malloc and assume non-NULL in all callees.  This fixes
65faa39
    BZ #28506
65faa39
    
65faa39
    Signed-off-by: Siddhesh Poyarekar <siddhesh@sourceware.org>
65faa39
    Reviewed-by: Florian Weimer <fweimer@redhat.com>
65faa39
    Reviewed-by: Richard W.M. Jones <rjones@redhat.com>
65faa39
    (cherry picked from commit 88e316b06414ee7c944cd6f8b30b07a972b78499)
65faa39
65faa39
diff --git a/malloc/malloc-debug.c b/malloc/malloc-debug.c
65faa39
index 9922ef5f25d2e018..3d7e6d44fdc9e17b 100644
65faa39
--- a/malloc/malloc-debug.c
65faa39
+++ b/malloc/malloc-debug.c
65faa39
@@ -1,5 +1,6 @@
65faa39
 /* Malloc debug DSO.
65faa39
    Copyright (C) 2021 Free Software Foundation, Inc.
65faa39
+   Copyright The GNU Toolchain Authors.
65faa39
    This file is part of the GNU C Library.
65faa39
 
65faa39
    The GNU C Library is free software; you can redistribute it and/or
65faa39
@@ -399,17 +400,17 @@ strong_alias (__debug_calloc, calloc)
65faa39
 size_t
65faa39
 malloc_usable_size (void *mem)
65faa39
 {
65faa39
+  if (mem == NULL)
65faa39
+    return 0;
65faa39
+
65faa39
   if (__is_malloc_debug_enabled (MALLOC_MCHECK_HOOK))
65faa39
     return mcheck_usable_size (mem);
65faa39
   if (__is_malloc_debug_enabled (MALLOC_CHECK_HOOK))
65faa39
     return malloc_check_get_size (mem);
65faa39
 
65faa39
-  if (mem != NULL)
65faa39
-    {
65faa39
-      mchunkptr p = mem2chunk (mem);
65faa39
-     if (DUMPED_MAIN_ARENA_CHUNK (p))
65faa39
-       return chunksize (p) - SIZE_SZ;
65faa39
-    }
65faa39
+  mchunkptr p = mem2chunk (mem);
65faa39
+  if (DUMPED_MAIN_ARENA_CHUNK (p))
65faa39
+    return chunksize (p) - SIZE_SZ;
65faa39
 
65faa39
   return musable (mem);
65faa39
 }
65faa39
diff --git a/malloc/malloc.c b/malloc/malloc.c
65faa39
index e065785af77af72c..7882c70f0a0312d1 100644
65faa39
--- a/malloc/malloc.c
65faa39
+++ b/malloc/malloc.c
65faa39
@@ -1,5 +1,6 @@
65faa39
 /* Malloc implementation for multiple threads without lock contention.
65faa39
    Copyright (C) 1996-2021 Free Software Foundation, Inc.
65faa39
+   Copyright The GNU Toolchain Authors.
65faa39
    This file is part of the GNU C Library.
65faa39
    Contributed by Wolfram Gloger <wg@malloc.de>
65faa39
    and Doug Lea <dl@cs.oswego.edu>, 2001.
65faa39
@@ -5009,20 +5010,13 @@ __malloc_trim (size_t s)
65faa39
 static size_t
65faa39
 musable (void *mem)
65faa39
 {
65faa39
-  mchunkptr p;
65faa39
-  if (mem != 0)
65faa39
-    {
65faa39
-      size_t result = 0;
65faa39
-
65faa39
-      p = mem2chunk (mem);
65faa39
+  mchunkptr p = mem2chunk (mem);
65faa39
 
65faa39
-      if (chunk_is_mmapped (p))
65faa39
-	result = chunksize (p) - CHUNK_HDR_SZ;
65faa39
-      else if (inuse (p))
65faa39
-	result = memsize (p);
65faa39
+  if (chunk_is_mmapped (p))
65faa39
+    return chunksize (p) - CHUNK_HDR_SZ;
65faa39
+  else if (inuse (p))
65faa39
+    return memsize (p);
65faa39
 
65faa39
-      return result;
65faa39
-    }
65faa39
   return 0;
65faa39
 }
65faa39
 
65faa39
@@ -5030,10 +5024,9 @@ musable (void *mem)
65faa39
 size_t
65faa39
 __malloc_usable_size (void *m)
65faa39
 {
65faa39
-  size_t result;
65faa39
-
65faa39
-  result = musable (m);
65faa39
-  return result;
65faa39
+  if (m == NULL)
65faa39
+    return 0;
65faa39
+  return musable (m);
65faa39
 }
65faa39
 #endif
65faa39
 
65faa39
diff --git a/malloc/tst-malloc-usable.c b/malloc/tst-malloc-usable.c
65faa39
index a1074b782a0de96c..b0d702be10ba1610 100644
65faa39
--- a/malloc/tst-malloc-usable.c
65faa39
+++ b/malloc/tst-malloc-usable.c
65faa39
@@ -2,6 +2,7 @@
65faa39
    MALLOC_CHECK_ exported to a positive value.
65faa39
 
65faa39
    Copyright (C) 2012-2021 Free Software Foundation, Inc.
65faa39
+   Copyright The GNU Toolchain Authors.
65faa39
    This file is part of the GNU C Library.
65faa39
 
65faa39
    The GNU C Library is free software; you can redistribute it and/or
65faa39
@@ -21,29 +22,24 @@
65faa39
 #include <malloc.h>
65faa39
 #include <string.h>
65faa39
 #include <stdio.h>
65faa39
+#include <support/support.h>
65faa39
+#include <support/check.h>
65faa39
 
65faa39
 static int
65faa39
 do_test (void)
65faa39
 {
65faa39
   size_t usable_size;
65faa39
   void *p = malloc (7);
65faa39
-  if (!p)
65faa39
-    {
65faa39
-      printf ("memory allocation failed\n");
65faa39
-      return 1;
65faa39
-    }
65faa39
 
65faa39
+  TEST_VERIFY_EXIT (p != NULL);
65faa39
   usable_size = malloc_usable_size (p);
65faa39
-  if (usable_size != 7)
65faa39
-    {
65faa39
-      printf ("malloc_usable_size: expected 7 but got %zu\n", usable_size);
65faa39
-      return 1;
65faa39
-    }
65faa39
-
65faa39
+  TEST_COMPARE (usable_size, 7);
65faa39
   memset (p, 0, usable_size);
65faa39
   free (p);
65faa39
+
65faa39
+  TEST_COMPARE (malloc_usable_size (NULL), 0);
65faa39
+
65faa39
   return 0;
65faa39
 }
65faa39
 
65faa39
-#define TEST_FUNCTION do_test ()
65faa39
-#include "../test-skeleton.c"
65faa39
+#include "support/test-driver.c"