From 0194f66dba4f599ba66971d97e8c213d95979441 Mon Sep 17 00:00:00 2001
From: Aleksander Alekseev <aleksander@timescale.com>
Date: Tue, 27 Sep 2022 13:33:21 +0300
Subject: [PATCH v4 1/2] Pass Size/size_t as a 2nd argument of snprintf()

This is a follow-up to a932824d. The previous patch made sure the second
argument of snprintf() is passed as Size rather than int in tablesync.c.
This patch does the same change to the rest of the project.

Aleksander Alekseev, reviewed by Peter Smith
Discussion: https://postgr.es/m/CAHut%2BPsa8hhfSE6ozUK-ih7GkQziAVAf4f3bqiXEj2nQiu-43g%40mail.gmail.com
---
 src/backend/replication/logical/worker.c | 4 ++--
 src/backend/utils/adt/jsonb.c            | 6 +++---
 src/bin/pg_dump/parallel.c               | 4 ++--
 src/bin/pg_dump/pg_dump_sort.c           | 4 ++--
 src/bin/psql/command.c                   | 2 +-
 src/common/ip.c                          | 4 ++--
 src/interfaces/ecpg/ecpglib/execute.c    | 2 +-
 src/interfaces/ecpg/ecpglib/misc.c       | 2 +-
 src/interfaces/ecpg/ecpglib/prepare.c    | 2 +-
 src/interfaces/libpq/fe-connect.c        | 2 +-
 src/interfaces/libpq/fe-gssapi-common.c  | 2 +-
 src/interfaces/libpq/libpq-int.h         | 2 +-
 src/test/regress/pg_regress.c            | 2 +-
 13 files changed, 19 insertions(+), 19 deletions(-)

diff --git a/src/backend/replication/logical/worker.c b/src/backend/replication/logical/worker.c
index 207a5805ba..188c51660e 100644
--- a/src/backend/replication/logical/worker.c
+++ b/src/backend/replication/logical/worker.c
@@ -349,7 +349,7 @@ static void apply_handle_tuple_routing(ApplyExecutionData *edata,
 									   CmdType operation);
 
 /* Compute GID for two_phase transactions */
-static void TwoPhaseTransactionGid(Oid subid, TransactionId xid, char *gid, int szgid);
+static void TwoPhaseTransactionGid(Oid subid, TransactionId xid, char *gid, Size szgid);
 
 /* Common streaming function to apply all the spooled messages */
 static void apply_spooled_messages(TransactionId xid, XLogRecPtr lsn);
@@ -3480,7 +3480,7 @@ cleanup_subxact_info()
  * Return the GID in the supplied buffer.
  */
 static void
-TwoPhaseTransactionGid(Oid subid, TransactionId xid, char *gid, int szgid)
+TwoPhaseTransactionGid(Oid subid, TransactionId xid, char *gid, Size szgid)
 {
 	Assert(subid != InvalidRepOriginId);
 
diff --git a/src/backend/utils/adt/jsonb.c b/src/backend/utils/adt/jsonb.c
index 9e14922ec2..1ccdca349e 100644
--- a/src/backend/utils/adt/jsonb.c
+++ b/src/backend/utils/adt/jsonb.c
@@ -1592,7 +1592,7 @@ jsonb_agg_transfn(PG_FUNCTION_ARGS)
 					/* copy string values in the aggregate context */
 					char	   *buf = palloc(v.val.string.len + 1);
 
-					snprintf(buf, v.val.string.len + 1, "%s", v.val.string.val);
+					snprintf(buf, (Size) (v.val.string.len + 1), "%s", v.val.string.val);
 					v.val.string.val = buf;
 				}
 				else if (v.type == jbvNumeric)
@@ -1763,7 +1763,7 @@ jsonb_object_agg_transfn(PG_FUNCTION_ARGS)
 					/* copy string values in the aggregate context */
 					char	   *buf = palloc(v.val.string.len + 1);
 
-					snprintf(buf, v.val.string.len + 1, "%s", v.val.string.val);
+					snprintf(buf, (Size) (v.val.string.len + 1), "%s", v.val.string.val);
 					v.val.string.val = buf;
 				}
 				else
@@ -1822,7 +1822,7 @@ jsonb_object_agg_transfn(PG_FUNCTION_ARGS)
 					/* copy string values in the aggregate context */
 					char	   *buf = palloc(v.val.string.len + 1);
 
