shithub: 9pro

Download patch

ref: 4a9f03f341b69bcad13c5ea24c855980c5e3c8f0
parent: fa3dedf095a17cdca105b57ee84f8f6f9117012f
author: Sigrid Haflínudóttir <ftrvxmtrx@gmail.com>
date: Thu Jun 18 09:51:44 EDT 2020

9pex: fix dir reading

--- a/9pex.c
+++ b/9pex.c
@@ -685,6 +685,7 @@
 		}
 	}
 
+	res = 0;
 	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 */
@@ -692,7 +693,8 @@
 		errno = 0;
 		if ((e = readdir(f->dir)) == NULL && errno != 0) {
 			*err = strerror(errno);
-			return -1;
+			res = -1;
+			goto done;
 		}
 		if (e == NULL) /* eof */
 			break;
@@ -701,22 +703,32 @@
 
 		if (fstatat(f->fd, e->d_name, &st, 0) != 0) {
 			*err = strerror(errno);
-			if (fstatat(f->fd, e->d_name, &st, AT_SYMLINK_NOFOLLOW) != 0) /* broken symlink, try to stat the link itself */
-				return -1;
+			if (fstatat(f->fd, e->d_name, &st, AT_SYMLINK_NOFOLLOW) != 0) { /* broken symlink, try to stat the link itself */
+				res = -1;
+				goto done;
+			}
 		}
-		if (stat2c9stat(e->d_name, &st, &c9st[i], err) != 0)
-			return -1;
+		if (stat2c9stat(e->d_name, &st, &c9st[i], err) != 0) {
+			res = -1;
+			goto done;
+		}
+		c9st[i].name = strdup(c9st[i].name);
 		c9stp[num++] = &c9st[i];
 	}
 
 	i = num;
-	if (s9do(s9readdir(c, tag, c9stp, &num, &f->diroffset, size), err) != 0)
-		return -1;
+	if (s9do(s9readdir(c, tag, c9stp, &num, &f->diroffset, size), err) != 0) {
+		res = -1;
+		goto done;
+	}
 	trace("<- Rread tag=%d count=%"PRIu64" data=...\n", tag, f->diroffset - offset);
 	if (i != num)
 		seekdir(f->dir, dirpos[num]);
 
-	return 0;
+done:
+	for (i = 0; i < num; i++)
+		free(c9st[i].name);
+	return res;
 }
 
 static void