Skip to content

Commit 319406c

Browse files
committed
Improve pg_check_dir's handling of closedir() failures.
Avoid losing errno if readdir() fails and closedir() works. This also avoids leaking the directory handle when readdir() fails. Commit 6f03927 introduced logic to better handle readdir() and closedir() failures, bu it missed these cases. Extracted from a larger patch by Marco Nenciarini.
1 parent 6b70030 commit 319406c

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

src/port/pgcheckdir.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ pg_check_dir(const char *dir)
3131
int result = 1;
3232
DIR *chkdir;
3333
struct dirent *file;
34+
int readdir_errno;
3435

3536
chkdir = opendir(dir);
3637

@@ -58,8 +59,15 @@ pg_check_dir(const char *dir)
5859
errno = 0;
5960
#endif
6061

61-
if (errno || closedir(chkdir))
62+
if (errno)
6263
result = -1; /* some kind of I/O error? */
6364

65+
/* Close chkdir and avoid overwriting the readdir errno on success */
66+
readdir_errno = errno;
67+
if (closedir(chkdir))
68+
result = -1; /* error executing closedir */
69+
else
70+
errno = readdir_errno;
71+
6472
return result;
6573
}

0 commit comments

Comments
 (0)