Skip to content

Commit 0ac9f9e

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 65d2b4b commit 0ac9f9e

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

109109
#ifdef FDDEBUG
110-
#define DO_DB(A) A
110+
#define DO_DB(A) \
111+
do { \
112+
int _do_db_save_errno = errno; \
113+
A; \
114+
errno = _do_db_save_errno; \
115+
} while (0)
111116
#else
112-
#define DO_DB(A) /* A */
117+
#define DO_DB(A) \
118+
((void) 0)
113119
#endif
114120

115121
#define VFD_CLOSED (-1)
@@ -643,7 +649,7 @@ LruInsert(File file)
643649
if (vfdP->fd < 0)
644650
{
645651
DO_DB(elog(LOG, "RE_OPEN FAILED: %d", errno));
646-
return vfdP->fd;
652+
return -1;
647653
}
648654
else
649655
{
@@ -694,7 +700,7 @@ AllocateVfd(void)
694700
Index i;
695701
File file;
696702

697-
DO_DB(elog(LOG, "AllocateVfd. Size %lu", SizeVfdCache));
703+
DO_DB(elog(LOG, "AllocateVfd. Size %lu", (unsigned long) SizeVfdCache));
698704

699705
Assert(SizeVfdCache > 0); /* InitFileAccess not called? */
700706

@@ -851,8 +857,11 @@ PathNameOpenFile(FileName fileName, int fileFlags, int fileMode)
851857

852858
if (vfdP->fd < 0)
853859
{
860+
int save_errno = errno;
861+
854862
FreeVfd(file);
855863
free(fnamecopy);
864+
errno = save_errno;
856865
return -1;
857866
}
858867
++nfile;

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy