1
0
mirror of https://github.com/postgres/postgres.git synced 2025-07-02 09:02:37 +03:00

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
6f03927fce introduced logic to better
handle readdir() and closedir() failures, bu it missed these cases.

Extracted from a larger patch by Marco Nenciarini.
This commit is contained in:
Robert Haas
2015-02-17 10:19:30 -05:00
parent 6b700301c3
commit 319406c2ac

View File

@ -31,6 +31,7 @@ pg_check_dir(const char *dir)
int result = 1; int result = 1;
DIR *chkdir; DIR *chkdir;
struct dirent *file; struct dirent *file;
int readdir_errno;
chkdir = opendir(dir); chkdir = opendir(dir);
@ -58,8 +59,15 @@ pg_check_dir(const char *dir)
errno = 0; errno = 0;
#endif #endif
if (errno || closedir(chkdir)) if (errno)
result = -1; /* some kind of I/O error? */ result = -1; /* some kind of I/O error? */
/* Close chkdir and avoid overwriting the readdir errno on success */
readdir_errno = errno;
if (closedir(chkdir))
result = -1; /* error executing closedir */
else
errno = readdir_errno;
return result; return result;
} }