From: "Richard W.M. Jones" <rjones@redhat.com>
To: Siddhesh Poyarekar <siddhesh@sourceware.org>
Cc: libc-alpha@sourceware.org, fweimer@redhat.com
Subject: Re: [PATCH v2] Handle NULL input to malloc_usable_size [BZ #28506]
Date: Fri, 29 Oct 2021 10:00:02 +0100 [thread overview]
Message-ID: <20211029090002.GQ3361@redhat.com> (raw)
In-Reply-To: <20211029085521.2203458-1-siddhesh@sourceware.org>
On Fri, Oct 29, 2021 at 02:25:21PM +0530, Siddhesh Poyarekar wrote:
> Hoist the NULL check for malloc_usable_size into its entry points in
> malloc-debug and malloc and assume non-NULL in all callees. This fixes
> BZ #28506
>
> Signed-off-by: Siddhesh Poyarekar <siddhesh@sourceware.org>
> ---
> malloc/malloc-debug.c | 12 ++++++------
> malloc/malloc.c | 24 ++++++++----------------
> malloc/tst-malloc-usable.c | 21 ++++++++-------------
> 3 files changed, 22 insertions(+), 35 deletions(-)
>
> diff --git a/malloc/malloc-debug.c b/malloc/malloc-debug.c
> index 9922ef5f25..5e954d7dc2 100644
> --- a/malloc/malloc-debug.c
> +++ b/malloc/malloc-debug.c
> @@ -399,17 +399,17 @@ strong_alias (__debug_calloc, calloc)
> size_t
> malloc_usable_size (void *mem)
> {
> + if (mem == NULL)
> + return 0;
> +
> if (__is_malloc_debug_enabled (MALLOC_MCHECK_HOOK))
> return mcheck_usable_size (mem);
> if (__is_malloc_debug_enabled (MALLOC_CHECK_HOOK))
> return malloc_check_get_size (mem);
>
> - if (mem != NULL)
> - {
> - mchunkptr p = mem2chunk (mem);
> - if (DUMPED_MAIN_ARENA_CHUNK (p))
> - return chunksize (p) - SIZE_SZ;
> - }
> + mchunkptr p = mem2chunk (mem);
> + if (DUMPED_MAIN_ARENA_CHUNK (p))
> + return chunksize (p) - SIZE_SZ;
>
> return musable (mem);
> }
> diff --git a/malloc/malloc.c b/malloc/malloc.c
> index 2ba1fee144..a9dfc82788 100644
> --- a/malloc/malloc.c
> +++ b/malloc/malloc.c
> @@ -5007,20 +5007,13 @@ __malloc_trim (size_t s)
> static size_t
> musable (void *mem)
> {
> - mchunkptr p;
> - if (mem != 0)
> - {
> - size_t result = 0;
> -
> - p = mem2chunk (mem);
> + mchunkptr p = mem2chunk (mem);
>
> - if (chunk_is_mmapped (p))
> - result = chunksize (p) - CHUNK_HDR_SZ;
> - else if (inuse (p))
> - result = memsize (p);
> + if (chunk_is_mmapped (p))
> + return chunksize (p) - CHUNK_HDR_SZ;
> + else if (inuse (p))
> + return memsize (p);
>
> - return result;
> - }
> return 0;
> }
>
> @@ -5028,10 +5021,9 @@ musable (void *mem)
> size_t
> __malloc_usable_size (void *m)
> {
> - size_t result;
> -
> - result = musable (m);
> - return result;
> + if (m == NULL)
> + return 0;
> + return musable (m);
> }
> #endif
>
> diff --git a/malloc/tst-malloc-usable.c b/malloc/tst-malloc-usable.c
> index a1074b782a..e50cadcf10 100644
> --- a/malloc/tst-malloc-usable.c
> +++ b/malloc/tst-malloc-usable.c
> @@ -21,29 +21,24 @@
> #include <malloc.h>
> #include <string.h>
> #include <stdio.h>
> +#include <support/support.h>
> +#include <support/check.h>
>
> static int
> do_test (void)
> {
> size_t usable_size;
> void *p = malloc (7);
> - if (!p)
> - {
> - printf ("memory allocation failed\n");
> - return 1;
> - }
>
> + TEST_VERIFY_EXIT (p != NULL);
> usable_size = malloc_usable_size (p);
> - if (usable_size != 7)
> - {
> - printf ("malloc_usable_size: expected 7 but got %zu\n", usable_size);
> - return 1;
> - }
> -
> + TEST_COMPARE (usable_size, 7);
> memset (p, 0, usable_size);
> free (p);
> +
> + TEST_COMPARE (malloc_usable_size (NULL), 0);
> +
> return 0;
> }
>
> -#define TEST_FUNCTION do_test ()
> -#include "../test-skeleton.c"
> +#include "support/test-driver.c"
Also looks sensible, so:
Reviewed-by: Richard W.M. Jones <rjones@redhat.com>
Rich.
--
Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones
Read my programming and virtualization blog: http://rwmj.wordpress.com
virt-builder quickly builds VMs from scratch
http://libguestfs.org/virt-builder.1.html
next prev parent reply other threads:[~2021-10-29 9:00 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-10-29 3:18 [PATCH] " Siddhesh Poyarekar
2021-10-29 8:30 ` Florian Weimer
2021-10-29 8:55 ` [PATCH v2] " Siddhesh Poyarekar
2021-10-29 9:00 ` Richard W.M. Jones [this message]
2021-10-29 9:06 ` Florian Weimer
2021-10-29 9:25 ` [COMMITTED] " Siddhesh Poyarekar
2021-10-29 9:27 ` Florian Weimer
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20211029090002.GQ3361@redhat.com \
--to=rjones@redhat.com \
--cc=fweimer@redhat.com \
--cc=libc-alpha@sourceware.org \
--cc=siddhesh@sourceware.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).