Hello,

On Wed, 08 Nov 2023 20:17:49 +0100 =?utf-8?q?Andr=C3=A9_Offringa?= 
<offri...@gmail.com> wrote:

$ gmemusage
realloc(): invalid next size
Aborted

Looks like caused by having not enough space
for process names longer than 13 characters.

A package built with the modification below
shows no longer this crash.

Kind regard,
Bernhard




(gdb) bt
#0  __pthread_kill_implementation (threadid=<optimized out>, 
signo=signo@entry=6, no_tid=no_tid@entry=0) at ./nptl/pthread_kill.c:44
#1  0x00007f43d244b15f in __pthread_kill_internal (signo=6, threadid=<optimized 
out>) at ./nptl/pthread_kill.c:78
#2  0x00007f43d23fd472 in __GI_raise (sig=sig@entry=6) at 
../sysdeps/posix/raise.c:26
#3  0x00007f43d23e74b2 in __GI_abort () at ./stdlib/abort.c:79
#4  0x00007f43d23e81ed in __libc_message (fmt=fmt@entry=0x7f43d255a78c "%s\n") 
at ../sysdeps/posix/libc_fatal.c:150
#5  0x00007f43d2454a75 in malloc_printerr (str=str@entry=0x7f43d2558326 "realloc(): 
invalid next size") at ./malloc/malloc.c:5658
#6  0x00007f43d245876c in _int_realloc (av=av@entry=0x7f43d2594c80 
<main_arena>, oldp=oldp@entry=0x564149c00680, oldsize=oldsize@entry=912, 
nb=nb@entry=1808) at ./malloc/malloc.c:4836
#7  0x00007f43d2459596 in __GI___libc_realloc 
(oldmem=oldmem@entry=0x564149c00690, bytes=bytes@entry=1792) at 
./malloc/malloc.c:3477
#8  0x0000564148a039a5 in addProc (procname=procname@entry=0x7fffa8056900 
"cpuhp/3", mem=0, rss=0) at hash.c:89
#9  0x0000564148a03f20 in makeProcs () at proc.c:215
#10 0x0000564148a02e32 in draw_window () at gmemusage.c:489
#11 0x0000564148a02b85 in main (argc=<optimized out>, argv=<optimized out>) at 
gmemusage.c:300



benutzer@debian:~$ valgrind gmemusage
==1246== Memcheck, a memory error detector
==1246== Copyright (C) 2002-2022, and GNU GPL'd, by Julian Seward et al.
==1246== Using Valgrind-3.19.0 and LibVEX; rerun with -h for copyright info
==1246== Command: gmemusage
==1246==
==1246== Invalid write of size 1
==1246==    at 0x48468E4: strcpy (vg_replace_strmem.c:553)
==1246==    by 0x10B958: addProc (hash.c:101)
==1246==    by 0x10BF1F: makeProcs (proc.c:215)
==1246==    by 0x10AE31: draw_window (gmemusage.c:489)
==1246==    by 0x10AB84: main (gmemusage.c:300)
==1246==  Address 0x4e6b740 is 0 bytes after a block of size 3,584 alloc'd
==1246==    at 0x484582F: realloc (vg_replace_malloc.c:1437)
==1246==    by 0x10B9A4: addProc (hash.c:89)
==1246==    by 0x10BF1F: makeProcs (proc.c:215)
==1246==    by 0x10AE31: draw_window (gmemusage.c:489)
==1246==    by 0x10AB84: main (gmemusage.c:300)
==1246==



--- hash.c.orig 1998-01-14 17:43:13.000000000 +0100
+++ hash.c      2023-11-25 11:26:06.292932169 +0100
@@ -10,2 +10,3 @@
 #include <malloc.h>
+#include <string.h>
 #include "common.h"
@@ -73,3 +74,4 @@ addProc ( char *procname , int mem , int
       thisproc = nextproc = procs ;
-      strcpy ( thisproc -> procname , procname ) ;
+      strncpy ( thisproc -> procname , procname , sizeof(thisproc -> procname) 
) ;
+      thisproc -> procname[sizeof(thisproc -> procname)-1] = '\0';
       thisproc -> totMem = mem ;
@@ -100,3 +102,4 @@ addProc ( char *procname , int mem , int
       thisproc = procs + nProcs ;
-      strcpy ( thisproc -> procname , procname ) ;
+      strncpy ( thisproc -> procname , procname , sizeof(thisproc -> procname) 
) ;
+      thisproc -> procname[sizeof(thisproc -> procname)-1] = '\0';
       thisproc -> totMem = mem ;

Reply via email to