Don't cast pgoff_t to possibly 32-bit types for output

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
This commit is contained in:
Peter Eisentraut 2026-07-07 11:45:09 +02:00
parent bb4142fb68
commit 04fc2564fb
5 changed files with 14 additions and 8 deletions

View 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);

View 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;

View 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;

View 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)));
}
}

View 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;
}