Skip to content

Commit 9585ed5

Browse files
kusmagitster
authored andcommitted
win32: dirent: handle errors
Previously all error conditions were ignored. Be nice, and set errno when we should. Signed-off-by: Erik Faye-Lund <kusmabite@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 20c6788 commit 9585ed5

2 files changed

Lines changed: 28 additions & 2 deletions

File tree

compat/mingw.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1582,7 +1582,7 @@ struct dirent *mingw_readdir(DIR *dir)
15821582
HANDLE handle;
15831583
struct mingw_DIR *mdir = (struct mingw_DIR*)dir;
15841584

1585-
if (!dir->dd_handle) {
1585+
if (!dir || !dir->dd_handle) {
15861586
errno = EBADF; /* No set_errno for mingw */
15871587
return NULL;
15881588
}

compat/msvc.c

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,29 @@
55

66
DIR *opendir(const char *name)
77
{
8-
int len = strlen(name);
8+
DWORD attrs = GetFileAttributes(name);
9+
int len;
910
DIR *p;
11+
12+
/* check for valid path */
13+
if (attrs == INVALID_FILE_ATTRIBUTES) {
14+
errno = ENOENT;
15+
return NULL;
16+
}
17+
18+
/* check if it's a directory */
19+
if (!(attrs & FILE_ATTRIBUTE_DIRECTORY)) {
20+
errno = ENOTDIR;
21+
return NULL;
22+
}
23+
24+
/* check that the pattern won't be too long for FindFirstFileA */
25+
len = strlen(name);
26+
if (len + 2 >= MAX_PATH) {
27+
errno = ENAMETOOLONG;
28+
return NULL;
29+
}
30+
1031
p = malloc(sizeof(DIR) + len + 2);
1132
if (!p)
1233
return NULL;
@@ -21,6 +42,11 @@ DIR *opendir(const char *name)
2142
}
2243
int closedir(DIR *dir)
2344
{
45+
if (!dir) {
46+
errno = EBADF;
47+
return -1;
48+
}
49+
2450
if (dir->dd_handle != (long)INVALID_HANDLE_VALUE)
2551
FindClose((HANDLE)dir->dd_handle);
2652
free(dir);

0 commit comments

Comments
 (0)