Commit ed83bff79c0a5866dbd7df60234018c0e350b451

Stefan Sperling 2019-02-05T15:50:03

we must use dynamic allocation for dirents in diff_fileindex_dir()

diff --git a/lib/fileindex.c b/lib/fileindex.c
index 5208c1b..27bcbfa 100644
--- a/lib/fileindex.c
+++ b/lib/fileindex.c
@@ -749,16 +749,31 @@ diff_fileindex_dir(struct got_fileindex *fileindex,
 
 	while (1) {
 		struct got_pathlist_entry *new = NULL;
+		struct dirent *dep = NULL;
 
-		de = readdir(dir);
-		if (de == NULL)
+		de = malloc(sizeof(struct dirent) + NAME_MAX + 1);
+		if (de == NULL) {
+			err = got_error_from_errno();
+			goto done;
+		}
+
+		if (readdir_r(dir, de, &dep) != 0) {
+			err = got_error_from_errno();
+			goto done;
+		}
+		if (dep == NULL) {
+			free(de);
+			de = NULL;
 			break;
+		}
 
 		if (strcmp(de->d_name, ".") == 0 ||
 		    strcmp(de->d_name, "..") == 0 ||
 		    (path[0] == '\0' &&
-		    strcmp(de->d_name, GOT_WORKTREE_GOT_DIR) == 0))
+		    strcmp(de->d_name, GOT_WORKTREE_GOT_DIR) == 0)) {
+			free(de);
 			continue;
+		}
 
 		err = got_pathlist_insert(&new, &dirlist, de->d_name, de);
 		if (err)
@@ -816,6 +831,8 @@ diff_fileindex_dir(struct got_fileindex *fileindex,
 		}
 	}
 done:
+	TAILQ_FOREACH(dle, &dirlist, entry)
+		free(dle->data);
 	got_pathlist_free(&dirlist);
 	return err;
 }