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

[PATCH v2 1/6] msvc: opendir: fix malloc-failure

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

Previsouly, the code checked for malloc-failure after it had accessed the returned pointer. Move the check a bit earlier to avoid segfault.

Signed-off-by: Erik Faye-Lund <kusmabite@gmail.com>
---
 compat/msvc.c |    8 ++++----
 1 files changed, 4 insertions(+), 4 deletions(-)
diff --git a/compat/msvc.c b/compat/msvc.c
index ac04a4c..d6096e4 100644
--- a/compat/msvc.c
+++ b/compat/msvc.c
@@ -7,16 +7,16 @@ DIR *opendir(const char *name)
 {
 	int len;
 	DIR *p;
-	p = (DIR*)malloc(sizeof(DIR));
+	p = malloc(sizeof(DIR));
+	if (!p)
+		return NULL;
+
 	memset(p, 0, sizeof(DIR));
 	strncpy(p->dd_name, name, PATH_MAX);
 	len = strlen(p->dd_name);
 	p->dd_name[len] = '/';
 	p->dd_name[len+1] = '*';
 
-	if (p == NULL)
-		return NULL;
-
 	p->dd_handle = _findfirst(p->dd_name, &p->dd_dta);
 
 	if (p->dd_handle == -1) {
-- 
1.7.3.2.493.gc8738
Previous: Erik Faye-LundNext: Erik Faye-Lund
Message 2 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.