Standardize the printf format for st_size

Started by Peter Eisentrautover 5 years ago1 messages
#1Peter Eisentraut
peter.eisentraut@2ndquadrant.com
1 attachment(s)

Existing code uses various inconsistent ways to printf struct stat's
st_size member. The type of that is off_t, which is in most cases a
signed 64-bit integer, so use the long long int format for it.

I don't think anything is affected by this in practice, but it seems
reasonable to set a good convention for the next time this code gets copied.

--
Peter Eisentraut http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services

Attachments:

0001-Standardize-the-printf-format-for-st_size.patchtext/plain; charset=UTF-8; name=0001-Standardize-the-printf-format-for-st_size.patch; x-mac-creator=0; x-mac-type=0Download
From 028e5a17e2cfa1acfdbbd9c2eaaf526b07b484c4 Mon Sep 17 00:00:00 2001
From: Peter Eisentraut <peter@eisentraut.org>
Date: Tue, 15 Sep 2020 13:33:48 +0200
Subject: [PATCH] Standardize the printf format for st_size

Existing code used various inconsistent ways to printf struct stat's
st_size member.  The type of that is off_t, which is in most cases a
signed 64-bit integer, so use the long long int format for it.
---
 src/backend/access/transam/twophase.c     | 12 ++++++------
 src/backend/access/transam/xlogarchive.c  |  6 +++---
 src/bin/pg_basebackup/pg_receivewal.c     |  4 ++--
 src/bin/pg_verifybackup/pg_verifybackup.c |  8 ++++----
 src/fe_utils/archive.c                    |  6 +++---
 5 files changed, 18 insertions(+), 18 deletions(-)

diff --git a/src/backend/access/transam/twophase.c b/src/backend/access/transam/twophase.c
index ef4f9981e3..7940060443 100644
--- a/src/backend/access/transam/twophase.c
+++ b/src/backend/access/transam/twophase.c
@@ -1243,10 +1243,10 @@ ReadTwoPhaseFile(TransactionId xid, bool missing_ok)
 		stat.st_size > MaxAllocSize)
 		ereport(ERROR,
 				(errcode(ERRCODE_DATA_CORRUPTED),
-				 errmsg_plural("incorrect size of file \"%s\": %zu byte",
-							   "incorrect size of file \"%s\": %zu bytes",
-							   (Size) stat.st_size, path,
-							   (Size) stat.st_size)));
+				 errmsg_plural("incorrect size of file \"%s\": %lld byte",
+							   "incorrect size of file \"%s\": %lld bytes",
+							   (long long int) stat.st_size, path,
+							   (long long int) stat.st_size)));
 
 	crc_offset = stat.st_size - sizeof(pg_crc32c);
 	if (crc_offset != MAXALIGN(crc_offset))
@@ -1270,8 +1270,8 @@ ReadTwoPhaseFile(TransactionId xid, bool missing_ok)
 					 errmsg("could not read file \"%s\": %m", path)));
 		else
 			ereport(ERROR,
-					(errmsg("could not read file \"%s\": read %d of %zu",
-							path, r, (Size) stat.st_size)));
+					(errmsg("could not read file \"%s\": read %d of %lld",
+							path, r, (long long int) stat.st_size)));
 	}
 
 	pgstat_report_wait_end();
diff --git a/src/backend/access/transam/xlogarchive.c b/src/backend/access/transam/xlogarchive.c
index 8f8734dc1d..cae93ab69d 100644
--- a/src/backend/access/transam/xlogarchive.c
+++ b/src/backend/access/transam/xlogarchive.c
@@ -202,10 +202,10 @@ RestoreArchivedFile(char *path, const char *xlogfname,
 				else
 					elevel = FATAL;
 				ereport(elevel,
-						(errmsg("archive file \"%s\" has wrong size: %lu instead of %lu",
+						(errmsg("archive file \"%s\" has wrong size: %lld instead of %lld",
 								xlogfname,
-								(unsigned long) stat_buf.st_size,
-								(unsigned long) expectedSize)));
+								(long long int) stat_buf.st_size,
+								(long long int) expectedSize)));
 				return false;
 			}
 			else
diff --git a/src/bin/pg_basebackup/pg_receivewal.c b/src/bin/pg_basebackup/pg_receivewal.c
index cd05f5fede..cddc896390 100644
--- a/src/bin/pg_basebackup/pg_receivewal.c
+++ b/src/bin/pg_basebackup/pg_receivewal.c
@@ -269,8 +269,8 @@ FindStreamingStart(uint32 *tli)
 
 			if (statbuf.st_size != WalSegSz)
 			{
-				pg_log_warning("segment file \"%s\" has incorrect size %d, skipping",
-							   dirent->d_name, (int) statbuf.st_size);
+				pg_log_warning("segment file \"%s\" has incorrect size %lld, skipping",
+							   dirent->d_name, (long long int) statbuf.st_size);
 				continue;
 			}
 		}
diff --git a/src/bin/pg_verifybackup/pg_verifybackup.c b/src/bin/pg_verifybackup/pg_verifybackup.c
index 20140aa027..bb3733b57e 100644
--- a/src/bin/pg_verifybackup/pg_verifybackup.c
+++ b/src/bin/pg_verifybackup/pg_verifybackup.c
@@ -411,8 +411,8 @@ parse_manifest_file(char *manifest_path, manifest_files_hash **ht_p,
 			report_fatal_error("could not read file \"%s\": %m",
 							   manifest_path);
 		else
-			report_fatal_error("could not read file \"%s\": read %d of %zu",
-							   manifest_path, rc, (size_t) statbuf.st_size);
+			report_fatal_error("could not read file \"%s\": read %d of %lld",
+							   manifest_path, rc, (long long int) statbuf.st_size);
 	}
 
 	/* Close the manifest file. */
@@ -638,8 +638,8 @@ verify_backup_file(verifier_context *context, char *relpath, char *fullpath)
 	if (m->size != sb.st_size)
 	{
 		report_backup_error(context,
-							"\"%s\" has size %zu on disk but size %zu in the manifest",
-							relpath, (size_t) sb.st_size, m->size);
+							"\"%s\" has size %lld on disk but size %zu in the manifest",
+							relpath, (long long int) sb.st_size, m->size);
 		m->bad = true;
 	}
 
diff --git a/src/fe_utils/archive.c b/src/fe_utils/archive.c
index 1e9d994af6..252dc0fb6a 100644
--- a/src/fe_utils/archive.c
+++ b/src/fe_utils/archive.c
@@ -71,9 +71,9 @@ RestoreArchivedFile(const char *path, const char *xlogfname,
 		{
 			if (expectedSize > 0 && stat_buf.st_size != expectedSize)
 			{
-				pg_log_fatal("unexpected file size for \"%s\": %lu instead of %lu",
-							 xlogfname, (unsigned long) stat_buf.st_size,
-							 (unsigned long) expectedSize);
+				pg_log_fatal("unexpected file size for \"%s\": %lld instead of %lld",
+							 xlogfname, (long long int) stat_buf.st_size,
+							 (long long int) expectedSize);
 				exit(1);
 			}
 			else
-- 
2.28.0