shithub: 9pro

Download patch

ref: 2b78d49cb789a70e1ba36c353c5f04d2715648b1
parent: c52cc89283ceb0106db86d332d5134c8b07d67e2
author: Sigrid Haflínudóttir <[email protected]>
date: Sat Dec 21 21:25:05 EST 2019

make valgrind happier

--- a/9pex.c
+++ b/9pex.c
@@ -310,6 +310,7 @@
 
 		if ((real = realpath(path, NULL)) == NULL)
 			break;
+		free(path);
 		if (strlen(real) < rootlen) { /* don't escape root */
 			free(real);
 			real = strdup(rootpath);
@@ -520,6 +521,7 @@
 		}
 	}
 
+	num = 0;
 	for (i = 0; i < nelem(c9st); i++) {
 		dirpos[i] = telldir(f->dir); /* so we can rewind in case another stat doesn't fit */
 
@@ -537,14 +539,14 @@
 		}
 		if (stat2c9stat(e->d_name, &st, &c9st[i], err) != 0)
 			return -1;
-		c9stp[i] = &c9st[i];
+		c9stp[num++] = &c9st[i];
 	}
 
-	num = i;
 	if (s9readdir(c, tag, c9stp, &num, &f->diroffset, size) != 0)
 		return -1;
 	trace("<- Rread tag=%d ...\n", tag);
-	seekdir(f->dir, dirpos[num]);
+	if (num > 0)
+		seekdir(f->dir, dirpos[num]);
 
 	return 0;
 }
@@ -654,7 +656,7 @@
 			err = Enowrite;
 			break;
 		case Tclunk:
-			trace("\n");
+			trace(" fid=%d\n", t->fid);
 			if (delfid(t->fid, &err) == 0 && s9do(s9clunk(c, t->tag), &err) == 0)
 				trace("<- Rclunk tag=%d\n", t->tag);
 			break;
@@ -685,7 +687,8 @@
 int
 main(int argc, char **argv)
 {
-	int can;
+	Fid *f;
+	int can, i;
 
 	used(argc); used(argv);
 
@@ -707,8 +710,8 @@
 	ctx.t = ctxt;
 	ctx.error = trace;
 
-	rdbuf = malloc(ctx.msize);
-	wrbuf = malloc(ctx.msize);
+	rdbuf = calloc(1, ctx.msize);
+	wrbuf = calloc(1, ctx.msize);
 	wroff = 0;
 
 	for (;;) {
@@ -717,6 +720,22 @@
 		if ((can & Canrd) != 0 && s9proc(&ctx) != 0)
 			break;
 	}
+
+	for (i = 0; i < numfids; i++) {
+		if ((f = fids[i]) != NULL) {
+			if (f->dir != NULL)
+				closedir(f->dir);
+			else if (f->fd >= 0)
+				close(f->fd);
+			free(f->path);
+			free(f);
+		}
+	}
+
+	memset(wrbuf, 0, ctx.msize);
+	free(wrbuf);
+	memset(rdbuf, 0, ctx.msize);
+	free(rdbuf);
 
 	return 0;
 }