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

Commit 530f89e

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 5649931 commit 530f89e

File tree

2 files changed

+59
-7
lines changed

2 files changed

+59
-7
lines changed

src/bin/pgbench/pgbench.c

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3880,8 +3880,14 @@ advanceConnectionState(TState *thread, CState *st, StatsData *agg)
38803880
switch (conditional_stack_peek(st->cstack))
38813881
{
38823882
case IFSTATE_FALSE:
3883-
if (command->meta == META_IF ||
3884-
command->meta == META_ELIF)
3883+
if (command->meta == META_IF)
3884+
{
3885+
/* nested if in skipped branch - ignore */
3886+
conditional_stack_push(st->cstack,
3887+
IFSTATE_IGNORED);
3888+
st->command++;
3889+
}
3890+
else if (command->meta == META_ELIF)
38853891
{
38863892
/* we must evaluate the condition */
38873893
st->state = CSTATE_START_COMMAND;
@@ -3900,11 +3906,7 @@ advanceConnectionState(TState *thread, CState *st, StatsData *agg)
39003906
conditional_stack_pop(st->cstack);
39013907
if (conditional_active(st->cstack))
39023908
st->state = CSTATE_START_COMMAND;
3903-
3904-
/*
3905-
* else state remains in
3906-
* CSTATE_SKIP_COMMAND
3907-
*/
3909+
/* else state remains CSTATE_SKIP_COMMAND */
39083910
st->command++;
39093911
}
39103912
break;

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

Lines changed: 50 additions & 0 deletions
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)