malloc_stats(): Fix `unsigned int` overflow

Message ID e32ab95a-39f8-4536-acc9-b9b87ebaa996@nh2.me
State Changes Requested, archived
Headers
Series malloc_stats(): Fix `unsigned int` overflow |

Checks

Context Check Description
dj/TryBot-apply_patch success Patch applied to master at the time it was sent
dj/TryBot-32bit fail Patch series failed to build

Commit Message

Niklas Hambüchen Oct. 29, 2021, 5:11 p.m. UTC
  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 <mail@nh2.me>
---
 malloc/malloc.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
  

Comments

DJ Delorie Oct. 29, 2021, 7:50 p.m. UTC | #1
Fails 32-bit build:

https://www.delorie.com/trybots/32bit/4291/make.tail.txt
  
Niklas Hambüchen Oct. 30, 2021, 12:36 a.m. UTC | #2
The error:

> malloc.c:5169:48: error: format '%lu' expects argument of type 'long unsigned int', but argument 3 has type 'size_t' {aka 'unsigned int'} [-Werror=format=]

glibc doesn't require C99, so I guess "%zu" is not an option, right?

Since this is just in an `fprintf`, would casting to `(unsigned long long)` and using "%llu" be considered acceptable?

Niklas
  
Siddhesh Poyarekar Oct. 30, 2021, 1:39 a.m. UTC | #3
On 10/30/21 06:06, Niklas Hambüchen via Libc-alpha wrote:
> The error:
> 
>> malloc.c:5169:48: error: format '%lu' expects argument of type 'long unsigned int', but argument 3 has type 'size_t' {aka 'unsigned int'} [-Werror=format=]
> 
> glibc doesn't require C99, so I guess "%zu" is not an option, right?

%zu is already being used in the file you're changing, so it's perfectly 
fine to use.

Siddhesh
  
Andreas Schwab Oct. 30, 2021, 6:55 a.m. UTC | #4
On Okt 30 2021, Niklas Hambüchen via Libc-alpha wrote:

> glibc doesn't require C99, so I guess "%zu" is not an option, right?

glibc _implements_ C99, so you can use all its features.

Andreas.
  
Adhemerval Zanella Netto Nov. 1, 2021, 12:34 p.m. UTC | #5
On 29/10/2021 21:36, Niklas Hambüchen via Libc-alpha wrote:
> The error:
> 
>> malloc.c:5169:48: error: format '%lu' expects argument of type 'long unsigned int', but argument 3 has type 'size_t' {aka 'unsigned int'} [-Werror=format=]
> 
> glibc doesn't require C99, so I guess "%zu" is not an option, right?

We build glibc with -std=gnu11, so we do require a compile that at least support
C11.

> 
> Since this is just in an `fprintf`, would casting to `(unsigned long long)` and using "%llu" be considered acceptable?

Either way would be fine.
  

Patch

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;