From patchwork Tue Jun 28 11:45:25 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florian Weimer X-Patchwork-Id: 55484 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 7D9B138A8164 for ; Tue, 28 Jun 2022 14:28:29 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 7D9B138A8164 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1656426509; bh=35leEfjRVTfuBntALIpgBGxSWYO6YNgKyZVAr6RAHzw=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=O99GvSjjAgM9LTbkK23cnyPQDmmtohmu0CAG8e5217X0lKWAYizUrflYVNvLpPmMJ P0QLviSbOM9ZQaXRjeqT2/Ne7cq6mit6AwbiThzxpk2jcfW0pzP8w3q6v5ZMrE8cMn W0P65LImK6NmgoMmZNpcIHn9eVwTMXr0kR2MabWI= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 829DD3831C98 for ; Tue, 28 Jun 2022 11:45:29 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 829DD3831C98 Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-474-JeM5SLXYPfe56A_Kj6vWZQ-1; Tue, 28 Jun 2022 07:45:27 -0400 X-MC-Unique: JeM5SLXYPfe56A_Kj6vWZQ-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 9206829ABA0B for ; Tue, 28 Jun 2022 11:45:27 +0000 (UTC) Received: from oldenburg.str.redhat.com (unknown [10.39.193.0]) by smtp.corp.redhat.com (Postfix) with ESMTPS id EA8792026D64 for ; Tue, 28 Jun 2022 11:45:26 +0000 (UTC) To: libc-alpha@sourceware.org Subject: [PATCH] malloc: Simplify checked_request2size interface Date: Tue, 28 Jun 2022 13:45:25 +0200 Message-ID: <871qv9ggfe.fsf@oldenburg.str.redhat.com> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux) MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.78 on 10.11.54.4 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-11.4 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) 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: Florian Weimer via Libc-alpha From: Florian Weimer Reply-To: Florian Weimer Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" In-band signaling avoids an uninitialized variable warning when building with -Og and GCC 12. Tested on i686-linux-gnu and x86_64-linux-gnu. Built with build-many-glibcs.py. Reviewed-by: Siddhesh Poyarekar --- malloc/malloc-check.c | 3 ++- malloc/malloc.c | 30 ++++++++++++++++-------------- 2 files changed, 18 insertions(+), 15 deletions(-) diff --git a/malloc/malloc-check.c b/malloc/malloc-check.c index 0299fe99a7..3368532adf 100644 --- a/malloc/malloc-check.c +++ b/malloc/malloc-check.c @@ -275,7 +275,8 @@ realloc_check (void *oldmem, size_t bytes) malloc_printerr ("realloc(): invalid pointer"); const INTERNAL_SIZE_T oldsize = chunksize (oldp); - if (!checked_request2size (rb, &chnb)) + chnb = checked_request2size (rb); + if (chnb == 0) { __set_errno (ENOMEM); goto invert; diff --git a/malloc/malloc.c b/malloc/malloc.c index 09e5ff2bce..12908b8f97 100644 --- a/malloc/malloc.c +++ b/malloc/malloc.c @@ -1333,15 +1333,15 @@ nextchunk-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ MINSIZE : \ ((req) + SIZE_SZ + MALLOC_ALIGN_MASK) & ~MALLOC_ALIGN_MASK) -/* Check if REQ overflows when padded and aligned and if the resulting value - is less than PTRDIFF_T. Returns TRUE and the requested size or MINSIZE in - case the value is less than MINSIZE on SZ or false if any of the previous - check fail. */ -static inline bool -checked_request2size (size_t req, size_t *sz) __nonnull (1) +/* Check if REQ overflows when padded and aligned and if the resulting + value is less than PTRDIFF_T. Returns the requested size or + MINSIZE in case the value is less than MINSIZE, or 0 if any of the + previous checks fail. */ +static inline size_t +checked_request2size (size_t req) __nonnull (1) { if (__glibc_unlikely (req > PTRDIFF_MAX)) - return false; + return 0; /* When using tagged memory, we cannot share the end of the user block with the header for the next chunk, so ensure that we @@ -1359,8 +1359,7 @@ checked_request2size (size_t req, size_t *sz) __nonnull (1) ~(size_t)(__MTAG_GRANULE_SIZE - 1); } - *sz = request2size (req); - return true; + return request2size (req); } /* @@ -3295,8 +3294,8 @@ __libc_malloc (size_t bytes) ptmalloc_init (); #if USE_TCACHE /* int_free also calls request2size, be careful to not pad twice. */ - size_t tbytes; - if (!checked_request2size (bytes, &tbytes)) + size_t tbytes = checked_request2size (bytes); + if (tbytes == 0) { __set_errno (ENOMEM); return NULL; @@ -3443,7 +3442,8 @@ __libc_realloc (void *oldmem, size_t bytes) || __builtin_expect (misaligned_chunk (oldp), 0))) malloc_printerr ("realloc(): invalid pointer"); - if (!checked_request2size (bytes, &nb)) + nb = checked_request2size (bytes); + if (nb == 0) { __set_errno (ENOMEM); return NULL; @@ -3800,7 +3800,8 @@ _int_malloc (mstate av, size_t bytes) aligned. */ - if (!checked_request2size (bytes, &nb)) + nb = checked_request2size (bytes); + if (nb == 0) { __set_errno (ENOMEM); return NULL; @@ -4952,7 +4953,8 @@ _int_memalign (mstate av, size_t alignment, size_t bytes) - if (!checked_request2size (bytes, &nb)) + nb = checked_request2size (bytes); + if (nb == 0) { __set_errno (ENOMEM); return NULL;