From: Peter Eisentraut Date: Wed, 15 Jul 2026 08:58:13 +0000 (+0200) Subject: Clean up write() return type X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=1f8c504e3086cf70a1538038b63b045de5df6a9b;p=thirdparty%2Fpostgresql.git Clean up write() return type and analogously for pg_pwrite() and FileWrite() Be sure to store the return value in a variable of type ssize_t, not int. Some callers of FileWrite() did not have a separate error message for a short write. This is okay in practice because FileWriteV() sets ENOSPC for all non-error returns, so you'll get a reasonable error message either way. But callers handled this inconsistently, and this behavior isn't really prominently documented and commit 871fe4917e1 seems to frown upon it, so it seems better to make all callers handle this consistently by adding the separate error message. Reviewed-by: Heikki Linnakangas Discussion: https://www.postgresql.org/message-id/flat/f9aab072-0078-49e4-ab93-3b08086a4406@eisentraut.org --- diff --git a/src/backend/access/heap/rewriteheap.c b/src/backend/access/heap/rewriteheap.c index 0ccd392c3dc..0648e433031 100644 --- a/src/backend/access/heap/rewriteheap.c +++ b/src/backend/access/heap/rewriteheap.c @@ -829,8 +829,8 @@ logical_heap_rewrite_flush_mappings(RewriteState state) char *waldata_start; xl_heap_rewrite_mapping xlrec; Oid dboid; - uint32 len; - int written; + size_t len; + ssize_t written; uint32 num_mappings = dclist_count(&src->mappings); /* this file hasn't got any new mappings */ @@ -882,10 +882,14 @@ logical_heap_rewrite_flush_mappings(RewriteState state) */ written = FileWrite(src->vfd, waldata_start, len, src->off, WAIT_EVENT_LOGICAL_REWRITE_WRITE); - if (written != len) + if (written < 0) ereport(ERROR, (errcode_for_file_access(), - errmsg("could not write to file \"%s\", wrote %d of %d: %m", src->path, + errmsg("could not write to file \"%s\": %m", src->path))); + else if (written != len) + ereport(ERROR, + (errcode_for_file_access(), + errmsg("could not write to file \"%s\": wrote %zd of %zu", src->path, written, len))); src->off += len; @@ -1078,7 +1082,7 @@ heap_xlog_logical_rewrite(XLogReaderState *r) char path[MAXPGPATH]; int fd; xl_heap_rewrite_mapping *xlrec; - uint32 len; + size_t len; char *data; xlrec = (xl_heap_rewrite_mapping *) XLogRecGetData(r); diff --git a/src/backend/access/transam/timeline.c b/src/backend/access/transam/timeline.c index db101b761ee..d80c8ffe0a7 100644 --- a/src/backend/access/transam/timeline.c +++ b/src/backend/access/transam/timeline.c @@ -461,7 +461,7 @@ writeTimeLineHistory(TimeLineID newTLI, TimeLineID parentTLI, * to avoid emplacing a bogus file. */ void -writeTimeLineHistoryFile(TimeLineID tli, const char *content, int size) +writeTimeLineHistoryFile(TimeLineID tli, const char *content, size_t size) { char path[MAXPGPATH]; char tmppath[MAXPGPATH]; @@ -483,7 +483,7 @@ writeTimeLineHistoryFile(TimeLineID tli, const char *content, int size) errno = 0; pgstat_report_wait_start(WAIT_EVENT_TIMELINE_HISTORY_FILE_WRITE); - if ((int) write(fd, content, size) != size) + if (write(fd, content, size) != size) { int save_errno = errno; diff --git a/src/backend/access/transam/twophase.c b/src/backend/access/transam/twophase.c index 69c12226b7b..439e28c9987 100644 --- a/src/backend/access/transam/twophase.c +++ b/src/backend/access/transam/twophase.c @@ -232,7 +232,7 @@ static void ProcessRecords(char *bufptr, FullTransactionId fxid, const TwoPhaseCallback callbacks[]); static void RemoveGXact(GlobalTransaction gxact); -static void XlogReadTwoPhaseData(XLogRecPtr lsn, char **buf, int *len); +static void XlogReadTwoPhaseData(XLogRecPtr lsn, char **buf, size_t *len); static char *ProcessTwoPhaseBuffer(FullTransactionId fxid, XLogRecPtr prepare_start_lsn, bool fromdisk, bool setParent, bool setNextXid); @@ -240,7 +240,7 @@ static void MarkAsPreparingGuts(GlobalTransaction gxact, FullTransactionId fxid, const char *gid, TimestampTz prepared_at, Oid owner, Oid databaseid); static void RemoveTwoPhaseFile(FullTransactionId fxid, bool giveWarning); -static void RecreateTwoPhaseFile(FullTransactionId fxid, const void *content, int len); +static void RecreateTwoPhaseFile(FullTransactionId fxid, const void *content, size_t len); /* * Register shared memory for two-phase state. @@ -1417,7 +1417,7 @@ ReadTwoPhaseFile(FullTransactionId fxid, bool missing_ok) * similarly to the way WALSender or Logical Decoding would do. */ static void -XlogReadTwoPhaseData(XLogRecPtr lsn, char **buf, int *len) +XlogReadTwoPhaseData(XLogRecPtr lsn, char **buf, size_t *len) { XLogRecord *record; XLogReaderState *xlogreader; @@ -1747,7 +1747,7 @@ RemoveTwoPhaseFile(FullTransactionId fxid, bool giveWarning) * Note: content and len don't include CRC. */ static void -RecreateTwoPhaseFile(FullTransactionId fxid, const void *content, int len) +RecreateTwoPhaseFile(FullTransactionId fxid, const void *content, size_t len) { char path[MAXPGPATH]; pg_crc32c statefile_crc; @@ -1867,7 +1867,7 @@ CheckPointTwoPhase(XLogRecPtr redo_horizon) gxact->prepare_end_lsn <= redo_horizon) { char *buf; - int len; + size_t len; XlogReadTwoPhaseData(gxact->prepare_start_lsn, &buf, &len); RecreateTwoPhaseFile(gxact->fxid, buf, len); diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index 29169236896..f8b939853e9 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -3541,7 +3541,7 @@ XLogFileCopy(TimeLineID destTLI, XLogSegNo destsegno, } errno = 0; pgstat_report_wait_start(WAIT_EVENT_WAL_COPY_WRITE); - if ((int) write(fd, buffer.data, sizeof(buffer)) != (int) sizeof(buffer)) + if (write(fd, buffer.data, sizeof(buffer)) != sizeof(buffer)) { int save_errno = errno; diff --git a/src/backend/backup/basebackup_server.c b/src/backend/backup/basebackup_server.c index 3d44bf71d19..b2e89f09d30 100644 --- a/src/backend/backup/basebackup_server.c +++ b/src/backend/backup/basebackup_server.c @@ -160,7 +160,7 @@ static void bbsink_server_archive_contents(bbsink *sink, size_t len) { bbsink_server *mysink = (bbsink_server *) sink; - int nbytes; + ssize_t nbytes; nbytes = FileWrite(mysink->file, mysink->base.bbs_buffer, len, mysink->filepos, WAIT_EVENT_BASEBACKUP_WRITE); @@ -176,7 +176,7 @@ bbsink_server_archive_contents(bbsink *sink, size_t len) /* short write: complain appropriately */ ereport(ERROR, (errcode(ERRCODE_DISK_FULL), - errmsg("could not write file \"%s\": wrote only %d of %zu bytes at offset %lld", + errmsg("could not write file \"%s\": wrote only %zd of %zu bytes at offset %lld", FilePathName(mysink->file), nbytes, len, (long long) mysink->filepos), errhint("Check free disk space."))); @@ -253,7 +253,7 @@ static void bbsink_server_manifest_contents(bbsink *sink, size_t len) { bbsink_server *mysink = (bbsink_server *) sink; - int nbytes; + ssize_t nbytes; nbytes = FileWrite(mysink->file, mysink->base.bbs_buffer, len, mysink->filepos, WAIT_EVENT_BASEBACKUP_WRITE); @@ -269,7 +269,7 @@ bbsink_server_manifest_contents(bbsink *sink, size_t len) /* short write: complain appropriately */ ereport(ERROR, (errcode(ERRCODE_DISK_FULL), - errmsg("could not write file \"%s\": wrote only %d of %zu bytes at offset %lld", + errmsg("could not write file \"%s\": wrote only %zd of %zu bytes at offset %lld", FilePathName(mysink->file), nbytes, len, (long long) mysink->filepos), errhint("Check free disk space."))); diff --git a/src/backend/commands/dbcommands.c b/src/backend/commands/dbcommands.c index f0819d15ab7..51dcbd9cace 100644 --- a/src/backend/commands/dbcommands.c +++ b/src/backend/commands/dbcommands.c @@ -460,7 +460,7 @@ static void CreateDirAndVersionFile(char *dbpath, Oid dbid, Oid tsid, bool isRedo) { int fd; - int nbytes; + size_t nbytes; char versionfile[MAXPGPATH]; char buf[16]; @@ -500,7 +500,7 @@ CreateDirAndVersionFile(char *dbpath, Oid dbid, Oid tsid, bool isRedo) /* Write PG_MAJORVERSION in the PG_VERSION file. */ pgstat_report_wait_start(WAIT_EVENT_VERSION_FILE_WRITE); errno = 0; - if ((int) write(fd, buf, nbytes) != nbytes) + if (write(fd, buf, nbytes) != nbytes) { /* If write didn't set errno, assume problem is no disk space. */ if (errno == 0) diff --git a/src/backend/libpq/be-fsstubs.c b/src/backend/libpq/be-fsstubs.c index 1aa4a3080f5..10cc05d5e4b 100644 --- a/src/backend/libpq/be-fsstubs.c +++ b/src/backend/libpq/be-fsstubs.c @@ -488,8 +488,7 @@ be_lo_export(PG_FUNCTION_ARGS) Oid lobjId = PG_GETARG_OID(0); text *filename = PG_GETARG_TEXT_PP(1); int fd; - int nbytes, - tmp; + int nbytes; char buf[BUFSIZE]; char fnamebuf[MAXPGPATH]; LargeObjectDesc *lobj; @@ -531,6 +530,8 @@ be_lo_export(PG_FUNCTION_ARGS) */ while ((nbytes = inv_read(lobj, buf, BUFSIZE)) > 0) { + ssize_t tmp; + tmp = write(fd, buf, nbytes); if (tmp != nbytes) ereport(ERROR, diff --git a/src/backend/postmaster/fork_process.c b/src/backend/postmaster/fork_process.c index 855c1a9abda..c6b0c41c6d4 100644 --- a/src/backend/postmaster/fork_process.c +++ b/src/backend/postmaster/fork_process.c @@ -102,7 +102,7 @@ fork_process(void) if (fd >= 0) { const char *oomvalue = getenv("PG_OOM_ADJUST_VALUE"); - int rc; + ssize_t rc; if (oomvalue == NULL) /* supply a useful default */ oomvalue = "0"; diff --git a/src/backend/replication/libpqwalreceiver/libpqwalreceiver.c b/src/backend/replication/libpqwalreceiver/libpqwalreceiver.c index 5376519fea5..ff76469bcf2 100644 --- a/src/backend/replication/libpqwalreceiver/libpqwalreceiver.c +++ b/src/backend/replication/libpqwalreceiver/libpqwalreceiver.c @@ -70,7 +70,7 @@ static char *libpqrcv_get_option_from_conninfo(const char *connInfo, static int libpqrcv_server_version(WalReceiverConn *conn); static void libpqrcv_readtimelinehistoryfile(WalReceiverConn *conn, TimeLineID tli, char **filename, - char **content, int *len); + char **content, size_t *len); static bool libpqrcv_startstreaming(WalReceiverConn *conn, const WalRcvStreamOptions *options); static void libpqrcv_endstreaming(WalReceiverConn *conn, @@ -738,7 +738,7 @@ libpqrcv_endstreaming(WalReceiverConn *conn, TimeLineID *next_tli) static void libpqrcv_readtimelinehistoryfile(WalReceiverConn *conn, TimeLineID tli, char **filename, - char **content, int *len) + char **content, size_t *len) { PGresult *res; char cmd[64]; diff --git a/src/backend/replication/walreceiver.c b/src/backend/replication/walreceiver.c index 05e2f690fa7..429a1b2d96d 100644 --- a/src/backend/replication/walreceiver.c +++ b/src/backend/replication/walreceiver.c @@ -755,7 +755,7 @@ WalRcvFetchTimeLineHistoryFiles(TimeLineID first, TimeLineID last) { char *fname; char *content; - int len; + size_t len; char expectedfname[MAXFNAMELEN]; ereport(LOG, @@ -913,7 +913,7 @@ static void XLogWalRcvWrite(char *buf, Size nbytes, XLogRecPtr recptr, TimeLineID tli) { int startoff; - int byteswritten; + ssize_t byteswritten; instr_time start; Assert(tli != 0); diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index b44d24cdbd3..5c59913646b 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -498,7 +498,6 @@ static void BufFileDumpBuffer(BufFile *file) { int64 wpos = 0; - int64 bytestowrite; File thisfile; /* @@ -510,6 +509,8 @@ BufFileDumpBuffer(BufFile *file) int64 availbytes; instr_time io_start; instr_time io_time; + size_t bytestowrite; + ssize_t rc; /* * Advance to next component file if necessary and possible. @@ -538,12 +539,12 @@ BufFileDumpBuffer(BufFile *file) else INSTR_TIME_SET_ZERO(io_start); - bytestowrite = FileWrite(thisfile, - file->buffer.data + wpos, - bytestowrite, - file->curOffset, - WAIT_EVENT_BUFFILE_WRITE); - if (bytestowrite <= 0) + rc = FileWrite(thisfile, + file->buffer.data + wpos, + bytestowrite, + file->curOffset, + WAIT_EVENT_BUFFILE_WRITE); + if (rc <= 0) ereport(ERROR, (errcode_for_file_access(), errmsg("could not write to file \"%s\": %m", @@ -555,8 +556,8 @@ BufFileDumpBuffer(BufFile *file) INSTR_TIME_ACCUM_DIFF(pgBufferUsage.temp_blk_write_time, io_time, io_start); } - file->curOffset += bytestowrite; - wpos += bytestowrite; + file->curOffset += rc; + wpos += rc; pgBufferUsage.temp_blks_written++; } diff --git a/src/backend/storage/ipc/waiteventset.c b/src/backend/storage/ipc/waiteventset.c index 880c05dd647..5c807c3b274 100644 --- a/src/backend/storage/ipc/waiteventset.c +++ b/src/backend/storage/ipc/waiteventset.c @@ -1905,7 +1905,7 @@ latch_sigurg_handler(SIGNAL_ARGS) static void sendSelfPipeByte(void) { - int rc; + ssize_t rc; char dummy = 0; retry: diff --git a/src/backend/storage/smgr/md.c b/src/backend/storage/smgr/md.c index 79febf12de3..9f96d9cbbfc 100644 --- a/src/backend/storage/smgr/md.c +++ b/src/backend/storage/smgr/md.c @@ -488,7 +488,7 @@ mdextend(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, const void *buffer, bool skipFsync) { pgoff_t seekpos; - int nbytes; + ssize_t nbytes; MdfdVec *v; /* If this build supports direct I/O, the buffer must be I/O aligned. */ @@ -530,9 +530,9 @@ mdextend(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, /* short write: complain appropriately */ ereport(ERROR, (errcode(ERRCODE_DISK_FULL), - errmsg("could not extend file \"%s\": wrote only %d of %d bytes at block %u", + errmsg("could not extend file \"%s\": wrote only %zd of %zu bytes at block %u", FilePathName(v->mdfd_vfd), - nbytes, BLCKSZ, blocknum), + nbytes, (size_t) BLCKSZ, blocknum), errhint("Check free disk space."))); } @@ -1080,7 +1080,7 @@ mdwritev(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, struct iovec iov[PG_IOV_MAX]; int iovcnt; pgoff_t seekpos; - int nbytes; + ssize_t nbytes; MdfdVec *v; BlockNumber nblocks_this_segment; size_t transferred_this_segment; diff --git a/src/backend/utils/error/elog.c b/src/backend/utils/error/elog.c index a6936a0c664..b9d2c96b97a 100644 --- a/src/backend/utils/error/elog.c +++ b/src/backend/utils/error/elog.c @@ -3027,7 +3027,7 @@ write_eventlog(int level, const char *line, int len) static void write_console(const char *line, int len) { - int rc; + ssize_t rc; #ifdef WIN32 @@ -3917,7 +3917,7 @@ write_pipe_chunks(char *data, int len, int dest) { PipeProtoChunk p; int fd = fileno(stderr); - int rc; + ssize_t rc; Assert(len > 0); diff --git a/src/backend/utils/init/miscinit.c b/src/backend/utils/init/miscinit.c index 263ae7b8d86..eddce1ce33f 100644 --- a/src/backend/utils/init/miscinit.c +++ b/src/backend/utils/init/miscinit.c @@ -1522,7 +1522,8 @@ void AddToDataDirLockFile(int target_line, const char *str) { int fd; - ssize_t len; + ssize_t nread; + size_t len; int lineno; char *srcptr; char *destptr; @@ -1539,9 +1540,9 @@ AddToDataDirLockFile(int target_line, const char *str) return; } pgstat_report_wait_start(WAIT_EVENT_LOCK_FILE_ADDTODATADIR_READ); - len = read(fd, srcbuffer, sizeof(srcbuffer) - 1); + nread = read(fd, srcbuffer, sizeof(srcbuffer) - 1); pgstat_report_wait_end(); - if (len < 0) + if (nread < 0) { ereport(LOG, (errcode_for_file_access(), @@ -1550,7 +1551,7 @@ AddToDataDirLockFile(int target_line, const char *str) close(fd); return; } - srcbuffer[len] = '\0'; + srcbuffer[nread] = '\0'; /* * Advance over lines we are not supposed to rewrite, then copy them to diff --git a/src/backend/utils/probes.d b/src/backend/utils/probes.d index 48b269468a5..2d8f12cbe30 100644 --- a/src/backend/utils/probes.d +++ b/src/backend/utils/probes.d @@ -85,7 +85,7 @@ provider postgresql { probe smgr__md__read__start(ForkNumber, BlockNumber, Oid, Oid, Oid, int); probe smgr__md__read__done(ForkNumber, BlockNumber, Oid, Oid, Oid, int, long long int, long long int); probe smgr__md__write__start(ForkNumber, BlockNumber, Oid, Oid, Oid, int); - probe smgr__md__write__done(ForkNumber, BlockNumber, Oid, Oid, Oid, int, int, int); + probe smgr__md__write__done(ForkNumber, BlockNumber, Oid, Oid, Oid, int, long long int, long long int); probe wal__insert(unsigned char, unsigned char); probe wal__switch(); diff --git a/src/bin/pg_basebackup/pg_recvlogical.c b/src/bin/pg_basebackup/pg_recvlogical.c index bc95e4d0735..40f6f65f757 100644 --- a/src/bin/pg_basebackup/pg_recvlogical.c +++ b/src/bin/pg_basebackup/pg_recvlogical.c @@ -292,10 +292,10 @@ StreamLogicalLog(void) while (!time_to_abort) { int r; - int bytes_left; - int bytes_written; + size_t bytes_left; + size_t bytes_written; TimestampTz now; - int hdr_len; + size_t hdr_len; cur_record_lsn = InvalidXLogRecPtr; @@ -560,7 +560,7 @@ StreamLogicalLog(void) while (bytes_left) { - int ret; + ssize_t ret; ret = write(outfd, copybuf + hdr_len + bytes_written, @@ -568,7 +568,7 @@ StreamLogicalLog(void) if (ret < 0) { - pg_log_error("could not write %d bytes to log file \"%s\": %m", + pg_log_error("could not write %zu bytes to log file \"%s\": %m", bytes_left, outfile); goto error; } @@ -580,8 +580,8 @@ StreamLogicalLog(void) if (write(outfd, "\n", 1) != 1) { - pg_log_error("could not write %d bytes to log file \"%s\": %m", - 1, outfile); + pg_log_error("could not write %zu bytes to log file \"%s\": %m", + (size_t) 1, outfile); goto error; } diff --git a/src/bin/pg_basebackup/receivelog.c b/src/bin/pg_basebackup/receivelog.c index 4925e992741..faa60711b1b 100644 --- a/src/bin/pg_basebackup/receivelog.c +++ b/src/bin/pg_basebackup/receivelog.c @@ -274,7 +274,7 @@ existsTimeLineHistoryFile(StreamCtl *stream) static bool writeTimeLineHistoryFile(StreamCtl *stream, const char *filename, const char *content) { - int size = strlen(content); + size_t size = strlen(content); char histfname[MAXFNAMELEN]; Walfile *f; @@ -299,7 +299,7 @@ writeTimeLineHistoryFile(StreamCtl *stream, const char *filename, const char *co return false; } - if ((int) stream->walmethod->ops->write(f, content, size) != size) + if (stream->walmethod->ops->write(f, content, size) != size) { pg_log_error("could not write timeline history file \"%s\": %s", histfname, GetLastWalMethodError(stream->walmethod)); diff --git a/src/bin/pg_checksums/pg_checksums.c b/src/bin/pg_checksums/pg_checksums.c index 412c9a18f95..3b3ae23f1a6 100644 --- a/src/bin/pg_checksums/pg_checksums.c +++ b/src/bin/pg_checksums/pg_checksums.c @@ -237,7 +237,7 @@ scan_file(const char *fn, int segmentno) } else if (mode == PG_MODE_ENABLE) { - int w; + ssize_t w; /* * Do not rewrite if the checksum is already set to the expected @@ -263,8 +263,8 @@ scan_file(const char *fn, int segmentno) pg_fatal("could not write block %u in file \"%s\": %m", blockno, fn); else - pg_fatal("could not write block %u in file \"%s\": wrote %d of %d", - blockno, fn, w, BLCKSZ); + pg_fatal("could not write block %u in file \"%s\": wrote %zd of %zu", + blockno, fn, w, (size_t) BLCKSZ); } } diff --git a/src/bin/pg_combinebackup/backup_label.c b/src/bin/pg_combinebackup/backup_label.c index c7f51f0f8c7..b757e772b92 100644 --- a/src/bin/pg_combinebackup/backup_label.c +++ b/src/bin/pg_combinebackup/backup_label.c @@ -151,18 +151,19 @@ write_backup_label(char *output_directory, StringInfo buf, if (!line_starts_with(s, e, "INCREMENTAL FROM LSN: ", NULL) && !line_starts_with(s, e, "INCREMENTAL FROM TLI: ", NULL)) { + const size_t bytes_left = e - s; ssize_t wb; - wb = write(output_fd, s, e - s); - if (wb != e - s) + wb = write(output_fd, s, bytes_left); + if (wb != bytes_left) { if (wb < 0) pg_fatal("could not write file \"%s\": %m", output_filename); else - pg_fatal("could not write file \"%s\": wrote %d of %d", - output_filename, (int) wb, (int) (e - s)); + pg_fatal("could not write file \"%s\": wrote %zd of %zu", + output_filename, wb, bytes_left); } - if (pg_checksum_update(&checksum_ctx, (uint8 *) s, e - s) < 0) + if (pg_checksum_update(&checksum_ctx, (uint8 *) s, bytes_left) < 0) pg_fatal("could not update checksum of file \"%s\"", output_filename); } diff --git a/src/bin/pg_combinebackup/copy_file.c b/src/bin/pg_combinebackup/copy_file.c index 0287d6e87df..740b63be559 100644 --- a/src/bin/pg_combinebackup/copy_file.c +++ b/src/bin/pg_combinebackup/copy_file.c @@ -179,7 +179,7 @@ copy_file_blocks(const char *src, const char *dst, uint8 *buffer; const int buffer_size = 50 * BLCKSZ; ssize_t rb; - unsigned offset = 0; + size_t offset = 0; if ((src_fd = open(src, O_RDONLY | PG_BINARY, 0)) < 0) pg_fatal("could not open file \"%s\": %m", src); @@ -199,8 +199,8 @@ copy_file_blocks(const char *src, const char *dst, if (wb < 0) pg_fatal("could not write to file \"%s\": %m", dst); else - pg_fatal("could not write to file \"%s\", offset %u: wrote %d of %d", - dst, offset, (int) wb, (int) rb); + pg_fatal("could not write to file \"%s\", offset %zu: wrote %zd of %zu", + dst, offset, wb, (size_t) rb); } if (pg_checksum_update(checksum_ctx, buffer, rb) < 0) diff --git a/src/bin/pg_combinebackup/reconstruct.c b/src/bin/pg_combinebackup/reconstruct.c index 3c377b749f5..e4b02746127 100644 --- a/src/bin/pg_combinebackup/reconstruct.c +++ b/src/bin/pg_combinebackup/reconstruct.c @@ -755,15 +755,15 @@ static void write_block(int fd, const char *output_filename, const uint8 *buffer, pg_checksum_context *checksum_ctx) { - int wb; + ssize_t wb; if ((wb = write(fd, buffer, BLCKSZ)) != BLCKSZ) { if (wb < 0) pg_fatal("could not write file \"%s\": %m", output_filename); else - pg_fatal("could not write file \"%s\": wrote %d of %d", - output_filename, wb, BLCKSZ); + pg_fatal("could not write file \"%s\": wrote %zd of %zu", + output_filename, wb, (size_t) BLCKSZ); } /* Update the checksum computation. */ diff --git a/src/bin/pg_combinebackup/write_manifest.c b/src/bin/pg_combinebackup/write_manifest.c index 715286043b5..c2ab7281266 100644 --- a/src/bin/pg_combinebackup/write_manifest.c +++ b/src/bin/pg_combinebackup/write_manifest.c @@ -259,8 +259,8 @@ flush_manifest(manifest_writer *mwriter) if (wb < 0) pg_fatal("could not write file \"%s\": %m", mwriter->pathname); else - pg_fatal("could not write file \"%s\": wrote %zd of %d", - mwriter->pathname, wb, mwriter->buf.len); + pg_fatal("could not write file \"%s\": wrote %zd of %zu", + mwriter->pathname, wb, (size_t) mwriter->buf.len); } if (mwriter->still_checksumming && diff --git a/src/bin/pg_dump/parallel.c b/src/bin/pg_dump/parallel.c index 4a0d04b646f..d6bdb60977c 100644 --- a/src/bin/pg_dump/parallel.c +++ b/src/bin/pg_dump/parallel.c @@ -186,7 +186,7 @@ static CRITICAL_SECTION signal_info_lock; #define write_stderr(str) \ do { \ const char *str_ = (str); \ - int rc_; \ + ssize_t rc_; \ rc_ = write(fileno(stderr), str_, strlen(str_)); \ (void) rc_; \ } while (0) @@ -1526,7 +1526,7 @@ getMessageFromLeader(int pipefd[2]) static void sendMessageToLeader(int pipefd[2], const char *str) { - int len = strlen(str) + 1; + size_t len = strlen(str) + 1; if (pipewrite(pipefd[PIPE_WRITE], str, len) != len) pg_fatal("could not write to the communication channel: %m"); @@ -1643,7 +1643,7 @@ getMessageFromWorker(ParallelState *pstate, bool do_wait, int *worker) static void sendMessageToWorker(ParallelState *pstate, int worker, const char *str) { - int len = strlen(str) + 1; + size_t len = strlen(str) + 1; if (pipewrite(pstate->parallelSlot[worker].pipeWrite, str, len) != len) { diff --git a/src/bin/pg_test_fsync/pg_test_fsync.c b/src/bin/pg_test_fsync/pg_test_fsync.c index 4b84f86e7d7..c51b1271f1f 100644 --- a/src/bin/pg_test_fsync/pg_test_fsync.c +++ b/src/bin/pg_test_fsync/pg_test_fsync.c @@ -593,7 +593,7 @@ test_non_sync(void) static void signal_cleanup(SIGNAL_ARGS) { - int rc; + ssize_t rc; /* Delete the file if it exists. Ignore errors */ if (needs_unlink) diff --git a/src/fe_utils/cancel.c b/src/fe_utils/cancel.c index 9fac04e333e..0de2ccf684c 100644 --- a/src/fe_utils/cancel.c +++ b/src/fe_utils/cancel.c @@ -61,7 +61,7 @@ #define write_stderr(str) \ do { \ const char *str_ = (str); \ - int rc_; \ + ssize_t rc_; \ rc_ = write(fileno(stderr), str_, strlen(str_)); \ (void) rc_; \ } while (0) diff --git a/src/include/access/timeline.h b/src/include/access/timeline.h index 76b852d07ab..3aee3419a5c 100644 --- a/src/include/access/timeline.h +++ b/src/include/access/timeline.h @@ -34,7 +34,7 @@ extern bool existsTimeLineHistory(TimeLineID probeTLI); extern TimeLineID findNewestTimeLine(TimeLineID startTLI); extern void writeTimeLineHistory(TimeLineID newTLI, TimeLineID parentTLI, XLogRecPtr switchpoint, const char *reason); -extern void writeTimeLineHistoryFile(TimeLineID tli, const char *content, int size); +extern void writeTimeLineHistoryFile(TimeLineID tli, const char *content, size_t size); extern void restoreTimeLineHistoryFiles(TimeLineID begin, TimeLineID end); extern bool tliInHistory(TimeLineID tli, List *expectedTLEs); extern TimeLineID tliOfPointInHistory(XLogRecPtr ptr, List *history); diff --git a/src/include/replication/walreceiver.h b/src/include/replication/walreceiver.h index 47c07574d4d..760364e3587 100644 --- a/src/include/replication/walreceiver.h +++ b/src/include/replication/walreceiver.h @@ -309,7 +309,7 @@ typedef void (*walrcv_readtimelinehistoryfile_fn) (WalReceiverConn *conn, TimeLineID tli, char **filename, char **content, - int *size); + size_t *size); /* * walrcv_startstreaming_fn diff --git a/src/interfaces/libpq/fe-lobj.c b/src/interfaces/libpq/fe-lobj.c index 3014cae33ac..19ebab22ba3 100644 --- a/src/interfaces/libpq/fe-lobj.c +++ b/src/interfaces/libpq/fe-lobj.c @@ -749,8 +749,7 @@ lo_export(PGconn *conn, Oid lobjId, const char *filename) { int result = 1; int fd; - int nbytes, - tmp; + int nbytes; char buf[LO_BUFSIZE]; int lobj; char sebuf[PG_STRERROR_R_BUFLEN]; @@ -788,6 +787,8 @@ lo_export(PGconn *conn, Oid lobjId, const char *filename) */ while ((nbytes = lo_read(conn, lobj, buf, LO_BUFSIZE)) > 0) { + ssize_t tmp; + tmp = write(fd, buf, nbytes); if (tmp != nbytes) { diff --git a/src/test/examples/testlo.c b/src/test/examples/testlo.c index f73a0fcb100..0b1d097edff 100644 --- a/src/test/examples/testlo.c +++ b/src/test/examples/testlo.c @@ -151,8 +151,7 @@ exportFile(PGconn *conn, Oid lobjId, char *filename) { int lobj_fd; char buf[BUFSIZE]; - int nbytes, - tmp; + int nbytes; int fd; /* @@ -177,6 +176,8 @@ exportFile(PGconn *conn, Oid lobjId, char *filename) */ while ((nbytes = lo_read(conn, lobj_fd, buf, BUFSIZE)) > 0) { + ssize_t tmp; + tmp = write(fd, buf, nbytes); if (tmp < nbytes) { diff --git a/src/test/examples/testlo64.c b/src/test/examples/testlo64.c index cecfa683698..3698d7f256e 100644 --- a/src/test/examples/testlo64.c +++ b/src/test/examples/testlo64.c @@ -174,8 +174,7 @@ exportFile(PGconn *conn, Oid lobjId, char *filename) { int lobj_fd; char buf[BUFSIZE]; - int nbytes, - tmp; + int nbytes; int fd; /* @@ -200,6 +199,8 @@ exportFile(PGconn *conn, Oid lobjId, char *filename) */ while ((nbytes = lo_read(conn, lobj_fd, buf, BUFSIZE)) > 0) { + ssize_t tmp; + tmp = write(fd, buf, nbytes); if (tmp < nbytes) {