From: Michael Paquier Date: Sat, 4 Aug 2018 20:32:37 +0000 (+0900) Subject: Reset properly errno before calling write() X-Git-Tag: REL9_5_14~7 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=afd5fde856675f54a7b5d857f7a173f9a3237b57;p=postgresql Reset properly errno before calling write() 6cb3372 enforces errno to ENOSPC when less bytes than what is expected have been written when it is unset, though it forgot to properly reset errno before doing a system call to write(), causing errno to potentially come from a previous system call. Reported-by: Tom Lane Author: Michael Paquier Reviewed-by: Tom Lane Discussion: https://postgr.es/m/31797.1533326676@sss.pgh.pa.us --- diff --git a/src/backend/access/heap/rewriteheap.c b/src/backend/access/heap/rewriteheap.c index dd03543b6b..c8fa62daeb 100644 --- a/src/backend/access/heap/rewriteheap.c +++ b/src/backend/access/heap/rewriteheap.c @@ -1164,6 +1164,7 @@ heap_xlog_logical_rewrite(XLogReaderState *r) len = xlrec->num_mappings * sizeof(LogicalRewriteMappingData); /* write out tail end of mapping file (again) */ + errno = 0; if (write(fd, data, len) != len) { /* if write didn't set errno, assume problem is no disk space */ diff --git a/src/backend/access/transam/twophase.c b/src/backend/access/transam/twophase.c index faf590b770..25d1a0d769 100644 --- a/src/backend/access/transam/twophase.c +++ b/src/backend/access/transam/twophase.c @@ -1577,6 +1577,7 @@ RecreateTwoPhaseFile(TransactionId xid, void *content, int len) path))); /* Write content and CRC */ + errno = 0; if (write(fd, content, len) != len) { int save_errno = errno; diff --git a/src/backend/replication/logical/origin.c b/src/backend/replication/logical/origin.c index 4ecd6a9eae..94832a105b 100644 --- a/src/backend/replication/logical/origin.c +++ b/src/backend/replication/logical/origin.c @@ -547,6 +547,7 @@ CheckPointReplicationOrigin(void) tmppath))); /* write magic */ + errno = 0; if ((write(tmpfd, &magic, sizeof(magic))) != sizeof(magic)) { int save_errno = errno; @@ -590,6 +591,7 @@ CheckPointReplicationOrigin(void) /* make sure we only write out a commit that's persistent */ XLogFlush(local_lsn); + errno = 0; if ((write(tmpfd, &disk_state, sizeof(disk_state))) != sizeof(disk_state)) { @@ -612,6 +614,7 @@ CheckPointReplicationOrigin(void) /* write out the CRC */ FIN_CRC32C(crc); + errno = 0; if ((write(tmpfd, &crc, sizeof(crc))) != sizeof(crc)) { int save_errno = errno; diff --git a/src/backend/replication/logical/reorderbuffer.c b/src/backend/replication/logical/reorderbuffer.c index 3606e9ec7f..b73ac5b924 100644 --- a/src/backend/replication/logical/reorderbuffer.c +++ b/src/backend/replication/logical/reorderbuffer.c @@ -2369,6 +2369,7 @@ ReorderBufferSerializeChange(ReorderBuffer *rb, ReorderBufferTXN *txn, ondisk->size = sz; + errno = 0; if (write(fd, rb->outbuf, ondisk->size) != ondisk->size) { int save_errno = errno; diff --git a/src/backend/replication/logical/snapbuild.c b/src/backend/replication/logical/snapbuild.c index 2c96fbd506..7a06fe086f 100644 --- a/src/backend/replication/logical/snapbuild.c +++ b/src/backend/replication/logical/snapbuild.c @@ -1571,6 +1571,7 @@ SnapBuildSerialize(SnapBuild *builder, XLogRecPtr lsn) ereport(ERROR, (errmsg("could not open file \"%s\": %m", path))); + errno = 0; if ((write(fd, ondisk, needed_length)) != needed_length) { int save_errno = errno; diff --git a/src/backend/replication/slot.c b/src/backend/replication/slot.c index a5bc452d81..b42861d625 100644 --- a/src/backend/replication/slot.c +++ b/src/backend/replication/slot.c @@ -1028,6 +1028,7 @@ SaveSlotToPath(ReplicationSlot *slot, const char *dir, int elevel) SnapBuildOnDiskChecksummedSize); FIN_CRC32C(cp.checksum); + errno = 0; if ((write(fd, &cp, sizeof(cp))) != sizeof(cp)) { int save_errno = errno; diff --git a/src/bin/pg_basebackup/receivelog.c b/src/bin/pg_basebackup/receivelog.c index 71f3fb59b7..fb70b204f7 100644 --- a/src/bin/pg_basebackup/receivelog.c +++ b/src/bin/pg_basebackup/receivelog.c @@ -157,6 +157,7 @@ open_walfile(XLogRecPtr startpoint, uint32 timeline, char *basedir, zerobuf = pg_malloc0(XLOG_BLCKSZ); for (bytes = 0; bytes < XLogSegSize; bytes += XLOG_BLCKSZ) { + errno = 0; if (write(f, zerobuf, XLOG_BLCKSZ) != XLOG_BLCKSZ) { /* if write didn't set errno, assume problem is no disk space */ @@ -1217,6 +1218,7 @@ ProcessXLogDataMsg(PGconn *conn, char *copybuf, int len, } } + errno = 0; if (write(walfile, copybuf + hdr_len + bytes_written, bytes_to_write) != bytes_to_write)