-					snprintf(buf, v.val.string.len + 1, "%s", v.val.string.val);
+					snprintf(buf, (Size) (v.val.string.len + 1), "%s", v.val.string.val);
 					v.val.string.val = buf;
 				}
 				else if (v.type == jbvNumeric)
diff --git a/src/bin/pg_dump/parallel.c b/src/bin/pg_dump/parallel.c
index c8a70d9bc1..f5e4c63e8f 100644
--- a/src/bin/pg_dump/parallel.c
+++ b/src/bin/pg_dump/parallel.c
@@ -1106,7 +1106,7 @@ ParallelBackupEnd(ArchiveHandle *AH, ParallelState *pstate)
  */
 static void
 buildWorkerCommand(ArchiveHandle *AH, TocEntry *te, T_Action act,
-				   char *buf, int buflen)
+				   char *buf, Size buflen)
 {
 	if (act == ACT_DUMP)
 		snprintf(buf, buflen, "DUMP %d", te->dumpId);
@@ -1154,7 +1154,7 @@ parseWorkerCommand(ArchiveHandle *AH, TocEntry **te, T_Action *act,
  */
 static void
 buildWorkerResponse(ArchiveHandle *AH, TocEntry *te, T_Action act, int status,
-					char *buf, int buflen)
+					char *buf, Size buflen)
 {
 	snprintf(buf, buflen, "OK %d %d %d",
 			 te->dumpId,
diff --git a/src/bin/pg_dump/pg_dump_sort.c b/src/bin/pg_dump/pg_dump_sort.c
index 5de3241eb4..51337ff798 100644
--- a/src/bin/pg_dump/pg_dump_sort.c
+++ b/src/bin/pg_dump/pg_dump_sort.c
@@ -173,7 +173,7 @@ static int	findLoop(DumpableObject *obj,
 static void repairDependencyLoop(DumpableObject **loop,
 								 int nLoop);
 static void describeDumpableObject(DumpableObject *obj,
-								   char *buf, int bufsize);
+								   char *buf, Size bufsize);
 
 
 /*
@@ -1268,7 +1268,7 @@ repairDependencyLoop(DumpableObject **loop,
  * This should probably go somewhere else...
  */
 static void
-describeDumpableObject(DumpableObject *obj, char *buf, int bufsize)
+describeDumpableObject(DumpableObject *obj, char *buf, Size bufsize)
 {
 	switch (obj->objType)
 	{
diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c
index a141146e70..fc882138fb 100644
--- a/src/bin/psql/command.c
+++ b/src/bin/psql/command.c
@@ -5008,7 +5008,7 @@ do_watch(PQExpBuffer query_buf, double sleep)
 	char	   *title;
 	const char *pagerprog = NULL;
 	FILE	   *pagerpipe = NULL;
-	int			title_len;
+	Size		title_len;
 	int			res = 0;
 #ifndef WIN32
 	sigset_t	sigalrm_sigchld_sigint;
diff --git a/src/common/ip.c b/src/common/ip.c
index 6343f49a70..5f09be9195 100644
--- a/src/common/ip.c
+++ b/src/common/ip.c
@@ -42,7 +42,7 @@ static int	getaddrinfo_unix(const char *path,
 
 static int	getnameinfo_unix(const struct sockaddr_un *sa, int salen,
 							 char *node, int nodelen,
-							 char *service, int servicelen,
+							 char *service, Size servicelen,
 							 int flags);
 
 
@@ -227,7 +227,7 @@ getaddrinfo_unix(const char *path, const struct addrinfo *hintsp,
 static int
 getnameinfo_unix(const struct sockaddr_un *sa, int salen,
 				 char *node, int nodelen,
-				 char *service, int servicelen,
+				 char *service, Size servicelen,
 				 int flags)
 {
 	int			ret;
diff --git a/src/interfaces/ecpg/ecpglib/execute.c b/src/interfaces/ecpg/ecpglib/execute.c
index bd94bd4e6c..f1aa833911 100644
--- a/src/interfaces/ecpg/ecpglib/execute.c
+++ b/src/interfaces/ecpg/ecpglib/execute.c
@@ -1533,7 +1533,7 @@ ecpg_build_params(struct statement *stmt)
 			if (stmt->command[position] == '?')
 			{
 				/* yes, replace with new style */
-				int			buffersize = sizeof(int) * CHAR_BIT * 10 / 3;	/* a rough guess of the
+				size_t		buffersize = sizeof(int) * CHAR_BIT * 10 / 3;	/* a rough guess of the
 																			 * size we need */
 
 				if (!(tobeinserted = (char *) ecpg_alloc(buffersize, stmt->lineno)))
diff --git a/src/interfaces/ecpg/ecpglib/misc.c b/src/interfaces/ecpg/ecpglib/misc.c
index 1eef1ec044..d861d06993 100644
--- a/src/interfaces/ecpg/ecpglib/misc.c
+++ b/src/interfaces/ecpg/ecpglib/misc.c
@@ -267,7 +267,7 @@ ecpg_log(const char *format,...)
 	va_list		ap;
 	struct sqlca_t *sqlca = ECPGget_sqlca();
 	const char *intl_format;
-	int			bufsize;
+	size_t		bufsize;
 	char	   *fmt;
 
 	if (!simple_debug)
diff --git a/src/interfaces/ecpg/ecpglib/prepare.c b/src/interfaces/ecpg/ecpglib/prepare.c
index ea1146f520..5ff8feac7d 100644
--- a/src/interfaces/ecpg/ecpglib/prepare.c
+++ b/src/interfaces/ecpg/ecpglib/prepare.c
@@ -120,7 +120,7 @@ replace_variables(char **text, int lineno)
 		else
 		{
 			/* a rough guess of the size we need: */
-			int			buffersize = sizeof(int) * CHAR_BIT * 10 / 3;
+			size_t		buffersize = sizeof(int) * CHAR_BIT * 10 / 3;
 			int			len;
 			char	   *buffer,
 					   *newcopy;
diff --git a/src/interfaces/libpq/fe-connect.c b/src/interfaces/libpq/fe-connect.c
index 746e9b4f1e..cdccd59c54 100644
--- a/src/interfaces/libpq/fe-connect.c
+++ b/src/interfaces/libpq/fe-connect.c
@@ -7313,7 +7313,7 @@ sslVerifyProtocolRange(const char *min, const char *max)
  * there (which it isn't).
  */
 bool
-pqGetHomeDirectory(char *buf, int bufsize)
+pqGetHomeDirectory(char *buf, size_t bufsize)
 {
 #ifndef WIN32
 	const char *home;
diff --git a/src/interfaces/libpq/fe-gssapi-common.c b/src/interfaces/libpq/fe-gssapi-common.c
index fa08526ee2..9f03d02c32 100644
--- a/src/interfaces/libpq/fe-gssapi-common.c
+++ b/src/interfaces/libpq/fe-gssapi-common.c
@@ -83,7 +83,7 @@ pg_GSS_load_servicename(PGconn *conn)
 {
 	OM_uint32	maj_stat,
 				min_stat;
-	int			maxlen;
+	Size		maxlen;
 	gss_buffer_desc temp_gbuf;
 	char	   *host;
 
diff --git a/src/interfaces/libpq/libpq-int.h b/src/interfaces/libpq/libpq-int.h
index c75ed63a2c..2d570017a3 100644
--- a/src/interfaces/libpq/libpq-int.h
+++ b/src/interfaces/libpq/libpq-int.h
@@ -648,7 +648,7 @@ extern char *const pgresStatus[];
 extern void pqDropConnection(PGconn *conn, bool flushInput);
 extern int	pqPacketSend(PGconn *conn, char pack_type,
 						 const void *buf, size_t buf_len);
-extern bool pqGetHomeDirectory(char *buf, int bufsize);
+extern bool pqGetHomeDirectory(char *buf, size_t bufsize);
 
 #ifdef ENABLE_THREAD_SAFETY
 extern pgthreadlock_t pg_g_threadlock;
diff --git a/src/test/regress/pg_regress.c b/src/test/regress/pg_regress.c
index dda076847a..229165a684 100644
--- a/src/test/regress/pg_regress.c
+++ b/src/test/regress/pg_regress.c
@@ -1206,7 +1206,7 @@ static char *
 get_alternative_expectfile(const char *expectfile, int i)
 {
 	char	   *last_dot;
-	int			ssize = strlen(expectfile) + 2 + 1;
+	Size		ssize = strlen(expectfile) + 2 + 1;
 	char	   *tmp;
 	char	   *s;
 
-- 
2.37.2

