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

[PATCH v2 4/6] win32: dirent: handle errors

From
Erik Faye-Lund <kusmabite@gmail.com>
Date
Nov 23, 2010, 18:38 UTC
Message-ID
<1290537509-360-5-git-send-email-kusmabite@gmail.com>
In-Reply-To
<1290537509-360-1-git-send-email-kusmabite@gmail.com>

Previously all error conditions were ignored. Be nice, and set errno when we should.

Signed-off-by: Erik Faye-Lund <kusmabite@gmail.com>
---
 compat/mingw.c |    2 +-
 compat/msvc.c  |   28 +++++++++++++++++++++++++++-
 2 files changed, 28 insertions(+), 2 deletions(-)
diff --git a/compat/mingw.c b/compat/mingw.c
index fdbf093..d8fd5d8 100644
--- a/compat/mingw.c
+++ b/compat/mingw.c
@@ -1584,7 +1584,7 @@ struct dirent *mingw_readdir(DIR *dir)
 	HANDLE handle;
 	struct mingw_DIR *mdir = (struct mingw_DIR*)dir;
 
-	if (!dir->dd_handle) {
+	if (!dir || !dir->dd_handle) {
 		errno = EBADF; /* No set_errno for mingw */
 		return NULL;
 	}
diff --git a/compat/msvc.c b/compat/msvc.c
index 88c6093..199eb22 100644
--- a/compat/msvc.c
+++ b/compat/msvc.c
@@ -5,8 +5,29 @@
 
 DIR *opendir(const char *name)
 {
-	int len = strlen(name);
+	DWORD attrs = GetFileAttributes(name);
+	int len;
 	DIR *p;
+
+	/* check for valid path */
+	if (attrs == INVALID_FILE_ATTRIBUTES) {
+		errno = ENOENT;
+		return NULL;
+	}
+
+	/* check if it's a directory */
+	if (!(attrs & FILE_ATTRIBUTE_DIRECTORY)) {
+		errno = ENOTDIR;
+		return NULL;
+	}
+
+	/* check that the pattern won't be too long for FindFirstFileA */
+	len = strlen(name);
+	if (len + 2 >= MAX_PATH) {
+		errno = ENAMETOOLONG;
+		return NULL;
+	}
+
 	p = malloc(sizeof(DIR) + len + 2);
 	if (!p)
 		return NULL;
@@ -21,6 +42,11 @@ DIR *opendir(const char *name)
 }
 int closedir(DIR *dir)
 {
+	if (!dir) {
+		errno = EBADF;
+		return -1;
+	}
+
 	if (dir->dd_handle != (long)INVALID_HANDLE_VALUE)
 		FindClose((HANDLE)dir->dd_handle);
 	free(dir);
-- 
1.7.3.2.493.gc8738
Previous: Erik Faye-LundNext: Erik Faye-Lund
Message 6 of 11 in “win32-dirent”
  1. 0/6 win32-direntErik Faye-Lund, Nov 23, 2010
  2. 1/6 msvc: opendir: fix malloc-failureErik Faye-Lund, Nov 23, 2010
  3. 2/6 msvc: opendir: allocate enough memoryErik Faye-Lund, Nov 23, 2010
  4. Junio C HamanoNov 26, 2010
  5. 3/6 msvc: opendir: do not start the searchErik Faye-Lund, Nov 23, 2010
  6. 4/6 win32: dirent: handle errorsErik Faye-Lund, Nov 23, 2010
  7. 5/6 msvc: opendir: handle paths ending with a slashErik Faye-Lund, Nov 23, 2010
  8. René ScharfeNov 23, 2010
  9. Erik Faye-LundNov 23, 2010
  10. René ScharfeNov 23, 2010
  11. 6/6 win32: use our own dirent.hErik Faye-Lund, Nov 23, 2010

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.