Skip to content

Commit 1528d24

Browse files
René Scharfegitster
authored andcommitted
dir: respect string length argument of read_directory_recursive()
A directory name is passed to read_directory_recursive() as a length-limited string, through the parameters base and baselen. Suprisingly, base must be a NUL-terminated string as well, as it is passed to opendir(), ignoring baselen. Fix this by postponing the call to opendir() until the length-limted string is added to a strbuf, which provides a NUL in the right place. Signed-off-by: Rene Scharfe <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent bef3692 commit 1528d24

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

dir.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -960,16 +960,17 @@ static int read_directory_recursive(struct dir_struct *dir,
960960
int check_only,
961961
const struct path_simplify *simplify)
962962
{
963-
DIR *fdir = opendir(*base ? base : ".");
963+
DIR *fdir;
964964
int contents = 0;
965965
struct dirent *de;
966966
struct strbuf path = STRBUF_INIT;
967967

968-
if (!fdir)
969-
return 0;
970-
971968
strbuf_add(&path, base, baselen);
972969

970+
fdir = opendir(path.len ? path.buf : ".");
971+
if (!fdir)
972+
goto out;
973+
973974
while ((de = readdir(fdir)) != NULL) {
974975
switch (treat_path(dir, de, &path, baselen, simplify)) {
975976
case path_recurse:
@@ -984,12 +985,11 @@ static int read_directory_recursive(struct dir_struct *dir,
984985
}
985986
contents++;
986987
if (check_only)
987-
goto exit_early;
988-
else
989-
dir_add_name(dir, path.buf, path.len);
988+
break;
989+
dir_add_name(dir, path.buf, path.len);
990990
}
991-
exit_early:
992991
closedir(fdir);
992+
out:
993993
strbuf_release(&path);
994994

995995
return contents;

0 commit comments

Comments
 (0)