Skip to content

Commit 72fba84

Browse files
committed
cfg syntax: Disallow esoteric if/while syntax
The opensips.cfg no longer accepts constructs such as: if $var(foo) { ... } if [ $var(foo) == 2 ] xlog("bar\n"); while $var(foo) < 10 { ... } ... and forces the condition to be paranthesized. Aside from any consistency considerations, this change also allows us to disambiguate the if-if-else shift/reduce conflicts of the grammar, which were so far masked using the "expect 2" rule.
1 parent a9e45e8 commit 72fba84

File tree

1 file changed

+16
-18
lines changed

1 file changed

+16
-18
lines changed

cfg.y

+16-18
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,7 @@ extern int cfg_parse_only_routes;
448448
%left BOR BAND BXOR BLSHIFT BRSHIFT
449449
%left PLUS MINUS SLASH MULT MODULO
450450
%right NOT BNOT
451+
%right RPAREN ELSE /* solves the classic if-if-else ambiguity */
451452

452453
/* values */
453454
%token <intval> NUMBER
@@ -516,11 +517,8 @@ extern int cfg_parse_only_routes;
516517
%type <strval> folded_string
517518
%type <multistr> multi_string
518519

519-
/*
520-
* since "if_cmd" is inherently ambiguous,
521-
* skip 1 harmless shift/reduce conflict when compiling our grammar
522-
*/
523-
%expect 2
520+
/* all shift/reduce conflicts are currently disambiguated */
521+
%expect 0
524522

525523

526524
%%
@@ -1813,47 +1811,47 @@ brk_action: BREAK SEMICOLON { mk_action0($$, BREAK_T);}
18131811
| cmd error { $$=0; yyerror("bad command: missing ';'?"); }
18141812
;
18151813

1816-
brk_if_cmd: IF exp brk_stm { mk_action3( $$, IF_T,
1814+
brk_if_cmd: IF LPAREN exp RPAREN brk_stm { mk_action3( $$, IF_T,
18171815
EXPR_ST,
18181816
ACTIONS_ST,
18191817
NOSUBTYPE,
1820-
$2,
18211818
$3,
1819+
$5,
18221820
0);
18231821
}
1824-
| IF exp brk_stm ELSE brk_stm { mk_action3( $$, IF_T,
1822+
| IF LPAREN exp RPAREN brk_stm ELSE brk_stm { mk_action3( $$, IF_T,
18251823
EXPR_ST,
18261824
ACTIONS_ST,
18271825
ACTIONS_ST,
1828-
$2,
18291826
$3,
1830-
$5);
1827+
$5,
1828+
$7);
18311829
}
18321830
;
18331831

1834-
if_cmd: IF exp stm { mk_action3( $$, IF_T,
1832+
if_cmd: IF LPAREN exp RPAREN stm { mk_action3( $$, IF_T,
18351833
EXPR_ST,
18361834
ACTIONS_ST,
18371835
NOSUBTYPE,
1838-
$2,
18391836
$3,
1837+
$5,
18401838
0);
18411839
}
1842-
| IF exp stm ELSE stm { mk_action3( $$, IF_T,
1840+
| IF LPAREN exp RPAREN stm ELSE stm { mk_action3( $$, IF_T,
18431841
EXPR_ST,
18441842
ACTIONS_ST,
18451843
ACTIONS_ST,
1846-
$2,
18471844
$3,
1848-
$5);
1845+
$5,
1846+
$7);
18491847
}
18501848
;
18511849

1852-
while_cmd: WHILE exp brk_stm { mk_action2( $$, WHILE_T,
1850+
while_cmd: WHILE LPAREN exp RPAREN brk_stm { mk_action2( $$, WHILE_T,
18531851
EXPR_ST,
18541852
ACTIONS_ST,
1855-
$2,
1856-
$3);
1853+
$3,
1854+
$5);
18571855
}
18581856
;
18591857

0 commit comments

Comments
 (0)