From 972567547f15e0f47512d08d97ce01aff4bc05a7 Mon Sep 17 00:00:00 2001
From: Kyotaro Horiguchi <horikyota.ntt@gmail.com>
Date: Thu, 8 Jul 2021 17:10:19 +0900
Subject: [PATCH v3 2/3] Make complain for invalid numeirc values in
 environemnt variables

Postgresql commands that read environment variables for integer values
are tolerant of trailing garbages like '5432xyz'. Ignore such values
and make warning on it.
---
 src/bin/pg_ctl/pg_ctl.c     |  6 ++++--
 src/bin/pg_upgrade/option.c | 26 ++++++++++++++++++++++++--
 src/bin/psql/startup.c      | 11 ++++++++++-
 3 files changed, 38 insertions(+), 5 deletions(-)

diff --git a/src/bin/pg_ctl/pg_ctl.c b/src/bin/pg_ctl/pg_ctl.c
index f7ccdcdc96..b3c3e66bda 100644
--- a/src/bin/pg_ctl/pg_ctl.c
+++ b/src/bin/pg_ctl/pg_ctl.c
@@ -2320,8 +2320,10 @@ main(int argc, char **argv)
 #endif
 
 	env_wait = getenv("PGCTLTIMEOUT");
-	if (env_wait != NULL)
-		wait_seconds = atoi(env_wait);
+	if (env_wait != NULL &&
+		option_parse_int(PG_LOG_OMIT, env_wait, "PGCTLTIMEOUT",
+						 0, 65535, &wait_seconds))
+		write_stderr(_("%s: ignored invalid setting of environment variable PGCTLTIMEOUT: %s\n"), progname, env_wait);
 
 	/*
 	 * 'Action' can be before or after args so loop over both. Some
diff --git a/src/bin/pg_upgrade/option.c b/src/bin/pg_upgrade/option.c
index a8cd23f7fa..e506c3e2af 100644
--- a/src/bin/pg_upgrade/option.c
+++ b/src/bin/pg_upgrade/option.c
@@ -74,8 +74,30 @@ parseCommandLine(int argc, char *argv[])
 	os_info.progname = get_progname(argv[0]);
 
 	/* Process libpq env. variables; load values here for usage() output */
-	old_cluster.port = getenv("PGPORTOLD") ? atoi(getenv("PGPORTOLD")) : DEF_PGUPORT;
-	new_cluster.port = getenv("PGPORTNEW") ? atoi(getenv("PGPORTNEW")) : DEF_PGUPORT;
+	old_cluster.port = new_cluster.port = DEF_PGUPORT;
+	if (getenv("PGPORTOLD"))
+	{
+		int p;
+
+		if (option_parse_int(PG_LOG_OMIT, getenv("PGPORTOLD"), "PGPORTOLD",
+							 0, 65535, &p))
+			pg_log(PG_WARNING,
+				   "%s: ignored invalid setting of environment variable PGPORTOLD: %s\n",
+			   os_info.progname, getenv("PGPORTOLD"));
+		old_cluster.port = p;
+	}
+
+	if (getenv("PGPORTNEW"))
+	{
+		int p;
+
+		if (option_parse_int(PG_LOG_OMIT, getenv("PGPORTNEW"), "PGPORTNEW",
+							 0, 65535, &p))
+			pg_log(PG_WARNING,
+				   "%s: ignored invalid setting of environment variable PGPORTNEW: %s\n",
+				   os_info.progname, getenv("PGPORTNEW"));
+		new_cluster.port = p;
+	}
 
 	os_user_effective_id = get_user_info(&os_info.user);
 	/* we override just the database user name;  we got the OS id above */
diff --git a/src/bin/psql/startup.c b/src/bin/psql/startup.c
index 2931530f33..819d5493c9 100644
--- a/src/bin/psql/startup.c
+++ b/src/bin/psql/startup.c
@@ -13,12 +13,14 @@
 #include <io.h>
 #include <win32.h>
 #endif							/* WIN32 */
+#include <limits.h>
 
 #include "command.h"
 #include "common.h"
 #include "common/logging.h"
 #include "common/string.h"
 #include "describe.h"
+#include "fe_utils/option_utils.h"
 #include "fe_utils/print.h"
 #include "getopt_long.h"
 #include "help.h"
@@ -181,7 +183,14 @@ main(int argc, char *argv[])
 	refresh_utf8format(&(pset.popt.topt));
 
 	/* We must get COLUMNS here before readline() sets it */
-	pset.popt.topt.env_columns = getenv("COLUMNS") ? atoi(getenv("COLUMNS")) : 0;
+	pset.popt.topt.env_columns = 0;
+	if (getenv("COLUMNS"))
+	{
+		if (option_parse_int(PG_LOG_OMIT, getenv("COLUMNS"), "COLUMNS",
+							 0, INT_MAX, &pset.popt.topt.env_columns))
+			pg_log_warning("ignored invalid setting of environemt variable COLUMNS: %s",
+						   getenv("COLUMNS"));
+	}
 
 	pset.notty = (!isatty(fileno(stdin)) || !isatty(fileno(stdout)));
 
-- 
2.27.0

