Skip to content

Commit c2e5f60

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 c606f10 commit c2e5f60

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
@@ -266,6 +266,26 @@ sub pgbench_scripts
266266
'endif syntax error',
267267
[qr{unexpected argument in command "endif"}],
268268
{ 'endif-bad.sql' => "\\if 0\n\\endif BAD\n" }
269+
],
270+
[
271+
'not enough arguments for least',
272+
[qr{at least one argument expected \(least\)}],
273+
{ 'bad-least.sql' => "\\set i least()\n" }
274+
],
275+
[
276+
'not enough arguments for greatest',
277+
[qr{at least one argument expected \(greatest\)}],
278+
{ 'bad-greatest.sql' => "\\set i greatest()\n" }
279+
],
280+
[
281+
'not enough arguments for hash',
282+
[qr{unexpected number of arguments \(hash\)}],
283+
{ 'bad-hash-1.sql' => "\\set i hash()\n" }
284+
],
285+
[
286+
'too many arguments for hash',
287+
[qr{unexpected number of arguments \(hash\)}],
288+
{ 'bad-hash-2.sql' => "\\set i hash(1,2,3)\n" }
269289
],);
270290

271291
for my $t (@script_tests)

0 commit comments

Comments
 (0)