- bugfix wrong precedence on special if conditions like '$foo is ... by $bar' could cause wrong code https://github.com/smarty-php/smarty/issues/178

This commit is contained in:
uwetews
2016-02-10 03:19:25 +01:00
parent 417088bdb7
commit 47237eb84f
6 changed files with 817 additions and 772 deletions

View File

@@ -1,6 +1,7 @@
 ===== 3.1.30-dev ===== (xx.xx.xx)  ===== 3.1.30-dev ===== (xx.xx.xx)
10.02.2016 10.02.2016
- bugfix {strip} must keep space on output creating smarty tags within html tags https://github.com/smarty-php/smarty/issues/177 - bugfix {strip} must keep space on output creating smarty tags within html tags https://github.com/smarty-php/smarty/issues/177
- bugfix wrong precedence on special if conditions like '$foo is ... by $bar' could cause wrong code https://github.com/smarty-php/smarty/issues/178
09.02.2016 09.02.2016
- move some code from parser into compiler - move some code from parser into compiler

View File

@@ -306,7 +306,8 @@ class Smarty_Internal_Templatelexer
literal = ~literal~ literal = ~literal~
strip = ~strip~ strip = ~strip~
lop = ~\s*(([!=][=]{1,2})|([<][=>]?)|([>][=]?)|[&|]{2})\s*~ lop = ~\s*(([!=][=]{1,2})|([<][=>]?)|([>][=]?)|[&|]{2})\s*~
tlop = ~\s+(eq|ne|neq|gt|ge|gte|lt|le|lte|mod|and|or|xor|(is\s+(not\s+)?(odd|even|div)\s+by))\s+~ slop = ~\s+(eq|ne|neq|gt|ge|gte|lt|le|lte|mod|and|or|xor)\s+~
tlop = ~\s+(is\s+(not\s+)?(odd|even|div)\s+by)\s+~
scond = ~\s+is\s+(not\s+)?(odd|even)~ scond = ~\s+is\s+(not\s+)?(odd|even)~
isin = ~\s+is\s+in\s+~ isin = ~\s+is\s+in\s+~
as = ~\s+as\s+~ as = ~\s+as\s+~
@@ -479,6 +480,9 @@ class Smarty_Internal_Templatelexer
lop { lop {
$this->token = Smarty_Internal_Templateparser::TP_LOGOP; $this->token = Smarty_Internal_Templateparser::TP_LOGOP;
} }
slop {
$this->token = Smarty_Internal_Templateparser::TP_SLOGOP;
}
tlop { tlop {
$this->token = Smarty_Internal_Templateparser::TP_TLOGOP; $this->token = Smarty_Internal_Templateparser::TP_TLOGOP;
} }

View File

