From b1a780642fca335d8db4220f2c6391fcdc2d1815 Mon Sep 17 00:00:00 2001
From: "Andrey M. Borodin" <x4mmm@flight.local>
Date: Mon, 20 Feb 2023 10:28:02 -0800
Subject: [PATCH v6 1/2] Fix incorrect argument handling in psql \watch

Incorrectly parsed argument was silently substituted with 1 second.
This is changed to proper error message.

Authour: Andrey Borodin <amborodin@acm.org>
Reviewed-by: Kyotaro Horiguchi <horikyota.ntt@gmail.com>
Reviewed-by: Nathan Bossart <nathandbossart@gmail.com>
Reviewed-by: Michael Paquier <michael@paquier.xyz>
Thread: https://postgr.es/m/CAAhFRxiZ2-n_L1ErMm9AZjgmUK%3DqS6VHb%2B0SaMn8sqqbhF7How%40mail.gmail.com
---
 src/bin/psql/command.c      | 20 +++++++++++++++++---
 src/bin/psql/t/001_basic.pl |  3 +++
 2 files changed, 20 insertions(+), 3 deletions(-)

diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c
index 955397ee9d..ae295dcfb2 100644
--- a/src/bin/psql/command.c
+++ b/src/bin/psql/command.c
@@ -2776,9 +2776,20 @@ exec_command_watch(PsqlScanState scan_state, bool active_branch,
 		/* Convert optional sleep-length argument */
 		if (opt)
 		{
-			sleep = strtod(opt, NULL);
-			if (sleep <= 0)
-				sleep = 1;
+			char *opt_end;
+			sleep = strtod(opt, &opt_end);
+			if (sleep < 0 || *opt_end || errno == ERANGE)
+			{
+				if (*opt_end)
+					pg_log_error("\\watch interval must be non-negative number, "
+								 "but argument is '%s'", opt);
+				else
+					pg_log_error("\\watch interval must be non-negative number");
+				free(opt);
+				resetPQExpBuffer(query_buf);
+				psql_scan_reset(scan_state);
+				return PSQL_CMD_ERROR;
+			}
 			free(opt);
 		}
 
@@ -5183,6 +5194,9 @@ do_watch(PQExpBuffer query_buf, double sleep)
 		if (pagerpipe && ferror(pagerpipe))
 			break;
 
+		if (sleep == 0)
+			continue;
+
 #ifdef WIN32
 
 		/*
diff --git a/src/bin/psql/t/001_basic.pl b/src/bin/psql/t/001_basic.pl
index 0167cb58a2..bd0fbf09df 100644
--- a/src/bin/psql/t/001_basic.pl
+++ b/src/bin/psql/t/001_basic.pl
@@ -325,4 +325,7 @@ is($row_count, '10',
 	'client-side error commits transaction, no ON_ERROR_STOP and multiple -c switches'
 );
 
+psql_fails_like($node, 'SELECT 1;\watch -10', qr/watch interval must be non-negative number/, '\watch negative interval');
+psql_fails_like($node, 'SELECT 1;\watch 10ab', qr/watch interval must be non-negative number, but argument is '10ab'/, '\watch incorrect interval');
+
 done_testing();
-- 
2.32.0 (Apple Git-132)

