postgresql/src/backend
Tom Lane 3f613c6a40 Fix sloppy handling of corner-case errors in fd.c.
Several places in fd.c had badly-thought-through handling of error returns
from lseek() and close().  The fact that those would seldom fail on valid
FDs is probably the reason we've not noticed this up to now; but if they
did fail, we'd get quite confused.

LruDelete and LruInsert actually just Assert'd that lseek never fails,
which is pretty awful on its face.

In LruDelete, we indeed can't throw an error, because that's likely to get
called during error abort and so throwing an error would probably just lead
to an infinite loop.  But by the same token, throwing an error from the
close() right after that was ill-advised, not to mention that it would've
left the LRU state corrupted since we'd already unlinked the VFD from the
list.  I also noticed that really, most of the time, we should know the
current seek position and it shouldn't be necessary to do an lseek here at
all.  As patched, if we don't have a seek position and an lseek attempt
doesn't give us one, we'll close the file but then subsequent re-open
attempts will fail (except in the somewhat-unlikely case that a
FileSeek(SEEK_SET) call comes between and allows us to re-establish a known
target seek position).  This isn't great but it won't result in any state
corruption.

Meanwhile, having an Assert instead of an honest test in LruInsert is
really dangerous: if that lseek failed, a subsequent read or write would
read or write from the start of the file, not where the caller expected,
leading to data corruption.

In both LruDelete and FileClose, if close() fails, just LOG that and mark
the VFD closed anyway.  Possibly leaking an FD is preferable to getting
into an infinite loop or corrupting the VFD list.  Besides, as far as I can
tell from the POSIX spec, it's unspecified whether or not the file has been
closed, so treating it as still open could be the wrong thing anyhow.

I also fixed a number of other places that were being sloppy about
behaving correctly when the seekPos is unknown.

Also, I changed FileSeek to return -1 with EINVAL for the cases where it
detects a bad offset, rather than throwing a hard elog(ERROR).  It seemed
pretty inconsistent that some bad-offset cases would get a failure return
while others got elog(ERROR).  It was missing an offset validity check for
the SEEK_CUR case on a closed file, too.

Back-patch to all supported branches, since all this code is fundamentally
identical in all of them.

Discussion: https://postgr.es/m/2982.1487617365@sss.pgh.pa.us
2017-02-21 17:51:28 -05:00
..
access Fix typos in comments. 2017-02-06 11:34:28 +02:00
bootstrap Protect against multixact members wraparound 2015-04-28 11:32:53 -03:00
catalog Log the creation of an init fork unconditionally. 2016-12-08 14:16:47 -05:00
commands Ignore tablespace ACLs when ignoring schema ACLs. 2017-02-12 16:05:12 -05:00
executor Make sure that hash join's bulk-tuple-transfer loops are interruptible. 2017-02-15 16:40:06 -05:00
foreign Arrange to cache FdwRoutine structs in foreign tables' relcache entries. 2013-03-06 23:48:09 -05:00
lib Misc comment typo fixes. 2014-12-16 16:39:33 +02:00
libpq Fix typos in comments. 2017-02-06 11:34:28 +02:00
main Make fallback implementation of pg_memory_barrier() work in 9.2 and 9.3. 2016-04-16 10:42:07 -04:00
nodes Fix improper repetition of previous results from a hashed aggregate. 2016-08-24 14:37:51 -04:00
optimizer Fix typos in comments. 2017-02-06 11:34:28 +02:00
parser Fix typos in comments. 2017-02-06 11:34:28 +02:00
po Translation updates 2017-02-06 12:35:49 -05:00
port Improve TranslateSocketError() to handle more Windows error codes. 2016-04-21 16:59:08 -04:00
postmaster Fix typos in comments. 2017-02-06 11:34:28 +02:00
regex Suppress compiler warnings about useless comparison of unsigned to zero. 2016-02-15 17:11:52 -05:00
replication Reset hot standby xmin after restart 2017-01-26 20:15:23 +00:00
rewrite Improve error message for rejecting RETURNING clauses with dropped columns. 2016-02-29 19:11:53 -05:00
snowball Fix ancient encoding error in hungarian.stop. 2014-06-10 22:48:39 -04:00
storage Fix sloppy handling of corner-case errors in fd.c. 2017-02-21 17:51:28 -05:00
tcop Add castNode(type, ptr) for safe casting between NodeTag based types. 2017-01-26 16:47:04 -08:00
tsearch Fix typos in comments. 2017-02-06 11:34:28 +02:00
utils Avoid returning stale attribute bitmaps in RelationGetIndexAttrBitmap(). 2017-02-06 13:20:24 -05:00
.gitignore Add gitignore for mingw/cygwin build outputs 2011-06-09 18:11:47 +02:00
common.mk Call check_keywords.pl in maintainer-check 2012-02-27 13:53:12 +02:00
Makefile AIX: Link the postgres executable with -Wl,-brtllib. 2015-07-15 21:00:30 -04:00
nls.mk xlogreader.c: Fix report_invalid_record translatability flag 2015-01-09 12:34:24 -03:00