Skip to contentSkip to conversation

master - coverity: avoid memleak

PATCH Zdenek Kabelac 1 participant 1 message Nov 7, 2017

lore
Share & tools
lore
#0 lore raw
Gitweb:        https://sourceware.org/git/?p=lvm2.git;a=commitdiff;h=56b527a6fbf6f886af4c78dd118867e4513c6a9c
Commit:        56b527a6fbf6f886af4c78dd118867e4513c6a9c
Parent:        14d0b0bbddb214fbb5dcacfb60d1723d87076bd9
Author:        Zdenek Kabelac <[email protected]>
AuthorDate:    Sun Nov 5 18:39:05 2017 +0100
Committer:     Zdenek Kabelac <[email protected]>
CommitterDate: Tue Nov 7 21:26:09 2017 +0100

coverity: avoid memleak

When security_level was set, allocated filename was leaking.

---
 WHATS_NEW           |    1 +
 tools/vgcfgbackup.c |   13 +++++++++----
 2 files changed, 10 insertions(+), 4 deletions(-)
WHATS_NEW +1 -0
index 94a7af6..0775d7b 100644
@@ -1,5 +1,6 @@
 Version 2.02.177 -
 ====================================
+  Fix memleak of string buffer when vgcfgbackup runs in secure mode.
   Do not print error when clvmd cannot find running clvmd.
   Prevent start of new merge of snapshot if origin is already being merged.
 
tools/vgcfgbackup.c +9 -4
index 5d21131..814ed1d 100644
@@ -20,8 +20,13 @@ static char *_expand_filename(const char *template, const char *vg_name,
 {
 	char *filename;
 
-	if (security_level())
-		return dm_strdup(template);
+	if (security_level()) {
+		if (!(filename = dm_strdup(template))) {
+			log_error("Failed to allocate filename.");
+			return NULL;
+		}
+		goto out;
+	}
 
 	if (!(filename = dm_malloc(PATH_MAX))) {
 		log_error("Failed to allocate filename.");
@@ -31,7 +36,7 @@ static char *_expand_filename(const char *template, const char *vg_name,
 	if (dm_snprintf(filename, PATH_MAX, template, vg_name) < 0) {
 		log_error("Error processing filename template %s",
 			   template);
-		dm_free(filename);	
+		dm_free(filename);
 		return NULL;
 	}
 	if (*last_filename && !strncmp(*last_filename, filename, PATH_MAX)) {
@@ -40,7 +45,7 @@ static char *_expand_filename(const char *template, const char *vg_name,
 		dm_free(filename);
 		return NULL;
 	}
-
+out:
 	dm_free(*last_filename);
 	*last_filename = filename;