[PATCH BlueZ 2/2] shared/shell: Fix memory leak by generator for submenu

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Since asprintf() allocates new memory when a submenu command is
complemented, the memory leak occurs as below:

   8 bytes in 1 blocks are definitely lost in loss record 18 of 179
      at 0x4C2FB0F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
      by 0x5679C99: strdup (strdup.c:42)
      by 0x13B1E7: find_cmd.constprop.2 (shell.c:597)
      by 0x13B2D1: cmd_generator (shell.c:646)
      by 0x53B976D: rl_completion_matches (in /lib/x86_64-linux-gnu/libreadline.so.7.0)
      by 0x13BA91: shell_completion (shell.c:777)
      by 0x53B98B6: ??? (in /lib/x86_64-linux-gnu/libreadline.so.7.0)
      by 0x53B9A99: rl_complete_internal (in /lib/x86_64-linux-gnu/libreadline.so.7.0)
      by 0x53B02EE: _rl_dispatch_subseq (in /lib/x86_64-linux-gnu/libreadline.so.7.0)
      by 0x53B07B5: readline_internal_char (in /lib/x86_64-linux-gnu/libreadline.so.7.0)
      by 0x53C8F84: rl_callback_read_char (in /lib/x86_64-linux-gnu/libreadline.so.7.0)
      by 0x13AD80: input_read (shell.c:1065)
---
 src/shared/shell.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/src/shared/shell.c b/src/shared/shell.c
index 7417e7ab4..9cd8d2567 100644
--- a/src/shared/shell.c
+++ b/src/shared/shell.c
@@ -645,10 +645,16 @@ static char *cmd_generator(const char *text, int state)
 	cmd = find_cmd(text + strlen(menu->name) + 1, menu->entries, &index);
 	if (cmd) {
 		int err;
+		char *tmp;
+
+		err = asprintf(&tmp, "%s.%s", menu->name, cmd);
+
+		free(cmd);
 
-		err = asprintf(&cmd, "%s.%s", menu->name, cmd);
 		if (err < 0)
 			return NULL;
+
+		cmd = tmp;
 	}
 
 	return cmd;
-- 
2.14.1

--
To unsubscribe from this list: send the line "unsubscribe linux-bluetooth" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html



[Index of Archives]     [Bluez Devel]     [Linux Wireless Networking]     [Linux Wireless Personal Area Networking]     [Linux ATH6KL]     [Linux USB Devel]     [Linux Media Drivers]     [Linux Audio Users]     [Linux Kernel]     [Linux SCSI]     [Big List of Linux Books]

  Powered by Linux