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 <rene.scharfe@lsrfire.ath.cx> Signed-off-by: Junio C Hamano <gitster@pobox.com>
René Scharfe committed
May 11, 2012 at 16:53 UTC
1528d247e53bcf33db5b004a25a7d9b8c75c90e4
1 file changed
+8
-8
dir.c
+8
-8
@@ -960,16 +960,17 @@ static int read_directory_recursive(struct dir_struct *dir,
960
int check_only,
961
const struct path_simplify *simplify)
962
{
963
- DIR *fdir = opendir(*base ? base : ".");
963
+ DIR *fdir;
964
int contents = 0;
965
struct dirent *de;
966
struct strbuf path = STRBUF_INIT;
967
968
- if (!fdir)
969
- return 0;
970
-
968
strbuf_add(&path, base, baselen);
969
970
+ fdir = opendir(path.len ? path.buf : ".");
971
+ if (!fdir)
972
+ goto out;
973
+
974
while ((de = readdir(fdir)) != NULL) {
975
switch (treat_path(dir, de, &path, baselen, simplify)) {
976
case path_recurse:
@@ -984,12 +985,11 @@ static int read_directory_recursive(struct dir_struct *dir,
985
}
986
contents++;
987
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);
990
}
991
-exit_early:
991
closedir(fdir);
992
+ out:
993
strbuf_release(&path);
994
995
return contents;