From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from brown.birch.relay.mailchannels.net (brown.birch.relay.mailchannels.net [23.83.209.23]) by sourceware.org (Postfix) with ESMTPS id 920CA3858416 for ; Fri, 29 Oct 2021 09:29:24 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 920CA3858416 X-Sender-Id: dreamhost|x-authsender|siddhesh@gotplt.org Received: from relay.mailchannels.net (localhost [127.0.0.1]) by relay.mailchannels.net (Postfix) with ESMTP id D37CC3422E5; Fri, 29 Oct 2021 09:29:22 +0000 (UTC) Received: from pdx1-sub0-mail-a31.g.dreamhost.com (100-96-17-28.trex.outbound.svc.cluster.local [100.96.17.28]) (Authenticated sender: dreamhost) by relay.mailchannels.net (Postfix) with ESMTPA id E1D63342669; Fri, 29 Oct 2021 09:29:21 +0000 (UTC) X-Sender-Id: dreamhost|x-authsender|siddhesh@gotplt.org Received: from pdx1-sub0-mail-a31.g.dreamhost.com (pop.dreamhost.com [64.90.62.162]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384) by 100.96.17.28 (trex/6.4.3); Fri, 29 Oct 2021 09:29:22 +0000 X-MC-Relay: Junk X-MailChannels-SenderId: dreamhost|x-authsender|siddhesh@gotplt.org X-MailChannels-Auth-Id: dreamhost X-Gusty-Name: 1a02623a6517fc8b_1635499762684_1434076870 X-MC-Loop-Signature: 1635499762684:692409262 X-MC-Ingress-Time: 1635499762684 Received: from pdx1-sub0-mail-a31.g.dreamhost.com (localhost [127.0.0.1]) by pdx1-sub0-mail-a31.g.dreamhost.com (Postfix) with ESMTP id 99AB47F126; Fri, 29 Oct 2021 02:29:21 -0700 (PDT) Received: from rhbox.redhat.com (unknown [1.186.223.58]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) (Authenticated sender: siddhesh@gotplt.org) by pdx1-sub0-mail-a31.g.dreamhost.com (Postfix) with ESMTPSA id D48B77F15A; Fri, 29 Oct 2021 02:29:17 -0700 (PDT) X-DH-BACKEND: pdx1-sub0-mail-a31 From: Siddhesh Poyarekar To: libc-stable@sourceware.org Cc: Florian Weimer , "Richard W . M . Jones" Subject: [COMMITTED 2.34] Handle NULL input to malloc_usable_size [BZ #28506] Date: Fri, 29 Oct 2021 14:59:02 +0530 Message-Id: <20211029092902.2216410-1-siddhesh@sourceware.org> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-3493.9 required=5.0 tests=BAYES_00, GIT_PATCH_0, JMQ_SPF_NEUTRAL, KAM_DMARC_NONE, KAM_DMARC_STATUS, RCVD_IN_ABUSEAT, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NEUTRAL, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: libc-stable@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-stable mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 29 Oct 2021 09:29:26 -0000 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 Reviewed-by: Florian Weimer Reviewed-by: Richard W.M. Jones (cherry picked from commit 88e316b06414ee7c944cd6f8b30b07a972b78499) --- malloc/malloc-debug.c | 13 +++++++------ malloc/malloc.c | 25 +++++++++---------------- malloc/tst-malloc-usable.c | 22 +++++++++------------- 3 files changed, 25 insertions(+), 35 deletions(-) diff --git a/malloc/malloc-debug.c b/malloc/malloc-debug.c index 9922ef5f25..3d7e6d44fd 100644 --- a/malloc/malloc-debug.c +++ b/malloc/malloc-debug.c @@ -1,5 +1,6 @@ /* Malloc debug DSO. Copyright (C) 2021 Free Software Foundation, Inc. + Copyright The GNU Toolchain Authors. This file is part of the GNU C Library. =20 The GNU C Library is free software; you can redistribute it and/or @@ -399,17 +400,17 @@ strong_alias (__debug_calloc, calloc) size_t malloc_usable_size (void *mem) { + if (mem =3D=3D 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); =20 - if (mem !=3D NULL) - { - mchunkptr p =3D mem2chunk (mem); - if (DUMPED_MAIN_ARENA_CHUNK (p)) - return chunksize (p) - SIZE_SZ; - } + mchunkptr p =3D mem2chunk (mem); + if (DUMPED_MAIN_ARENA_CHUNK (p)) + return chunksize (p) - SIZE_SZ; =20 return musable (mem); } diff --git a/malloc/malloc.c b/malloc/malloc.c index e065785af7..7882c70f0a 100644 --- a/malloc/malloc.c +++ b/malloc/malloc.c @@ -1,5 +1,6 @@ /* Malloc implementation for multiple threads without lock contention. Copyright (C) 1996-2021 Free Software Foundation, Inc. + Copyright The GNU Toolchain Authors. This file is part of the GNU C Library. Contributed by Wolfram Gloger and Doug Lea , 2001. @@ -5009,20 +5010,13 @@ __malloc_trim (size_t s) static size_t musable (void *mem) { - mchunkptr p; - if (mem !=3D 0) - { - size_t result =3D 0; - - p =3D mem2chunk (mem); + mchunkptr p =3D mem2chunk (mem); =20 - if (chunk_is_mmapped (p)) - result =3D chunksize (p) - CHUNK_HDR_SZ; - else if (inuse (p)) - result =3D memsize (p); + if (chunk_is_mmapped (p)) + return chunksize (p) - CHUNK_HDR_SZ; + else if (inuse (p)) + return memsize (p); =20 - return result; - } return 0; } =20 @@ -5030,10 +5024,9 @@ musable (void *mem) size_t __malloc_usable_size (void *m) { - size_t result; - - result =3D musable (m); - return result; + if (m =3D=3D NULL) + return 0; + return musable (m); } #endif =20 diff --git a/malloc/tst-malloc-usable.c b/malloc/tst-malloc-usable.c index a1074b782a..b0d702be10 100644 --- a/malloc/tst-malloc-usable.c +++ b/malloc/tst-malloc-usable.c @@ -2,6 +2,7 @@ MALLOC_CHECK_ exported to a positive value. =20 Copyright (C) 2012-2021 Free Software Foundation, Inc. + Copyright The GNU Toolchain Authors. This file is part of the GNU C Library. =20 The GNU C Library is free software; you can redistribute it and/or @@ -21,29 +22,24 @@ #include #include #include +#include +#include =20 static int do_test (void) { size_t usable_size; void *p =3D malloc (7); - if (!p) - { - printf ("memory allocation failed\n"); - return 1; - } =20 + TEST_VERIFY_EXIT (p !=3D NULL); usable_size =3D malloc_usable_size (p); - if (usable_size !=3D 7) - { - printf ("malloc_usable_size: expected 7 but got %zu\n", usable_siz= e); - return 1; - } - + TEST_COMPARE (usable_size, 7); memset (p, 0, usable_size); free (p); + + TEST_COMPARE (malloc_usable_size (NULL), 0); + return 0; } =20 -#define TEST_FUNCTION do_test () -#include "../test-skeleton.c" +#include "support/test-driver.c" --=20 2.31.1