Skip to content

Commit 39d51fe

Browse files
committed
Fix handling of pgbench's hash when no argument is provided
Depending on the platform used, this can cause a crash in the worst case, or an unhelpful error message, so fail gracefully. Author: Fabien Coelho Discussion: https://postgr.es/m/alpine.DEB.2.21.1807262302550.29874@lancre Backpatch: 11-, where hash() has been added in pgbench.
1 parent 9f77ad2 commit 39d51fe

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

src/bin/pgbench/exprparse.y

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,7 @@ make_func(yyscan_t yyscanner, int fnumber, PgBenchExprList *args)
467467

468468
/* hash functions with optional seed argument */
469469
case PGBENCH_NARGS_HASH:
470-
if (len > 2)
470+
if (len < 1 || len > 2)
471471
expr_yyerror_more(yyscanner, "unexpected number of arguments",
472472
PGBENCH_FUNCTIONS[fnumber].fname);
473473

src/bin/pgbench/t/002_pgbench_no_server.pl

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,26 @@ sub pgbench_scripts
270270
'endif syntax error',
271271
[qr{unexpected argument in command "endif"}],
272272
{ 'endif-bad.sql' => "\\if 0\n\\endif BAD\n" }
273+
],
274+
[
275+
'not enough arguments for least',
276+
[qr{at least one argument expected \(least\)}],
277+
{ 'bad-least.sql' => "\\set i least()\n" }
278+
],
279+
[
280+
'not enough arguments for greatest',
281+
[qr{at least one argument expected \(greatest\)}],
282+
{ 'bad-greatest.sql' => "\\set i greatest()\n" }
283+
],
284+
[
285+
'not enough arguments for hash',
286+
[qr{unexpected number of arguments \(hash\)}],
287+
{ 'bad-hash-1.sql' => "\\set i hash()\n" }
288+
],
289+
[
290+
'too many arguments for hash',
291+
[qr{unexpected number of arguments \(hash\)}],
292+
{ 'bad-hash-2.sql' => "\\set i hash(1,2,3)\n" }
273293
],);
274294

275295
for my $t (@script_tests)

0 commit comments

Comments
 (0)