]> git.ipfire.org Git - thirdparty/postgresql.git/commitdiff
Don't cast pgoff_t to possibly 32-bit types for output
authorPeter Eisentraut <peter@eisentraut.org>
Tue, 7 Jul 2026 09:45:09 +0000 (11:45 +0200)
committerPeter Eisentraut <peter@eisentraut.org>
Tue, 7 Jul 2026 09:45:09 +0000 (11:45 +0200)
pgoff_t is most likely a 64-bit integer, so casting it to a 32-bit
type for output could lose data.  In the cases addressed here, the
files cannot actually get that large, so this is only cosmetic and to
set better examples for the future.  (Similar issues that could have
actual practical impact were addressed separately in commit
e8f851d6172.)

In one case, the 32-bit size is baked into the protocol, so here we
add an elog and document this discrepancy.

Reviewed-by: Heikki Linnakangas <hlinnaka@iki.fi>
Reviewed-by: Chao Li <li.evan.chao@gmail.com>
Discussion: https://www.postgresql.org/message-id/flat/20ce62fa-47fc-457b-b504-12f3c1651726%40eisentraut.org

src/backend/access/heap/rewriteheap.c
src/backend/backup/walsummary.c
src/backend/replication/walsender.c
src/backend/storage/file/fd.c
src/bin/pg_upgrade/slru_io.c

index 5a5398a76ae7d46cfa937da90c2961d7c8473af9..0ccd392c3dc0ba96e499916b515fedf4bd26d32e 100644 (file)
@@ -1104,8 +1104,8 @@ heap_xlog_logical_rewrite(XLogReaderState *r)
        if (ftruncate(fd, xlrec->offset) != 0)
                ereport(ERROR,
                                (errcode_for_file_access(),
-                                errmsg("could not truncate file \"%s\" to %u: %m",
-                                               path, (uint32) xlrec->offset)));
+                                errmsg("could not truncate file \"%s\" to %lld: %m",
+                                               path, (long long int) xlrec->offset)));
        pgstat_report_wait_end();
 
        data = XLogRecGetData(r) + sizeof(*xlrec);
index 0cdc86af30bcf2a544ae0a631f7a8b04ae05efaf..48b7bd8c5211955d8245cf800b6d10a0e541e308 100644 (file)
@@ -306,9 +306,9 @@ WriteWalSummary(void *wal_summary_io, void *data, int length)
        if (nbytes != length)
                ereport(ERROR,
                                (errcode_for_file_access(),
-                                errmsg("could not write file \"%s\": wrote only %d of %d bytes at offset %u",
+                                errmsg("could not write file \"%s\": wrote only %d of %d bytes at offset %lld",
                                                FilePathName(io->file), nbytes,
-                                               length, (unsigned) io->filepos),
+                                               length, (long long) io->filepos),
                                 errhint("Check free disk space.")));
 
        io->filepos += nbytes;
index c931d9b4fa80a9737369bc10d018682d3a23d544..ae9ffd0d096bc7ddd3b07145bf8589ac8a59663f 100644 (file)
@@ -661,6 +661,12 @@ SendTimeLineHistory(TimeLineHistoryCmd *cmd)
                                (errcode_for_file_access(),
                                 errmsg("could not seek to beginning of file \"%s\": %m", path)));
 
+       /*
+        * unlikely in practice, but to document the implicit integer conversion
+        */
+       if (histfilelen > UINT32_MAX)
+               elog(ERROR, "timeline history file is too large");
+
        pq_sendint32(&buf, histfilelen);        /* col2 len */
 
        bytesleft = histfilelen;
index 4cf4717f764b053c22ae5d35712af753ae845358..b8a66b3b475e4eb5a089db658107ba1758671140 100644 (file)
@@ -1521,8 +1521,8 @@ ReportTemporaryFileUsage(const char *path, pgoff_t size)
        {
                if ((size / 1024) >= log_temp_files)
                        ereport(LOG,
-                                       (errmsg("temporary file: path \"%s\", size %lu",
-                                                       path, (unsigned long) size)));
+                                       (errmsg("temporary file: path \"%s\", size %lld",
+                                                       path, (long long) size)));
        }
 }
 
index 2188a287850d88d46a3ecc844ae2f95336de4cd5..aa9d59a0d7bad57f277368680000abb934bcc7c1 100644 (file)
@@ -133,8 +133,8 @@ SlruReadSwitchPageSlow(SlruSegState *state, uint64 pageno)
                if (rc == 0)
                {
                        /* unexpected EOF */
-                       pg_log(PG_WARNING, "unexpected EOF reading file \"%s\" at offset %u, reading as zeros",
-                                  state->fn, (unsigned int) offset);
+                       pg_log(PG_WARNING, "unexpected EOF reading file \"%s\" at offset %lld, reading as zeros",
+                                  state->fn, (long long int) offset);
                        memset(&state->buf.data[bytes_read], 0, BLCKSZ - bytes_read);
                        break;
                }