Skip to content

Commit 9d8bc2f

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 ee24949 commit 9d8bc2f

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
@@ -108,9 +108,15 @@ static int max_safe_fds = 32; /* default if not changed */
108108
/* Debugging.... */
109109

110110
#ifdef FDDEBUG
111-
#define DO_DB(A) A
111+
#define DO_DB(A) \
112+
do { \
113+
int _do_db_save_errno = errno; \
114+
A; \
115+
errno = _do_db_save_errno; \
116+
} while (0)
112117
#else
113-
#define DO_DB(A) /* A */
118+
#define DO_DB(A) \
119+
((void) 0)
114120
#endif
115121

116122
#define VFD_CLOSED (-1)
@@ -664,7 +670,7 @@ LruInsert(File file)
664670
if (vfdP->fd < 0)
665671
{
666672
DO_DB(elog(LOG, "RE_OPEN FAILED: %d", errno));
667-
return vfdP->fd;
673+
return -1;
668674
}
669675
else
670676
{
@@ -715,7 +721,7 @@ AllocateVfd(void)
715721
Index i;
716722
File file;
717723

718-
DO_DB(elog(LOG, "AllocateVfd. Size %lu", SizeVfdCache));
724+
DO_DB(elog(LOG, "AllocateVfd. Size %lu", (unsigned long) SizeVfdCache));
719725

720726
Assert(SizeVfdCache > 0); /* InitFileAccess not called? */
721727

@@ -872,8 +878,11 @@ PathNameOpenFile(FileName fileName, int fileFlags, int fileMode)
872878

873879
if (vfdP->fd < 0)
874880
{
881+
int save_errno = errno;
882+
875883
FreeVfd(file);
876884
free(fnamecopy);
885+
errno = save_errno;
877886
return -1;
878887
}
879888
++nfile;

0 commit comments

Comments
 (0)