From 76c1f38c84d4bc1e944ea6cc606221ea8a192c0d Mon Sep 17 00:00:00 2001 From: Peter Eisentraut Date: Mon, 29 Jun 2026 13:37:54 +0200 Subject: [PATCH v1 12/15] Clean up write() return type Be sure to store the return value in a variable of type ssize_t, not int. --- src/backend/access/transam/timeline.c | 4 ++-- src/backend/access/transam/twophase.c | 10 +++++----- src/backend/access/transam/xlog.c | 2 +- src/backend/commands/dbcommands.c | 4 ++-- src/backend/libpq/be-fsstubs.c | 5 +++-- src/backend/postmaster/fork_process.c | 2 +- .../libpqwalreceiver/libpqwalreceiver.c | 4 ++-- src/backend/replication/walreceiver.c | 2 +- src/backend/storage/ipc/waiteventset.c | 2 +- src/backend/utils/error/elog.c | 4 ++-- src/bin/pg_basebackup/pg_recvlogical.c | 14 +++++++------- src/bin/pg_basebackup/receivelog.c | 4 ++-- src/bin/pg_checksums/pg_checksums.c | 6 +++--- src/bin/pg_combinebackup/backup_label.c | 11 ++++++----- src/bin/pg_combinebackup/copy_file.c | 6 +++--- src/bin/pg_combinebackup/reconstruct.c | 6 +++--- src/bin/pg_combinebackup/write_manifest.c | 4 ++-- src/bin/pg_dump/parallel.c | 6 +++--- src/bin/pg_test_fsync/pg_test_fsync.c | 2 +- src/fe_utils/cancel.c | 2 +- src/include/access/timeline.h | 2 +- src/include/replication/walreceiver.h | 2 +- src/interfaces/libpq/fe-lobj.c | 5 +++-- src/test/examples/testlo.c | 5 +++-- src/test/examples/testlo64.c | 5 +++-- 25 files changed, 62 insertions(+), 57 deletions(-) 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 d76f77ac3f2..17288a5383b 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -3542,7 +3542,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/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..bb4c2ae0a12 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, 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/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/bin/pg_basebackup/pg_recvlogical.c b/src/bin/pg_basebackup/pg_recvlogical.c index 0f7d7fe9429..b15b3531d8c 100644 --- a/src/bin/pg_basebackup/pg_recvlogical.c +++ b/src/bin/pg_basebackup/pg_recvlogical.c @@ -293,10 +293,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; @@ -561,7 +561,7 @@ StreamLogicalLog(void) while (bytes_left) { - int ret; + ssize_t ret; ret = write(outfd, copybuf + hdr_len + bytes_written, @@ -569,7 +569,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; } @@ -581,8 +581,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 1448c62c775..be271b3b01c 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 7e2e9f958ea..f2399b15bbf 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) @@ -1528,7 +1528,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"); @@ -1645,7 +1645,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 e6b75439f56..55ff2f56c3b 100644 --- a/src/fe_utils/cancel.c +++ b/src/fe_utils/cancel.c @@ -31,7 +31,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) { -- 2.54.0