From patchwork Fri Oct 29 08:55:21 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Siddhesh Poyarekar X-Patchwork-Id: 1547895 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (1024-bit key; secure) header.d=sourceware.org header.i=@sourceware.org header.a=rsa-sha256 header.s=default header.b=yz4t6y6T; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=8.43.85.97; helo=sourceware.org; envelope-from=libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org; receiver=) Received: from sourceware.org (ip-8-43-85-97.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4Hgbqr2KYZz9sRK for ; Fri, 29 Oct 2021 19:55:52 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 82A0B385841E for ; Fri, 29 Oct 2021 08:55:49 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 82A0B385841E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1635497749; bh=XBGMxgGOgpUDvbcx1rfefx18f+XTgwKYRDkbnKmLDtA=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=yz4t6y6Tn7uZyB/s1zl+ySDSJBs/6yYK57BRUZh0WrhtvqGTqrvCzUdnYX0IaRZbl slRfjfZvId1Dykr7+zM4bZIxH07Ex+9FDxxV4es12c2gpqmYO4m6Ql1vpylIjgGnZf h0qIvGYnEEnhszaip49HI1LstorGHwDiwkki3VKA= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from brown.elm.relay.mailchannels.net (brown.elm.relay.mailchannels.net [23.83.212.23]) by sourceware.org (Postfix) with ESMTPS id 706F13858C39 for ; Fri, 29 Oct 2021 08:55:34 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 706F13858C39 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 0CC2C92183C; Fri, 29 Oct 2021 08:55:33 +0000 (UTC) Received: from pdx1-sub0-mail-a31.g.dreamhost.com (100-96-133-198.trex.outbound.svc.cluster.local [100.96.133.198]) (Authenticated sender: dreamhost) by relay.mailchannels.net (Postfix) with ESMTPA id 37CBC9218B1; Fri, 29 Oct 2021 08:55:32 +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.133.198 (trex/6.4.3); Fri, 29 Oct 2021 08:55:32 +0000 X-MC-Relay: Junk X-MailChannels-SenderId: dreamhost|x-authsender|siddhesh@gotplt.org X-MailChannels-Auth-Id: dreamhost X-Abaft-Fearful: 16a930dd5c4f9b39_1635497732705_802470172 X-MC-Loop-Signature: 1635497732705:1602579028 X-MC-Ingress-Time: 1635497732705 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 D89C4852DD; Fri, 29 Oct 2021 01:55:31 -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 A002C7F126; Fri, 29 Oct 2021 01:55:28 -0700 (PDT) X-DH-BACKEND: pdx1-sub0-mail-a31 To: libc-alpha@sourceware.org Subject: [PATCH v2] Handle NULL input to malloc_usable_size [BZ #28506] Date: Fri, 29 Oct 2021 14:25:21 +0530 Message-Id: <20211029085521.2203458-1-siddhesh@sourceware.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <875ytgkzbd.fsf@oldenburg.str.redhat.com> References: <875ytgkzbd.fsf@oldenburg.str.redhat.com> MIME-Version: 1.0 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-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Siddhesh Poyarekar via Libc-alpha From: Siddhesh Poyarekar Reply-To: Siddhesh Poyarekar Cc: fweimer@redhat.com Errors-To: libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org Sender: "Libc-alpha" 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: Richard W.M. Jones --- 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 #include #include +#include +#include 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"