8000 Fix fd.c to preserve errno where needed. · sqlparser/postgres@1b192fc · GitHub
[go: up one dir, main page]

Skip to content

Commit 1b192fc

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 e150773 commit 1b192fc

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)
@@ -661,7 +667,7 @@ LruInsert(File file)
661667
if (vfdP->fd < 0)
662668
{
663669
DO_DB(elog(LOG, "RE_OPEN FAILED: %d", errno));
664-
return vfdP->fd;
670+
return -1;
665671
}
666672
else
667673
{
@@ -712,7 +718,7 @@ AllocateVfd(void)
712718
Index i;
713719
File file;
714720

715-
DO_DB(elog(LOG, "AllocateVfd. Size %lu", SizeVfdCache));
721+
DO_DB(elog(LOG, "AllocateVfd. Size %lu", (unsigned long) SizeVfdCache));
716722

717723
Assert(SizeVfdCache > 0); /* InitFileAccess not called? */
718724

@@ -869,8 +875,11 @@ PathNameOpenFile(FileName fileName, int fileFlags, int fileMode)
869875

870876
if (vfdP->fd < 0)
871877
{
878+
int save_errno = errno;
879+
872880
FreeVfd(file);
873881
free(fnamecopy);
882+
errno = save_errno;
874883
return -1;
875884
}
876885
++nfile;

0 commit comments

Comments
 (0)
0