From 89fe3678ef9443fc56a8ebfc3657ad06f5585bf5 Mon Sep 17 00:00:00 2001 From: Peter Eisentraut Date: Mon, 26 Aug 2019 21:37:50 +0200 Subject: [PATCH v2 2/2] pg_checksums: Handle read and write returns correctly The read() return was not checking for errors, the write() return was not checking for short writes. --- src/bin/pg_checksums/pg_checksums.c | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/src/bin/pg_checksums/pg_checksums.c b/src/bin/pg_checksums/pg_checksums.c index 8c00ec9a3b..40619be0fa 100644 --- a/src/bin/pg_checksums/pg_checksums.c +++ b/src/bin/pg_checksums/pg_checksums.c @@ -196,7 +196,13 @@ scan_file(const char *fn, BlockNumber segmentno) if (r == 0) break; - if (r != BLCKSZ) + else if (r < 0) + { + pg_log_error("could not read block %u in file \"%s\": %m", + blockno, fn); + exit(1); + } + else if (r != BLCKSZ) { pg_log_error("could not read block %u in file \"%s\": read %d of %d", blockno, fn, r, BLCKSZ); @@ -222,6 +228,8 @@ scan_file(const char *fn, BlockNumber segmentno) } else if (mode == PG_MODE_ENABLE) { + int w; + /* Set checksum in page header */ header->pd_checksum = csum; @@ -233,10 +241,15 @@ scan_file(const char *fn, BlockNumber segmentno) } /* Write block with checksum */ - if (write(f, buf.data, BLCKSZ) != BLCKSZ) + w = write(f, buf.data, BLCKSZ); + if (w != BLCKSZ) { - pg_log_error("could not write block %u in file \"%s\": %m", - blockno, fn); + if (w < 0) + pg_log_error("could not write block %u in file \"%s\": %m", + blockno, fn); + else + pg_log_error("could not write block %u in file \"%s\": wrote %d of %d", + blockno, fn, w, BLCKSZ); exit(1); } } -- 2.22.0