From d7d294f5935e157f239b32c6d1f3d4e923a4eed5 Mon Sep 17 00:00:00 2001 From: Robert Haas Date: Tue, 17 Feb 2015 10:19:30 -0500 Subject: [PATCH] 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. --- src/port/pgcheckdir.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/port/pgcheckdir.c b/src/port/pgcheckdir.c index 3a2a34cbc61..f2fffe56243 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; }