Skip to contentSkip to conversation

libmultipath: Fix memory leak problem in print_multipath_topology

PATCH Applied Yingtai Xie 2 participants 2 messages Dec 29, 2014 – Jan 8, 2015 Device mapper

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 libmultipath: Fix memory leak problem in print_multipath_topology

/sys/block/ directory has been opened in snprint_devices, the handler should be released when returned abnormally. Similar memory leak problem happens in print_multipath_topology when carried out multipath -ll command.

---
 libmultipath/print.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
libmultipath/print.c +7 -2
index 383eae4..916906f 100644
@@ -890,6 +890,7 @@ print_multipath_topology (struct multipath * mpp, int verbosity)
 		}
 	} while (resize);
 	printf("%s", buff);
+	FREE(buff);
 }
 
 extern int
@@ -1427,8 +1428,10 @@ snprint_devices (char * buff, int len, struct vectors *vecs)
 	if (!(blkdir = opendir("/sys/block")))
 		return 1;
 
-	if ((len - fwd - threshold) <= 0)
+	if ((len - fwd - threshold) <= 0) {
+		closedir(blkdir);
 		return len;
+	}
 	fwd += snprintf(buff + fwd, len - fwd, "available block devices:\n");
 
 	strcpy(devpath,"/sys/block/");
@@ -1446,8 +1449,10 @@ snprint_devices (char * buff, int len, struct vectors *vecs)
 		if (S_ISDIR(statbuf.st_mode) == 0)
 			continue;
 
-		if ((len - fwd - threshold)  <= 0)
+		if ((len - fwd - threshold)  <= 0) {
+			closedir(blkdir);
 			return len;
+		}
 
 		fwd += snprintf(buff + fwd, len - fwd, "    %s", devptr);
 		pp = find_path_by_dev(vecs->pathvec, devptr);
Signature · 1 line
1.7.12.4
#1 lore raw

Applied,
thanks.

Quoted 53 lines · Yingtai Xie

/sys/block/ directory has been opened in snprint_devices, the handler should be released when returned abnormally. Similar memory leak problem happens in print_multipath_topology when carried out multipath -ll command.

---
 libmultipath/print.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
libmultipath/print.c +1 -0
index 383eae4..916906f 100644
@@ -890,6 +890,7 @@ print_multipath_topology (struct multipath * mpp, int
 verbosity)
                }
        } while (resize);
        printf("%s", buff);
+       FREE(buff);
 }
 

extern int

libmultipath/print.c +3 -1
@@ -1427,8 +1428,10 @@ snprint_devices (char * buff, int len, struct
 vectors *vecs)
        if (!(blkdir = opendir("/sys/block")))
                return 1;
 
-       if ((len - fwd - threshold) <= 0)
+       if ((len - fwd - threshold) <= 0) {
+               closedir(blkdir);
                return len;
+       }
        fwd += snprintf(buff + fwd, len - fwd, "available block
 devices:\n");
        strcpy(devpath,"/sys/block/");
libmultipath/print.c +3 -1
@@ -1446,8 +1449,10 @@ snprint_devices (char * buff, int len, struct
 vectors *vecs)
                if (S_ISDIR(statbuf.st_mode) == 0)
                        continue;
 
-               if ((len - fwd - threshold)  <= 0)
+               if ((len - fwd - threshold)  <= 0) {
+                       closedir(blkdir);
                        return len;
+               }
 
                fwd += snprintf(buff + fwd, len - fwd, "    %s", devptr);
                pp = find_path_by_dev(vecs->pathvec, devptr);

--
1.7.12.4