{"thread":{"id":"30509","subject":"[PATCH 1/2] dir: respect string length argument of read_directory_recursive()","startedAt":"2012-05-11T14:53:07Z","lastAt":"2012-05-11T14:59:25Z","messageCount":2,"participants":["René Scharfe"],"isPatch":true,"patchVersion":1,"patchTotal":2},"messages":[{"id":"191397","messageId":"4FAD27D3.7070208@lsrfire.ath.cx","threadId":"30509","inReplyTo":null,"subject":"[PATCH 1/2] dir: respect string length argument of read_directory_recursive()","fromName":"René Scharfe","fromEmail":"rene.scharfe@lsrfire.ath.cx","sentAt":"2012-05-11T14:53:07Z","receivedAt":"2012-05-11T14:53:07Z","isPatch":true,"sender":{"key":"l.s.r@web.de","avatar":"https://avatars.githubusercontent.com/u/26122331?v=4"},"body":"A directory name is passed to read_directory_recursive() as a\nlength-limited string, through the parameters base and baselen.\nSuprisingly, base must be a NUL-terminated string as well, as it is\npassed to opendir(), ignoring baselen.\n\nFix this by postponing the call to opendir() until the length-limted\nstring is added to a strbuf, which provides a NUL in the right place.\n\nSigned-off-by: Rene Scharfe <rene.scharfe@lsrfire.ath.cx>\n---\n dir.c |   16 ++++++++--------\n 1 file changed, 8 insertions(+), 8 deletions(-)\n\ndiff --git a/dir.c b/dir.c\nindex c6a98cc..d5444fb 100644\n--- a/dir.c\n+++ b/dir.c\n@@ -960,16 +960,17 @@ static int read_directory_recursive(struct dir_struct *dir,\n \t\t\t\t    int check_only,\n \t\t\t\t    const struct path_simplify *simplify)\n {\n-\tDIR *fdir = opendir(*base ? base : \".\");\n+\tDIR *fdir;\n \tint contents = 0;\n \tstruct dirent *de;\n \tstruct strbuf path = STRBUF_INIT;\n \n-\tif (!fdir)\n-\t\treturn 0;\n-\n \tstrbuf_add(&path, base, baselen);\n \n+\tfdir = opendir(path.len ? path.buf : \".\");\n+\tif (!fdir)\n+\t\tgoto out;\n+\n \twhile ((de = readdir(fdir)) != NULL) {\n \t\tswitch (treat_path(dir, de, &path, baselen, simplify)) {\n \t\tcase path_recurse:\n@@ -984,12 +985,11 @@ static int read_directory_recursive(struct dir_struct *dir,\n \t\t}\n \t\tcontents++;\n \t\tif (check_only)\n-\t\t\tgoto exit_early;\n-\t\telse\n-\t\t\tdir_add_name(dir, path.buf, path.len);\n+\t\t\tbreak;\n+\t\tdir_add_name(dir, path.buf, path.len);\n \t}\n-exit_early:\n \tclosedir(fdir);\n+ out:\n \tstrbuf_release(&path);\n \n \treturn contents;\n-- \n1.7.10.1\n"},{"id":"191398","messageId":"4FAD294D.4010101@lsrfire.ath.cx","threadId":"30509","inReplyTo":"4FAD27D3.7070208@lsrfire.ath.cx","subject":"[PATCH 2/2] dir: simplify fill_directory()","fromName":"René Scharfe","fromEmail":"rene.scharfe@lsrfire.ath.cx","sentAt":"2012-05-11T14:59:25Z","receivedAt":"2012-05-11T14:59:25Z","isPatch":true,"sender":{"key":"l.s.r@web.de","avatar":"https://avatars.githubusercontent.com/u/26122331?v=4"},"body":"Now that read_directory_recursive() (reached through read_directory())\nrespects the string length limit we provide, we don't need to create a\nNUL-limited copy of the common prefix anymore.\n\nSigned-off-by: Rene Scharfe <rene.scharfe@lsrfire.ath.cx>\n---\n dir.c |    9 +--------\n 1 file changed, 1 insertion(+), 8 deletions(-)\n\ndiff --git a/dir.c b/dir.c\nindex d5444fb..ed1510f 100644\n--- a/dir.c\n+++ b/dir.c\n@@ -74,7 +74,6 @@ char *common_prefix(const char **pathspec)\n \n int fill_directory(struct dir_struct *dir, const char **pathspec)\n {\n-\tconst char *path;\n \tsize_t len;\n \n \t/*\n@@ -82,15 +81,9 @@ int fill_directory(struct dir_struct *dir, const char **pathspec)\n \t * use that to optimize the directory walk\n \t */\n \tlen = common_prefix_len(pathspec);\n-\tpath = \"\";\n-\n-\tif (len)\n-\t\tpath = xmemdupz(*pathspec, len);\n \n \t/* Read the directory and prune it */\n-\tread_directory(dir, path, len, pathspec);\n-\tif (*path)\n-\t\tfree((char *)path);\n+\tread_directory(dir, pathspec ? *pathspec : \"\", len, pathspec);\n \treturn len;\n }\n \n-- \n1.7.10.1\n"}]}