[Ltrace-devel] [PATCH] Fix a memory leak in populate_this_symtab

Роман Донченко dpb at corrigendum.ru
Thu Apr 9 22:16:06 UTC 2015


symbols is only used (and freed) if only_exported_names is true, so
don't allocate it if it's false.
---
 ltrace-elf.c | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)

diff --git a/ltrace-elf.c b/ltrace-elf.c
index 24d02f0..f439cb0 100644
--- a/ltrace-elf.c
+++ b/ltrace-elf.c
@@ -908,11 +908,15 @@ populate_this_symtab(struct process *proc, const char *filename,
 	 * should be well enough for the number of symbols that we
 	 * typically deal with.  */
 	size_t num_symbols = 0;
-	struct unique_symbol *symbols = malloc(sizeof(*symbols) * count);
-	if (symbols == NULL) {
-		fprintf(stderr, "couldn't insert symbols for -x: %s\n",
-			strerror(errno));
-		return -1;
+	struct unique_symbol *symbols = NULL;
+
+	if (!only_exported_names) {
+		symbols = malloc(sizeof(*symbols) * count);
+		if (symbols == NULL) {
+			fprintf(stderr, "couldn't insert symbols for -x: %s\n",
+				strerror(errno));
+			return -1;
+		}
 	}
 
 	GElf_Word secflags[lte->ehdr.e_shnum];
-- 
1.8.5.6




More information about the Ltrace-devel mailing list