diff options
author | Robert Haas <rhaas@postgresql.org> | 2015-02-17 10:19:30 -0500 |
---|---|---|
committer | Robert Haas <rhaas@postgresql.org> | 2015-02-17 11:02:46 -0500 |
commit | 319406c2ac6b6534d82ff76c6f5c544c8483f9af (patch) | |
tree | a35bc25207d22fde5a8a8fc841aab7f3611e4919 /src | |
parent | 6b700301c36e380eb4972ab72c0e914cae60f9fd (diff) |
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
6f03927fce038096f53ca67eeab9adb24938f8a6 introduced logic to better
handle readdir() and closedir() failures, bu it missed these cases.
Extracted from a larger patch by Marco Nenciarini.
Diffstat (limited to 'src')
-rw-r--r-- | src/port/pgcheckdir.c | 10 |
1 files changed, 9 insertions, 1 deletions
diff --git a/src/port/pgcheckdir.c b/src/port/pgcheckdir.c index 36545618e45..c805c599f79 100644 --- a/src/port/pgcheckdir.c +++ b/src/port/pgcheckdir.c @@ -31,6 +31,7 @@ pg_check_dir(const char *dir) int result = 1; DIR *chkdir; struct dirent *file; + int readdir_errno; chkdir = opendir(dir); @@ -58,8 +59,15 @@ pg_check_dir(const char *dir) errno = 0; #endif - if (errno || closedir(chkdir)) + if (errno) 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; } |