Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 6563fb2

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 e7bfc7e commit 6563fb2

File tree

1 file changed

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

1 file changed

+13
-5
lines changed

src/backend/storage/file/fd.c

+13-5
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,15 @@ int max_safe_fds = 32; /* default if not changed */
128128
/* Debugging.... */
129129

130130
#ifdef FDDEBUG
131-
#define DO_DB(A) A
131+
#define DO_DB(A) \
132+
do { \
133+
int _do_db_save_errno = errno; \
134+
A; \
135+
errno = _do_db_save_errno; \
136+
} while (0)
132137
#else
133-
#define DO_DB(A) /* A */
138+
#define DO_DB(A) \
139+
((void) 0)
134140
#endif
135141

136142
#define VFD_CLOSED (-1)
@@ -703,7 +709,7 @@ LruInsert(File file)
703709
if (vfdP->fd < 0)
704710
{
705711
DO_DB(elog(LOG, "RE_OPEN FAILED: %d", errno));
706-
return vfdP->fd;
712+
return -1;
707713
}
708714
else
709715
{
@@ -754,7 +760,7 @@ AllocateVfd(void)
754760
Index i;
755761
File file;
756762

757-
DO_DB(elog(LOG, "AllocateVfd. Size %lu", SizeVfdCache));
763+
DO_DB(elog(LOG, "AllocateVfd. Size %lu", (unsigned long) SizeVfdCache));
758764

759765
Assert(SizeVfdCache > 0); /* InitFileAccess not called? */
760766

@@ -911,8 +917,11 @@ PathNameOpenFile(FileName fileName, int fileFlags, int fileMode)
911917

912918
if (vfdP->fd < 0)
913919
{
920+
int save_errno = errno;
921+
914922
FreeVfd(file);
915923
free(fnamecopy);
924+
errno = save_errno;
916925
return -1;
917926
}
918927
++nfile;
@@ -1554,7 +1563,6 @@ OpenTransientFile(FileName fileName, int fileFlags, int fileMode)
15541563
{
15551564
int fd;
15561565

1557-
15581566
DO_DB(elog(LOG, "OpenTransientFile: Allocated %d (%s)",
15591567
numAllocatedDescs, fileName));
15601568

0 commit comments

Comments
 (0)