Skip to contentSkip to conversation

rpcdebug: fix memory allocation size

PATCH In review Olga Kornievskaia 2 participants 2 messages Aug 14, 2024 – Aug 16, 2024

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 rpcdebug: fix memory allocation size

Memory isn't allocated enough to hold the null terminator.

Valgring complains about invalid memory access:

[aglo@localhost rpcdebug]$ valgrind ./rpcdebug
==222602== Memcheck, a memory error detector
==222602== Copyright (C) 2002-2024, and GNU GPL'd, by Julian Seward et al. ==222602== Using Valgrind-3.23.0 and LibVEX; rerun with -h for copyright info ==222602== Command: ./rpcdebug
==222602==
==222602== Invalid write of size 1
==222602== at 0x4871218: strcpy (vg_replace_strmem.c:564)
==222602== by 0x400CA3: main (rpcdebug.c:62)
==222602== Address 0x4a89048 is 0 bytes after a block of size 8 alloc'd
==222602== at 0x4868388: malloc (vg_replace_malloc.c:446)
==222602== by 0x400C77: main (rpcdebug.c:57)
==222602==
==222602== Invalid read of size 1

==222602==    at 0x48710E4: __GI_strlen (vg_replace_strmem.c:506)
==222602==    by 0x492FA7F: __vfprintf_internal (vfprintf-internal.c:1647)
==222602==    by 0x49302F3: buffered_vfprintf (vfprintf-internal.c:2296)
==222602==    by 0x492F21F: __vfprintf_internal (vfprintf-internal.c:1377)
==222602==    by 0x491BC93: fprintf (fprintf.c:32)
==222602==    by 0x40103F: main (rpcdebug.c:100)

==222602== Address 0x4a89048 is 0 bytes after a block of size 8 alloc'd
==222602== at 0x4868388: malloc (vg_replace_malloc.c:446)

---
 tools/rpcdebug/rpcdebug.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/rpcdebug/rpcdebug.c +1 -1
index ec05179e..1f935223 100644
@@ -54,7 +54,7 @@ main(int argc, char **argv)
 	char *		module = NULL;
 	int		c;
 
-	cdename = malloc(strlen(basename(argv[0])));
+	cdename = malloc(strlen(basename(argv[0])) + 1);
 	if (cdename == NULL) {
 	  fprintf(stderr, "failed in malloc\n");
 	  exit(1);
Signature · 1 line
2.43.5
#1 lore raw
Quoted 24 lines · Olga Kornievskaia

Memory isn't allocated enough to hold the null terminator.

Valgring complains about invalid memory access:

[aglo@localhost rpcdebug]$ valgrind ./rpcdebug
==222602== Memcheck, a memory error detector
==222602== Copyright (C) 2002-2024, and GNU GPL'd, by Julian Seward et al. ==222602== Using Valgrind-3.23.0 and LibVEX; rerun with -h for copyright info ==222602== Command: ./rpcdebug
==222602==
==222602== Invalid write of size 1
==222602== at 0x4871218: strcpy (vg_replace_strmem.c:564)
==222602== by 0x400CA3: main (rpcdebug.c:62)
==222602== Address 0x4a89048 is 0 bytes after a block of size 8 alloc'd
==222602== at 0x4868388: malloc (vg_replace_malloc.c:446)
==222602== by 0x400C77: main (rpcdebug.c:57)
==222602==
==222602== Invalid read of size 1

==222602==    at 0x48710E4: __GI_strlen (vg_replace_strmem.c:506)
==222602==    by 0x492FA7F: __vfprintf_internal (vfprintf-internal.c:1647)
==222602==    by 0x49302F3: buffered_vfprintf (vfprintf-internal.c:2296)
==222602==    by 0x492F21F: __vfprintf_internal (vfprintf-internal.c:1377)
==222602==    by 0x491BC93: fprintf (fprintf.c:32)
==222602==    by 0x40103F: main (rpcdebug.c:100)
Quoted 4 lines

==222602== Address 0x4a89048 is 0 bytes after a block of size 8 alloc'd
==222602== at 0x4868388: malloc (vg_replace_malloc.c:446)

Committed...

steved.

Quoted 17 lines
---
  tools/rpcdebug/rpcdebug.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
tools/rpcdebug/rpcdebug.c +1 -1
index ec05179e..1f935223 100644
@@ -54,7 +54,7 @@ main(int argc, char **argv)
  	char *		module = NULL;
  	int		c;
 
-	cdename = malloc(strlen(basename(argv[0])));
+	cdename = malloc(strlen(basename(argv[0])) + 1);
  	if (cdename == NULL) {
  	  fprintf(stderr, "failed in malloc\n");
  	  exit(1);