Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Reset properly errno before calling write()
authorMichael Paquier <michael@paquier.xyz>
Sat, 4 Aug 2018 20:32:44 +0000 (05:32 +0900)
committerMichael Paquier <michael@paquier.xyz>
Sat, 4 Aug 2018 20:32:44 +0000 (05:32 +0900)
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

src/backend/access/heap/rewriteheap.c
src/backend/access/transam/twophase.c
src/backend/replication/logical/reorderbuffer.c
src/backend/replication/logical/snapbuild.c
src/backend/replication/slot.c
src/bin/pg_basebackup/receivelog.c

index 871d9e43d772f87a302ae1b1654c84cf72d2f336..66c4ea0c3e2fb04ed1d59fce51b83fc4aff08835 100644 (file)
@@ -1172,6 +1172,7 @@ heap_xlog_logical_rewrite(XLogRecPtr lsn, XLogRecord *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 */
index c04baf34039f6c81d480e1d9291a8aae8303aba8..c93ef1d5d235b70c11e874abe735be16f319f14c 100644 (file)
@@ -1566,6 +1566,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;
index 7a8b706f2217fab2192b8292715720cc95bb03e6..94fb5b9deb2a78db322ede32073eb37a06fb74a3 100644 (file)
@@ -2296,6 +2296,7 @@ ReorderBufferSerializeChange(ReorderBuffer *rb, ReorderBufferTXN *txn,
 
    ondisk->size = sz;
 
+   errno = 0;
    if (write(fd, rb->outbuf, ondisk->size) != ondisk->size)
    {
        int save_errno = errno;
index 71cc06d46368db37168a45e769206363ae48e5d7..37a3d77a4c907c2b1e324cb8bc1f7a0e80746b98 100644 (file)
@@ -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;
index 661415b9a207520b5fc82e0150e65d549921f080..af47cdab0ebe08d5d56ef0924f85586a5bcf91e2 100644 (file)
@@ -1026,6 +1026,7 @@ SaveSlotToPath(ReplicationSlot *slot, const char *dir, int elevel)
                SnapBuildOnDiskChecksummedSize);
    FIN_CRC32(cp.checksum);
 
+   errno = 0;
    if ((write(fd, &cp, sizeof(cp))) != sizeof(cp))
    {
        int         save_errno = errno;
index da76d9befdad52e6047188f9dc43bbc80d29717d..5e1f3d40496ac4795290f3c546e1b33637efc9d7 100644 (file)
@@ -137,6 +137,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 */
@@ -1094,6 +1095,7 @@ HandleCopyStream(PGconn *conn, XLogRecPtr startpos, uint32 timeline,
                    }
                }
 
+               errno = 0;
                if (write(walfile,
                          copybuf + hdr_len + bytes_written,
                          bytes_to_write) != bytes_to_write)