@@ -690,10 +690,15 @@ expr(res) ::= expr(e) modifierlist(l). {
} }
// if expression // if expression
// special conditions
expr(res) ::= expr(e1) tlop(c) value(e2). {
res = c['pre']. e1.c['op'].e2 .')';
}
// simple expression // simple expression
expr(res) ::= expr(e1) lop(c) expr(e2). { expr(res) ::= expr(e1) lop(c) expr(e2). {
res = (isset(c['pre']) ? c['pre'] : '') . e1.c['op'].e2 . (isset(c['pre']) ? ')' : ''); res = e1.c.e2;
} }
expr(res) ::= expr(e1) scond(c). { expr(res) ::= expr(e1) scond(c). {
res = c . e1 . ')'; res = c . e1 . ')';
} }
@@ -1222,34 +1227,40 @@ static_class_access(res) ::= DOLLARID(v) arrayindex(a) objectchain(oc). {
// if conditions and operators // if conditions and operators
lop(res) ::= LOGOP(o). { lop(res) ::= LOGOP(o). {
res['op'] = ' '. trim(o) . ' '; res = ' '. trim(o) . ' ';
} }
lop(res) ::= TLOGOP(o). { lop(res) ::= SLOGOP(o). {
static $lops = array( static $lops = array(
'eq' => array('op' => ' == ', 'pre' => null), 'eq' => ' == ',
'ne' => array('op' => ' != ', 'pre' => null), 'ne' => ' != ',
'neq' => array('op' => ' != ', 'pre' => null), 'neq' => ' != ',
'gt' => array('op' => ' > ', 'pre' => null), 'gt' => ' > ',
'ge' => array('op' => ' >= ', 'pre' => null), 'ge' => ' >= ',
'gte' => array('op' => ' >= ', 'pre' => null), 'gte' => ' >= ',
'lt' => array('op' => ' < ', 'pre' => null), 'lt' => ' < ',
'le' => array('op' => ' <= ', 'pre' => null), 'le' => ' <= ',
'lte' => array('op' => ' <= ', 'pre' => null), 'lte' => ' <= ',
'mod' => array('op' => ' % ', 'pre' => null), 'mod' => ' % ',
'and' => array('op' => ' && ', 'pre' => null), 'and' => ' && ',
'or' => array('op' => ' || ', 'pre' => null), 'or' => ' || ',
'xor' => array('op' => ' xor ', 'pre' => null), 'xor' => ' xor ',
'isdivby' => array('op' => ' % ', 'pre' => '!('), );
'isnotdivby' => array('op' => ' % ', 'pre' => '('),
'isevenby' => array('op' => ' / ', 'pre' => '!(1 & '),
'isnotevenby' => array('op' => ' / ', 'pre' => '(1 & '),
'isoddby' => array('op' => ' / ', 'pre' => '(1 & '),
'isnotoddby' => array('op' => ' / ', 'pre' => '!(1 & '),
);
$op = strtolower(preg_replace('/\s*/', '', o)); $op = strtolower(preg_replace('/\s*/', '', o));
res = $lops[$op]; res = $lops[$op];
} }
tlop(res) ::= TLOGOP(o). {
static $tlops = array(
'isdivby' => array('op' => ' % ', 'pre' => '!('),
'isnotdivby' => array('op' => ' % ', 'pre' => '('),
'isevenby' => array('op' => ' / ', 'pre' => '!(1 & '),
'isnotevenby' => array('op' => ' / ', 'pre' => '(1 & '),
'isoddby' => array('op' => ' / ', 'pre' => '(1 & '),
'isnotoddby' => array('op' => ' / ', 'pre' => '!(1 & '),
);
$op = strtolower(preg_replace('/\s*/', '', o));
res = $tlops[$op];
}
scond(res) ::= SINGLECOND(o). { scond(res) ::= SINGLECOND(o). {
static $scond = array ( static $scond = array (

View File

@@ -121,7 +121,7 @@ class Smarty extends Smarty_Internal_TemplateBase
/** /**
* smarty version * smarty version
*/ */
const SMARTY_VERSION = '3.1.30-dev/34'; const SMARTY_VERSION = '3.1.30-dev/35';
/** /**
* define variable scopes * define variable scopes

View File

@@ -559,7 +559,7 @@ class Smarty_Internal_Templatelexer
{ {
if (!isset($this->yy_global_pattern3)) { if (!isset($this->yy_global_pattern3)) {
$this->yy_global_pattern3 = "/\G(\\s*" . $this->rdel . ")|\G(" . $this->ldel . $this->yy_global_pattern3 = "/\G(\\s*" . $this->rdel . ")|\G(" . $this->ldel .
"\\s*)|\G([\"])|\G('[^'\\\\]*(?:\\\\.[^'\\\\]*)*')|\G([$]smarty\\.block\\.(child|parent))|\G([$][0-9]*[a-zA-Z_]\\w*)|\G([$])|\G(\\s+is\\s+in\\s+)|\G(\\s+as\\s+)|\G(\\s+to\\s+)|\G(\\s+step\\s+)|\G(\\s+instanceof\\s+)|\G(\\s*(([!=][=]{1,2})|([<][=>]?)|([>][=]?)|[&|]{2})\\s*)|\G(\\s+(eq|ne|neq|gt|ge|gte|lt|le|lte|mod|and|or|xor|(is\\s+(not\\s+)?(odd|even|div)\\s+by))\\s+)|\G(\\s+is\\s+(not\\s+)?(odd|even))|\G(([!]\\s*)|(not\\s+))|\G([(](int(eger)?|bool(ean)?|float|double|real|string|binary|array|object)[)]\\s*)|\G(\\s*[(]\\s*)|\G(\\s*[)])|\G(\\[\\s*)|\G(\\s*\\])|\G(\\s*[-][>]\\s*)|\G(\\s*[=][>]\\s*)|\G(\\s*[=]\\s*)|\G(([+]|[-]){2})|\G(\\s*([+]|[-])\\s*)|\G(\\s*([*]{1,2}|[%\/^&]|[<>]{2})\\s*)|\G([@])|\G([#])|\G(\\s+[0-9]*[a-zA-Z_][a-zA-Z0-9_\-:]*\\s*[=]\\s*)|\G(([0-9]*[a-zA-Z_]\\w*)?(\\\\[0-9]*[a-zA-Z_]\\w*)+)|\G([0-9]*[a-zA-Z_]\\w*)|\G(\\d+)|\G([`])|\G([|])|\G([.])|\G(\\s*[,]\\s*)|\G(\\s*[;]\\s*)|\G([:]{2})|\G(\\s*[:]\\s*)|\G(\\s*[?]\\s*)|\G(0[xX][0-9a-fA-F]+)|\G(\\s+)|\G([\S\s])/isS"; "\\s*)|\G([\"])|\G('[^'\\\\]*(?:\\\\.[^'\\\\]*)*')|\G([$]smarty\\.block\\.(child|parent))|\G([$][0-9]*[a-zA-Z_]\\w*)|\G([$])|\G(\\s+is\\s+in\\s+)|\G(\\s+as\\s+)|\G(\\s+to\\s+)|\G(\\s+step\\s+)|\G(\\s+instanceof\\s+)|\G(\\s*(([!=][=]{1,2})|([<][=>]?)|([>][=]?)|[&|]{2})\\s*)|\G(\\s+(eq|ne|neq|gt|ge|gte|lt|le|lte|mod|and|or|xor)\\s+)|\G(\\s+(is\\s+(not\\s+)?(odd|even|div)\\s+by)\\s+)|\G(\\s+is\\s+(not\\s+)?(odd|even))|\G(([!]\\s*)|(not\\s+))|\G([(](int(eger)?|bool(ean)?|float|double|real|string|binary|array|object)[)]\\s*)|\G(\\s*[(]\\s*)|\G(\\s*[)])|\G(\\[\\s*)|\G(\\s*\\])|\G(\\s*[-][>]\\s*)|\G(\\s*[=][>]\\s*)|\G(\\s*[=]\\s*)|\G(([+]|[-]){2})|\G(\\s*([+]|[-])\\s*)|\G(\\s*([*]{1,2}|[%\/^&]|[<>]{2})\\s*)|\G([@])|\G([#])|\G(\\s+[0-9]*[a-zA-Z_][a-zA-Z0-9_\-:]*\\s*[=]\\s*)|\G(([0-9]*[a-zA-Z_]\\w*)?(\\\\[0-9]*[a-zA-Z_]\\w*)+)|\G([0-9]*[a-zA-Z_]\\w*)|\G(\\d+)|\G([`])|\G([|])|\G([.])|\G(\\s*[,]\\s*)|\G(\\s*[;]\\s*)|\G([:]{2})|\G(\\s*[:]\\s*)|\G(\\s*[?]\\s*)|\G(0[xX][0-9a-fA-F]+)|\G(\\s+)|\G([\S\s])/isS";
} }
if ($this->counter >= strlen($this->data)) { if ($this->counter >= strlen($this->data)) {
return false; // end of input return false; // end of input
@@ -698,102 +698,108 @@ class Smarty_Internal_Templatelexer
} }
function yy_r3_19() function yy_r3_19()
{
$this->token = Smarty_Internal_Templateparser::TP_SLOGOP;
}
function yy_r3_21()
{ {
$this->token = Smarty_Internal_Templateparser::TP_TLOGOP; $this->token = Smarty_Internal_Templateparser::TP_TLOGOP;
} }
function yy_r3_24() function yy_r3_25()
{ {
$this->token = Smarty_Internal_Templateparser::TP_SINGLECOND; $this->token = Smarty_Internal_Templateparser::TP_SINGLECOND;
} }
function yy_r3_27() function yy_r3_28()
{ {
$this->token = Smarty_Internal_Templateparser::TP_NOT; $this->token = Smarty_Internal_Templateparser::TP_NOT;
} }
function yy_r3_30() function yy_r3_31()
{ {
$this->token = Smarty_Internal_Templateparser::TP_TYPECAST; $this->token = Smarty_Internal_Templateparser::TP_TYPECAST;
} }
function yy_r3_34() function yy_r3_35()
{ {
$this->token = Smarty_Internal_Templateparser::TP_OPENP; $this->token = Smarty_Internal_Templateparser::TP_OPENP;
} }
function yy_r3_35() function yy_r3_36()
{ {
$this->token = Smarty_Internal_Templateparser::TP_CLOSEP; $this->token = Smarty_Internal_Templateparser::TP_CLOSEP;
} }
function yy_r3_36() function yy_r3_37()
{ {
$this->token = Smarty_Internal_Templateparser::TP_OPENB; $this->token = Smarty_Internal_Templateparser::TP_OPENB;
} }
function yy_r3_37() function yy_r3_38()
{ {
$this->token = Smarty_Internal_Templateparser::TP_CLOSEB; $this->token = Smarty_Internal_Templateparser::TP_CLOSEB;
} }
function yy_r3_38() function yy_r3_39()
{ {
$this->token = Smarty_Internal_Templateparser::TP_PTR; $this->token = Smarty_Internal_Templateparser::TP_PTR;
} }
function yy_r3_39() function yy_r3_40()
{ {
$this->token = Smarty_Internal_Templateparser::TP_APTR; $this->token = Smarty_Internal_Templateparser::TP_APTR;
} }
function yy_r3_40() function yy_r3_41()
{ {
$this->token = Smarty_Internal_Templateparser::TP_EQUAL; $this->token = Smarty_Internal_Templateparser::TP_EQUAL;
} }
function yy_r3_41() function yy_r3_42()
{ {
$this->token = Smarty_Internal_Templateparser::TP_INCDEC; $this->token = Smarty_Internal_Templateparser::TP_INCDEC;
} }
function yy_r3_43() function yy_r3_44()
{ {
$this->token = Smarty_Internal_Templateparser::TP_UNIMATH; $this->token = Smarty_Internal_Templateparser::TP_UNIMATH;
} }
function yy_r3_45() function yy_r3_46()
{ {
$this->token = Smarty_Internal_Templateparser::TP_MATH; $this->token = Smarty_Internal_Templateparser::TP_MATH;
} }
function yy_r3_47() function yy_r3_48()
{ {
$this->token = Smarty_Internal_Templateparser::TP_AT; $this->token = Smarty_Internal_Templateparser::TP_AT;
} }
function yy_r3_48() function yy_r3_49()
{ {
$this->token = Smarty_Internal_Templateparser::TP_HATCH; $this->token = Smarty_Internal_Templateparser::TP_HATCH;
} }
function yy_r3_49() function yy_r3_50()
{ {
// resolve conflicts with shorttag and right_delimiter starting with '=' // resolve conflicts with shorttag and right_delimiter starting with '='
@@ -808,86 +814,86 @@ class Smarty_Internal_Templatelexer
} }
} }
function yy_r3_50() function yy_r3_51()
{ {
$this->token = Smarty_Internal_Templateparser::TP_NAMESPACE; $this->token = Smarty_Internal_Templateparser::TP_NAMESPACE;
} }
function yy_r3_53() function yy_r3_54()
{ {
$this->token = Smarty_Internal_Templateparser::TP_ID; $this->token = Smarty_Internal_Templateparser::TP_ID;
} }
function yy_r3_54() function yy_r3_55()
{ {
$this->token = Smarty_Internal_Templateparser::TP_INTEGER; $this->token = Smarty_Internal_Templateparser::TP_INTEGER;
} }
function yy_r3_55() function yy_r3_56()
{ {
$this->token = Smarty_Internal_Templateparser::TP_BACKTICK; $this->token = Smarty_Internal_Templateparser::TP_BACKTICK;
$this->yypopstate(); $this->yypopstate();
} }
function yy_r3_56() function yy_r3_57()
{ {
$this->token = Smarty_Internal_Templateparser::TP_VERT; $this->token = Smarty_Internal_Templateparser::TP_VERT;
} }
function yy_r3_57() function yy_r3_58()
{ {
$this->token = Smarty_Internal_Templateparser::TP_DOT; $this->token = Smarty_Internal_Templateparser::TP_DOT;
} }
function yy_r3_58() function yy_r3_59()
{ {
$this->token = Smarty_Internal_Templateparser::TP_COMMA; $this->token = Smarty_Internal_Templateparser::TP_COMMA;
} }
function yy_r3_59() function yy_r3_60()
{ {
$this->token = Smarty_Internal_Templateparser::TP_SEMICOLON; $this->token = Smarty_Internal_Templateparser::TP_SEMICOLON;
} }
function yy_r3_60() function yy_r3_61()
{ {
$this->token = Smarty_Internal_Templateparser::TP_DOUBLECOLON; $this->token = Smarty_Internal_Templateparser::TP_DOUBLECOLON;
} }
function yy_r3_61() function yy_r3_62()
{ {
$this->token = Smarty_Internal_Templateparser::TP_COLON; $this->token = Smarty_Internal_Templateparser::TP_COLON;
} }
function yy_r3_62() function yy_r3_63()
{ {
$this->token = Smarty_Internal_Templateparser::TP_QMARK; $this->token = Smarty_Internal_Templateparser::TP_QMARK;
} }
function yy_r3_63() function yy_r3_64()
{ {
$this->token = Smarty_Internal_Templateparser::TP_HEX; $this->token = Smarty_Internal_Templateparser::TP_HEX;
} }
function yy_r3_64() function yy_r3_65()
{ {
$this->token = Smarty_Internal_Templateparser::TP_SPACE; $this->token = Smarty_Internal_Templateparser::TP_SPACE;
} }
function yy_r3_65() function yy_r3_66()
{ {
$this->token = Smarty_Internal_Templateparser::TP_TEXT; $this->token = Smarty_Internal_Templateparser::TP_TEXT;

File diff suppressed because it is too large Load Diff