From patchwork Fri Oct 29 17:11:34 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Niklas_Hamb=C3=BCchen?= X-Patchwork-Id: 46794 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 A6C04385780B for ; Fri, 29 Oct 2021 17:11:59 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org A6C04385780B DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1635527519; bh=SCZzy4GhvHh/84kO8FuVE2w08qUGg3qYhPx0v3OjjqE=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=dHkmc2LSapTvrJSlew20aDKJx0RID0EAO3IZjuJAwoNwlP3LXQ/bK6qXvVdLvEQjJ QSuRkPX93rNBlfH5WBKKHsCWS24WgQR+8Bf9P7c+3A3bMxWJ1Bk9TyKPTH3NKPj1si yl67JDE/WlXK+apEOxwavXTBV8EgDT4mjos/VYVI= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail.nh2.me (mail.nh2.me [IPv6:2a01:4f8:1c0c:8227::1]) by sourceware.org (Postfix) with ESMTPS id 153553858435 for ; Fri, 29 Oct 2021 17:11:37 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 153553858435 To: libc-alpha@sourceware.org Subject: [PATCH] malloc_stats(): Fix `unsigned int` overflow Message-ID: Date: Fri, 29 Oct 2021 19:11:34 +0200 MIME-Version: 1.0 Content-Language: en-US X-Spam-Status: No, score=-13.4 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_INFOUSMEBIZ, SPF_HELO_PASS, SPF_PASS 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: =?utf-8?q?Niklas_Hamb=C3=BCchen_via_Libc-alpha?= From: =?utf-8?q?Niklas_Hamb=C3=BCchen?= Reply-To: =?utf-8?q?Niklas_Hamb=C3=BCchen?= Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" Fixes malloc_stats() returning vastly wrong information for programs that use more than 2 GiB memory. `man mallinfo` documents that it uses `int` and wraps around, but `man malloc_stats` does not, and should not. Fixes https://sourceware.org/bugzilla/show_bug.cgi?id=21556 Signed-off-by: Niklas Hambüchen --- malloc/malloc.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/malloc/malloc.c b/malloc/malloc.c index 095d97a3be..cdc9aff9d5 100644 --- a/malloc/malloc.c +++ b/malloc/malloc.c @@ -5151,7 +5151,7 @@ __malloc_stats (void) { int i; mstate ar_ptr; - unsigned int in_use_b = mp_.mmapped_mem, system_b = in_use_b; + size_t in_use_b = mp_.mmapped_mem, system_b = in_use_b; if (!__malloc_initialized) ptmalloc_init (); @@ -5166,8 +5166,8 @@ __malloc_stats (void) __libc_lock_lock (ar_ptr->mutex); int_mallinfo (ar_ptr, &mi); fprintf (stderr, "Arena %d:\n", i); - fprintf (stderr, "system bytes = %10u\n", (unsigned int) mi.arena); - fprintf (stderr, "in use bytes = %10u\n", (unsigned int) mi.uordblks); + fprintf (stderr, "system bytes = %10lu\n", mi.arena); + fprintf (stderr, "in use bytes = %10lu\n", mi.uordblks); #if MALLOC_DEBUG > 1 if (i > 0) dump_heap (heap_for_ptr (top (ar_ptr))); @@ -5180,9 +5180,9 @@ __malloc_stats (void) break; } fprintf (stderr, "Total (incl. mmap):\n"); - fprintf (stderr, "system bytes = %10u\n", system_b); - fprintf (stderr, "in use bytes = %10u\n", in_use_b); - fprintf (stderr, "max mmap regions = %10u\n", (unsigned int) mp_.max_n_mmaps); + fprintf (stderr, "system bytes = %10lu\n", system_b); + fprintf (stderr, "in use bytes = %10lu\n", in_use_b); + fprintf (stderr, "max mmap regions = %10d\n", mp_.max_n_mmaps); fprintf (stderr, "max mmap bytes = %10lu\n", (unsigned long) mp_.max_mmapped_mem); stderr->_flags2 = old_flags2;