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