Skip to content

Commit e7e005e

Browse files
committed
Fix fd.c to preserve errno where needed.
PathNameOpenFile failed to ensure that the correct value of errno was returned to its caller after a failure (because it incorrectly supposed that free() can never change errno). In some cases this would result in a user-visible failure because an expected ENOENT errno was replaced with something else. Bogus EINVAL failures have been observed on OS X, for example. There were also a couple of places that could mangle an important value of errno if FDDEBUG was defined. While the usefulness of that debug support is highly debatable, we might as well make it safe to use, so add errno save/restore logic to the DO_DB macro. Per bug #8167 from Nelson Minar, diagnosed by RhodiumToad. Back-patch to all supported branches.
1 parent 630a8af commit e7e005e

File tree

1 file changed

+13
-4
lines changed
  • src/backend/storage/file

1 file changed

+13
-4
lines changed

src/backend/storage/file/fd.c

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,15 @@ int max_safe_fds = 32; /* default if not changed */
109109
/* Debugging.... */
110110

111111
#ifdef FDDEBUG
112-
#define DO_DB(A) A
112+
#define DO_DB(A) \
113+
do { \
114+
int _do_db_save_errno = errno; \
115+
A; \
116+
errno = _do_db_save_errno; \
117+
} while (0)
113118
#else
114-
#define DO_DB(A) /* A */
119+
#define DO_DB(A) \
120+
((void) 0)
115121
#endif
116122

117123
#define VFD_CLOSED (-1)
@@ -674,7 +680,7 @@ LruInsert(File file)
674680
if (vfdP->fd < 0)
675681
{
676682
DO_DB(elog(LOG, "RE_OPEN FAILED: %d", errno));
677-
return vfdP->fd;
683+
return -1;
678684
}
679685
else
680686
{
@@ -725,7 +731,7 @@ AllocateVfd(void)
725731
Index i;
726732
File file;
727733

728-
DO_DB(elog(LOG, "AllocateVfd. Size %lu", SizeVfdCache));
734+
DO_DB(elog(LOG, "AllocateVfd. Size %lu", (unsigned long) SizeVfdCache));
729735

730736
Assert(SizeVfdCache > 0); /* InitFileAccess not called? */
731737

@@ -882,8 +888,11 @@ PathNameOpenFile(FileName fileName, int fileFlags, int fileMode)
882888

883889
if (vfdP->fd < 0)
884890
{
891+
int save_errno = errno;
892+
885893
FreeVfd(file);
886894
free(fnamecopy);
895+
errno = save_errno;
887896
return -1;
888897
}
889898
++nfile;

0 commit comments

Comments
 (0)