Skip to contentSkip to conversation

libxcmd: add return value check for dynamic memory function

PATCH Superseded Weifeng Su 3 participants 3 messages Jun 7, 2023

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Weifeng Su

author
#0 lore raw

PATCH 1/1 libxcmd: add return value check for dynamic memory function

The result check was missed and It cause the coredump like:
0x00005589f3e358dd in add_command (ci=0x5589f3e3f020 <health_cmd>) at command.c:37 0x00005589f3e337d8 in init_commands () at init.c:37
init (argc=<optimized out>, argv=0x7ffecfb0cd28) at init.c:102 0x00005589f3e33399 in main (argc=<optimized out>, argv=<optimized out>) at init.c:112

Add check for realloc function to ignore this coredump and exit with error output

---
 libxcmd/command.c | 4 ++++
 1 file changed, 4 insertions(+)
libxcmd/command.c +4 -0
index a76d1515..47d050c3 100644
@@ -34,6 +34,10 @@ add_command(
 	const cmdinfo_t	*ci)
 {
 	cmdtab = realloc((void *)cmdtab, ++ncmds * sizeof(*cmdtab));
+	if (!cmdtab) {
+		perror("realloc");
+		exit(1);
+	}
 	cmdtab[ncmds - 1] = *ci;
 	qsort(cmdtab, ncmds, sizeof(*cmdtab), compare);
 }
Signature · 1 line
2.18.0.windows.1
#2 lore raw
In reply to Weifeng Su
Quoted 19 lines · Weifeng Su

The result check was missed and It cause the coredump like:
0x00005589f3e358dd in add_command (ci=0x5589f3e3f020 <health_cmd>) at command.c:37 0x00005589f3e337d8 in init_commands () at init.c:37
init (argc=<optimized out>, argv=0x7ffecfb0cd28) at init.c:102 0x00005589f3e33399 in main (argc=<optimized out>, argv=<optimized out>) at init.c:112

Add check for realloc function to ignore this coredump and exit with error output

---
 libxcmd/command.c | 4 ++++
 1 file changed, 4 insertions(+)
libxcmd/command.c +0 -0
index a76d1515..47d050c3 100644

libxcmd/command.c add_command( · line 38

Quoted 5 lines
libxcmd/command.c +2 -0
@@ -34,6 +34,10 @@ add_command(
 	const cmdinfo_t	*ci)
 {
 	cmdtab = realloc((void *)cmdtab, ++ncmds * sizeof(*cmdtab));
+	if (!cmdtab) {
+		perror("realloc");

The string argument should be more descriptive of /where/ we failed.

"realloc: Out of memory"

Tells us what failed, but not where; and we could probably figure that out from strace data.

	if (!cmdtab)
		perror("add_command");

	"add_command: Out of memory"

is better at implying that the triager should look for a function named add_command and start there. You could even go as far as:

	if (!cmdtab)
		perror(_("adding libxcmd command"));

	"adding libxcmd command: Out of memory"

which gives us a nice long string to grep.

--D

Quoted 8 lines
libxcmd/command.c +2 -0
+		exit(1);
+	}
 	cmdtab[ncmds - 1] = *ci;
 	qsort(cmdtab, ncmds, sizeof(*cmdtab), compare);
 }
Signature · 1 line
2.18.0.windows.1