Skip to content

Commit 1d7a6e3

Browse files
committed
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. Reviewed-by: Michael Paquier <michael@paquier.xyz> Discussion: https://www.postgresql.org/message-id/flat/5de61b6b-8be9-7771-0048-860328efe027%402ndquadrant.com
1 parent 396e4af commit 1d7a6e3

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

src/bin/pg_checksums/pg_checksums.c

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -198,8 +198,12 @@ scan_file(const char *fn, BlockNumber segmentno)
198198
break;
199199
if (r != BLCKSZ)
200200
{
201-
pg_log_error("could not read block %u in file \"%s\": read %d of %d",
202-
blockno, fn, r, BLCKSZ);
201+
if (r < 0)
202+
pg_log_error("could not read block %u in file \"%s\": %m",
203+
blockno, fn);
204+
else
205+
pg_log_error("could not read block %u in file \"%s\": read %d of %d",
206+
blockno, fn, r, BLCKSZ);
203207
exit(1);
204208
}
205209
blocks++;
@@ -222,6 +226,8 @@ scan_file(const char *fn, BlockNumber segmentno)
222226
}
223227
else if (mode == PG_MODE_ENABLE)
224228
{
229+
int w;
230+
225231
/* Set checksum in page header */
226232
header->pd_checksum = csum;
227233

@@ -233,10 +239,15 @@ scan_file(const char *fn, BlockNumber segmentno)
233239
}
234240

235241
/* Write block with checksum */
236-
if (write(f, buf.data, BLCKSZ) != BLCKSZ)
242+
w = write(f, buf.data, BLCKSZ);
243+
if (w != BLCKSZ)
237244
{
238-
pg_log_error("could not write block %u in file \"%s\": %m",
239-
blockno, fn);
245+
if (w < 0)
246+
pg_log_error("could not write block %u in file \"%s\": %m",
247+
blockno, fn);
248+
else
249+
pg_log_error("could not write block %u in file \"%s\": wrote %d of %d",
250+
blockno, fn, w, BLCKSZ);
240251
exit(1);
241252
}
242253
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy