0001-Don-t-cast-pgoff_t-to-possibly-32-bit-types-for-outp.patch

text/plain

Filename: 0001-Don-t-cast-pgoff_t-to-possibly-32-bit-types-for-outp.patch
Type: text/plain
Part: 0
Message: truncating casts of pgoff_t

Patch

Format: format-patch
Series: patch 0001
Subject: Don't cast pgoff_t to possibly 32-bit types for output
File+
src/backend/backup/basebackup_server.c 4 4
src/backend/backup/walsummary.c 2 2
src/backend/replication/walsender.c 2 0
src/backend/storage/file/fd.c 2 2
From abdb70a05f0d5b24511a5b915426fb4c1d7000d4 Mon Sep 17 00:00:00 2001
From: Peter Eisentraut <peter@eisentraut.org>
Date: Fri, 12 Jun 2026 08:53:24 +0200
Subject: [PATCH 1/2] 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 one case, the 32-bit size is baked into the protocol, so here we
just add an assertion and document this discrepancy.
---
 src/backend/backup/basebackup_server.c | 8 ++++----
 src/backend/backup/walsummary.c        | 4 ++--
 src/backend/replication/walsender.c    | 2 ++
 src/backend/storage/file/fd.c          | 4 ++--
 4 files changed, 10 insertions(+), 8 deletions(-)

diff --git a/src/backend/backup/basebackup_server.c b/src/backend/backup/basebackup_server.c
index 0d44a148f01..3d44bf71d19 100644
--- a/src/backend/backup/basebackup_server.c
+++ b/src/backend/backup/basebackup_server.c
@@ -176,9 +176,9 @@ 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 %u",
+				 errmsg("could not write file \"%s\": wrote only %d of %zu bytes at offset %lld",
 						FilePathName(mysink->file),
-						nbytes, len, (unsigned) mysink->filepos),
+						nbytes, len, (long long) mysink->filepos),
 				 errhint("Check free disk space.")));
 	}
 
@@ -269,9 +269,9 @@ 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 %u",
+				 errmsg("could not write file \"%s\": wrote only %d of %zu bytes at offset %lld",
 						FilePathName(mysink->file),
-						nbytes, len, (unsigned) mysink->filepos),
+						nbytes, len, (long long) mysink->filepos),
 				 errhint("Check free disk space.")));
 	}
 
diff --git a/src/backend/backup/walsummary.c b/src/backend/backup/walsummary.c
index 0cdc86af30b..48b7bd8c521 100644
--- a/src/backend/backup/walsummary.c
+++ b/src/backend/backup/walsummary.c
@@ -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;
diff --git a/src/backend/replication/walsender.c b/src/backend/replication/walsender.c
index c931d9b4fa8..de8c9940877 100644
--- a/src/backend/replication/walsender.c
+++ b/src/backend/replication/walsender.c
@@ -661,6 +661,8 @@ SendTimeLineHistory(TimeLineHistoryCmd *cmd)
 				(errcode_for_file_access(),
 				 errmsg("could not seek to beginning of file \"%s\": %m", path)));
 
+	/* XXX might truncate histfilelen */
+	Assert(histfilelen <= UINT32_MAX);
 	pq_sendint32(&buf, histfilelen);	/* col2 len */
 
 	bytesleft = histfilelen;
diff --git a/src/backend/storage/file/fd.c b/src/backend/storage/file/fd.c
index 4cf4717f764..b8a66b3b475 100644
--- a/src/backend/storage/file/fd.c
+++ b/src/backend/storage/file/fd.c
@@ -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)));
 	}
 }
 
-- 
2.54.0