@matheusbsbs / matheus-bagliano / commits / 9585ed519c
Fork 0

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>

Erik Faye-Lund committed Nov 23, 2010 at 19:38 UTC 9585ed519c59d5ac46f8e12b339220cdd0567b7d
2 files changed +28 -2
compat/mingw.c
+1 -1
@@ -1582,7 +1582,7 @@ struct dirent *mingw_readdir(DIR *dir)
1582 HANDLE handle;
1583 struct mingw_DIR *mdir = (struct mingw_DIR*)dir;
1584
1585 - if (!dir->dd_handle) {
1585 + if (!dir || !dir->dd_handle) {
1586 errno = EBADF; /* No set_errno for mingw */
1587 return NULL;
1588 }
compat/msvc.c
+27 -1
@@ -5,8 +5,29 @@
5
6 DIR *opendir(const char *name)
7 {
8 - int len = strlen(name);
8 + DWORD attrs = GetFileAttributes(name);
9 + int len;
10 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 +
31 p = malloc(sizeof(DIR) + len + 2);
32 if (!p)
33 return NULL;
@@ -21,6 +42,11 @@ DIR *opendir(const char *name)
42 }
43 int closedir(DIR *dir)
44 {
45 + if (!dir) {
46 + errno = EBADF;
47 + return -1;
48 + }
49 +
50 if (dir->dd_handle != (long)INVALID_HANDLE_VALUE)
51 FindClose((HANDLE)dir->dd_handle);
52 free(dir);