Hi.

I created a patch to improve \sleep meta command in pgbench.

There are two problems with the current pgbench implementation of \sleep. First, when the input is like "\sleep foo s" , this string will be treated as 0 through atoi function, and no error will be raised. Second, when the input is like "\sleep :some_bool s" and some_bool is set to True or False, this bool will be treated as 0 as well. However, I think we should catch this error, so I suggest my patch to detect this and raise errors.

Regards.
--
Kota Miyake
diff --git a/src/bin/pgbench/pgbench.c b/src/bin/pgbench/pgbench.c
index 31a4df45f5..3f28b022d7 100644
--- a/src/bin/pgbench/pgbench.c
+++ b/src/bin/pgbench/pgbench.c
@@ -2860,7 +2860,18 @@ evaluateSleep(CState *st, int argc, char **argv, int *usecs)
 			pg_log_error("%s: undefined variable \"%s\"", argv[0], argv[1] + 1);
 			return false;
 		}
+
+		for (int i=0; i<strlen(var); i++)
+		{
+			if(!isdigit(var[i]))
+			{
+				pg_log_error("\\sleep command argument must be an integer (not \"%s\")", var);
+				return false;
+			}
+		}
+
 		usec = atoi(var);
+
 	}
 	else
 		usec = atoi(argv[1]);
@@ -4647,7 +4658,7 @@ process_backslash_command(PsqlScanState sstate, const char *source)
 		 * will be parsed with atoi, which ignores trailing non-digit
 		 * characters.
 		 */
-		if (my_command->argc == 2 && my_command->argv[1][0] != ':')
+		if (my_command->argv[1][0] != ':')
 		{
 			char	   *c = my_command->argv[1];
 
@@ -4655,9 +4666,18 @@ process_backslash_command(PsqlScanState sstate, const char *source)
 				c++;
 			if (*c)
 			{
-				my_command->argv[2] = c;
-				offsets[2] = offsets[1] + (c - my_command->argv[1]);
-				my_command->argc = 3;
+				if (my_command->argc ==2)
+				{
+					my_command->argv[2] = c;
+					offsets[2] = offsets[1] + (c - my_command->argv[1]);
+					my_command->argc = 3;
+				}
+				else
+				{
+					syntax_error(source, lineno, my_command->first_line, my_command->argv[0],
+							 "\\sleep command argument must be an integer",
+							 my_command->argv[1], offsets[1] - start_offset);
+				}
 			}
 		}
 

Reply via email to