git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 1/2] dir: respect string length argument of read_directory_recursive()

From
René Scharfe <rene.scharfe@lsrfire.ath.cx>
Date
May 11, 2012, 14:53 UTC
Message-ID
<4FAD27D3.7070208@lsrfire.ath.cx>

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>
---
 dir.c |   16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)
diff --git a/dir.c b/dir.c
index c6a98cc..d5444fb 100644
--- a/dir.c
+++ b/dir.c
@@ -960,16 +960,17 @@ static int read_directory_recursive(struct dir_struct *dir,
 				    int check_only,
 				    const struct path_simplify *simplify)
 {
-	DIR *fdir = opendir(*base ? base : ".");
+	DIR *fdir;
 	int contents = 0;
 	struct dirent *de;
 	struct strbuf path = STRBUF_INIT;
 
-	if (!fdir)
-		return 0;
-
 	strbuf_add(&path, base, baselen);
 
+	fdir = opendir(path.len ? path.buf : ".");
+	if (!fdir)
+		goto out;
+
 	while ((de = readdir(fdir)) != NULL) {
 		switch (treat_path(dir, de, &path, baselen, simplify)) {
 		case path_recurse:
@@ -984,12 +985,11 @@ static int read_directory_recursive(struct dir_struct *dir,
 		}
 		contents++;
 		if (check_only)
-			goto exit_early;
-		else
-			dir_add_name(dir, path.buf, path.len);
+			break;
+		dir_add_name(dir, path.buf, path.len);
 	}
-exit_early:
 	closedir(fdir);
+ out:
 	strbuf_release(&path);
 
 	return contents;
-- 
1.7.10.1
Next: René Scharfe
Message 1 of 2 in “dir: respect string length argument of read_directory_recursive()”
  1. 1/2 dir: respect string length argument of read_directory_recursive()René Scharfe, May 11, 2012
  2. 2/2 dir: simplify fill_directory()René Scharfe, May 11, 2012

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.