shithub: 9pro

Download patch

ref: d93d385b79eea9111edc7a4822fbcfff36d537ae
parent: e05b2fc3331fa1f0eb6196ae92d62276c05e6cc6
author: Sigrid Solveig Haflínudóttir <[email protected]>
date: Mon Nov 8 16:52:16 EST 2021

9pex: fixes for two (unlikely but) possible memory leaks

--- a/9pex.c
+++ b/9pex.c
@@ -260,12 +260,16 @@
 	if (statpath(path, &st, err) != 0)
 		return NULL;
 
-	if ((f = calloc(1, sizeof(*f))) == NULL) {
+	if ((f = calloc(1, sizeof(*f))) == NULL || (f->path = strdup(path)) == NULL) {
+nomem:
+		if (f != NULL) {
+			free(f->path);
+			free(f);
+		}
 		*err = Enomem;
 		return NULL;
 	}
 	f->fd = -1;
-	f->path = strdup(path);
 	f->name = strrchr(f->path, '/');
 	if (f->name == NULL)
 		f->name = f->path;
@@ -281,10 +285,8 @@
 		}
 	}
 	if (i >= numfids) {
-		if ((newfids = realloc(fids, (numfids+1)*sizeof(*fids))) == NULL) {
-			*err = Enomem;
-			return NULL;
-		}
+		if ((newfids = realloc(fids, (numfids+1)*sizeof(*fids))) == NULL)
+			goto nomem;
 		fids = newfids;
 		fids[numfids++] = f;
 	}
@@ -393,9 +395,10 @@
 		path[plen+1+ellen] = 0;
 
 		if (!rootescape) {
-			if ((real = realpath(path, NULL)) == NULL)
-				break;
+			real = realpath(path, NULL);
 			free(path);
+			if (real == NULL)
+				break;
 			if (strlen(real) < rootlen) { /* don't escape root */
 				free(real);
 				real = strdup(rootpath);