Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit ff9dc96

Browse files
committed
pgbench: fix misprocessing of some nested \if constructs.
An \if command appearing within a false (not-to-be-executed) \if branch was incorrectly treated the same as \elif. This could allow statements within the inner \if to be executed when they should not be. Also the missing inner \if stack entry would result in an assertion failure (in assert-enabled builds) when the final \endif is reached. Report and patch by Michail Nikolaev. Back-patch to all supported branches. Discussion: https://postgr.es/m/CANtu0oiA1ke=SP6tauhNqkUdv5QFsJtS1p=aOOf_iU+EhyKkjQ@mail.gmail.com
1 parent 87e16d6 commit ff9dc96

File tree

2 files changed

+59
-7
lines changed

2 files changed

+59
-7
lines changed

src/bin/pgbench/pgbench.c

+9-7
Original file line numberDiff line numberDiff line change
@@ -3879,8 +3879,14 @@ advanceConnectionState(TState *thread, CState *st, StatsData *agg)
38793879
switch (conditional_stack_peek(st->cstack))
38803880
{
38813881
case IFSTATE_FALSE:
3882-
if (command->meta == META_IF ||
3883-
command->meta == META_ELIF)
3882+
if (command->meta == META_IF)
3883+
{
3884+
/* nested if in skipped branch - ignore */
3885+
conditional_stack_push(st->cstack,
3886+
IFSTATE_IGNORED);
3887+
st->command++;
3888+
}
3889+
else if (command->meta == META_ELIF)
38843890
{
38853891
/* we must evaluate the condition */
38863892
st->state = CSTATE_START_COMMAND;
@@ -3899,11 +3905,7 @@ advanceConnectionState(TState *thread, CState *st, StatsData *agg)
38993905
conditional_stack_pop(st->cstack);
39003906
if (conditional_active(st->cstack))
39013907
st->state = CSTATE_START_COMMAND;
3902-
3903-
/*
3904-
* else state remains in
3905-
* CSTATE_SKIP_COMMAND
3906-
*/
3908+
/* else state remains CSTATE_SKIP_COMMAND */
39073909
st->command++;
39083910
}
39093911
break;

src/bin/pgbench/t/001_pgbench_with_server.pl

+50
Original file line numberDiff line numberDiff line change
@@ -668,6 +668,56 @@ sub check_data_state
668668
}
669669
});
670670

671+
# test nested \if constructs
672+
$node->pgbench(
673+
'--no-vacuum --client=1 --exit-on-abort --transactions=1',
674+
0,
675+
[qr{actually processed}],
676+
[qr{^$}],
677+
'nested ifs',
678+
{
679+
'pgbench_nested_if' => q(
680+
\if false
681+
SELECT 1 / 0;
682+
\if true
683+
SELECT 1 / 0;
684+
\elif true
685+
SELECT 1 / 0;
686+
\else
687+
SELECT 1 / 0;
688+
\endif
689+
SELECT 1 / 0;
690+
\elif false
691+
\if true
692+
SELECT 1 / 0;
693+
\elif true
694+
SELECT 1 / 0;
695+
\else
696+
SELECT 1 / 0;
697+
\endif
698+
\else
699+
\if false
700+
SELECT 1 / 0;
701+
\elif false
702+
SELECT 1 / 0;
703+
\else
704+
SELECT 'correct';
705+
\endif
706+
\endif
707+
\if true
708+
SELECT 'correct';
709+
\else
710+
\if true
711+
SELECT 1 / 0;
712+
\elif true
713+
SELECT 1 / 0;
714+
\else
715+
SELECT 1 / 0;
716+
\endif
717+
\endif
718+
)
719+
});
720+
671721
# random determinism when seeded
672722
$node->safe_psql('postgres',
673723
'CREATE UNLOGGED TABLE seeded_random(seed INT8 NOT NULL, rand TEXT NOT NULL, val INTEGER NOT NULL);'

0 commit comments

Comments
 (0)