Skip to content

Commit f5b4bb8

Browse files
committed
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
1 parent 0a60a29 commit f5b4bb8

File tree

7 files changed

+10
-0
lines changed

7 files changed

+10
-0
lines changed

src/backend/access/heap/rewriteheap.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1162,6 +1162,7 @@ heap_xlog_logical_rewrite(XLogReaderState *r)
11621162
len = xlrec->num_mappings * sizeof(LogicalRewriteMappingData);
11631163

11641164
/* write out tail end of mapping file (again) */
1165+
errno = 0;
11651166
if (write(fd, data, len) != len)
11661167
{
11671168
/* if write didn't set errno, assume problem is no disk space */

src/backend/access/transam/twophase.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1553,6 +1553,7 @@ RecreateTwoPhaseFile(TransactionId xid, void *content, int len)
15531553
path)));
15541554

15551555
/* Write content and CRC */
1556+
errno = 0;
15561557
if (write(fd, content, len) != len)
15571558
{
15581559
int save_errno = errno;

src/backend/replication/logical/origin.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,7 @@ CheckPointReplicationOrigin(void)
547547
tmppath)));
548548

549549
/* write magic */
550+
errno = 0;
550551
if ((write(tmpfd, &magic, sizeof(magic))) != sizeof(magic))
551552
{
552553
int save_errno = errno;
@@ -590,6 +591,7 @@ CheckPointReplicationOrigin(void)
590591
/* make sure we only write out a commit that's persistent */
591592
XLogFlush(local_lsn);
592593

594+
errno = 0;
593595
if ((write(tmpfd, &disk_state, sizeof(disk_state))) !=
594596
sizeof(disk_state))
595597
{
@@ -612,6 +614,7 @@ CheckPointReplicationOrigin(void)
612614

613615
/* write out the CRC */
614616
FIN_CRC32C(crc);
617+
errno = 0;
615618
if ((write(tmpfd, &crc, sizeof(crc))) != sizeof(crc))
616619
{
617620
int save_errno = errno;

src/backend/replication/logical/reorderbuffer.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2479,6 +2479,7 @@ ReorderBufferSerializeChange(ReorderBuffer *rb, ReorderBufferTXN *txn,
24792479

24802480
ondisk->size = sz;
24812481

2482+
errno = 0;
24822483
if (write(fd, rb->outbuf, ondisk->size) != ondisk->size)
24832484
{
24842485
int save_errno = errno;

src/backend/replication/logical/snapbuild.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1588,6 +1588,7 @@ SnapBuildSerialize(SnapBuild *builder, XLogRecPtr lsn)
15881588
ereport(ERROR,
15891589
(errmsg("could not open file \"%s\": %m", path)));
15901590

1591+
errno = 0;
15911592
if ((write(fd, ondisk, needed_length)) != needed_length)
15921593
{
15931594
int save_errno = errno;

src/backend/replication/slot.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1080,6 +1080,7 @@ SaveSlotToPath(ReplicationSlot *slot, const char *dir, int elevel)
10801080
SnapBuildOnDiskChecksummedSize);
10811081
FIN_CRC32C(cp.checksum);
10821082

1083+
errno = 0;
10831084
if ((write(fd, &cp, sizeof(cp))) != sizeof(cp))
10841085
{
10851086
int save_errno = errno;

src/bin/pg_basebackup/receivelog.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ open_walfile(StreamCtl *stream, XLogRecPtr startpoint)
147147
zerobuf = pg_malloc0(XLOG_BLCKSZ);
148148
for (bytes = 0; bytes < XLogSegSize; bytes += XLOG_BLCKSZ)
149149
{
150+
errno = 0;
150151
if (write(f, zerobuf, XLOG_BLCKSZ) != XLOG_BLCKSZ)
151152
{
152153
/* if write didn't set errno, assume problem is no disk space */
@@ -1189,6 +1190,7 @@ ProcessXLogDataMsg(PGconn *conn, StreamCtl *stream, char *copybuf, int len,
11891190
}
11901191
}
11911192

1193+
errno = 0;
11921194
if (write(walfile,
11931195
copybuf + hdr_len + bytes_written,
11941196
bytes_to_write) != bytes_to_write)

0 commit comments

Comments
 (0)