Skip to content

Commit

Permalink
- bugfix wrong precedence on special if conditions like '$foo is ... …
Browse files Browse the repository at this point in the history
…by $bar' could cause wrong code #178
  • Loading branch information
uwetews committed Feb 10, 2016
1 parent 417088b commit 47237eb
Show file tree
Hide file tree
Showing 6 changed files with 812 additions and 767 deletions.
1 change: 1 addition & 0 deletions change_log.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
 ===== 3.1.30-dev ===== (xx.xx.xx)
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 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
- move some code from parser into compiler
Expand Down
6 changes: 5 additions & 1 deletion lexer/smarty_internal_templatelexer.plex
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,8 @@ class Smarty_Internal_Templatelexer
literal = ~literal~
strip = ~strip~
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)~
isin = ~\s+is\s+in\s+~
as = ~\s+as\s+~
Expand Down Expand Up @@ -479,6 +480,9 @@ class Smarty_Internal_Templatelexer
lop {
$this->token = Smarty_Internal_Templateparser::TP_LOGOP;
}
slop {
$this->token = Smarty_Internal_Templateparser::TP_SLOGOP;
}
tlop {
$this->token = Smarty_Internal_Templateparser::TP_TLOGOP;
}
Expand Down
57 changes: 34 additions & 23 deletions lexer/smarty_internal_templateparser.y
Original file line number Diff line number Diff line change
Expand Up @@ -690,10 +690,15 @@ expr(res) ::= expr(e) modifierlist(l). {
}

// if expression
// special conditions
expr(res) ::= expr(e1) tlop(c) value(e2). {
res = c['pre']. e1.c['op'].e2 .')';
}
// simple expression
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). {
res = c . e1 . ')';
}
Expand Down Expand Up @@ -1222,34 +1227,40 @@ static_class_access(res) ::= DOLLARID(v) arrayindex(a) objectchain(oc). {

// if conditions and operators
lop(res) ::= LOGOP(o). {
res['op'] = ' '. trim(o) . ' ';
res = ' '. trim(o) . ' ';
}

lop(res) ::= TLOGOP(o). {
lop(res) ::= SLOGOP(o). {
static $lops = array(
'eq' => array('op' => ' == ', 'pre' => null),
'ne' => array('op' => ' != ', 'pre' => null),
'neq' => array('op' => ' != ', 'pre' => null),
'gt' => array('op' => ' > ', 'pre' => null),
'ge' => array('op' => ' >= ', 'pre' => null),
'gte' => array('op' => ' >= ', 'pre' => null),
'lt' => array('op' => ' < ', 'pre' => null),
'le' => array('op' => ' <= ', 'pre' => null),
'lte' => array('op' => ' <= ', 'pre' => null),
'mod' => array('op' => ' % ', 'pre' => null),
'and' => array('op' => ' && ', 'pre' => null),
'or' => array('op' => ' || ', 'pre' => null),
'xor' => array('op' => ' xor ', 'pre' => null),
'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 & '),
);
'eq' => ' == ',
'ne' => ' != ',
'neq' => ' != ',
'gt' => ' > ',
'ge' => ' >= ',
'gte' => ' >= ',
'lt' => ' < ',
'le' => ' <= ',
'lte' => ' <= ',
'mod' => ' % ',
'and' => ' && ',
'or' => ' || ',
'xor' => ' xor ',
);
$op = strtolower(preg_replace('/\s*/', '', o));
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). {
static $scond = array (
Expand Down
2 changes: 1 addition & 1 deletion libs/Smarty.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ class Smarty extends Smarty_Internal_TemplateBase
/**
* smarty version
*/
const SMARTY_VERSION = '3.1.30-dev/34';
const SMARTY_VERSION = '3.1.30-dev/35';

/**
* define variable scopes
Expand Down
68 changes: 37 additions & 31 deletions libs/sysplugins/smarty_internal_templatelexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,7 @@ public function yylex3()
{
if (!isset($this->yy_global_pattern3)) {
$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)) {
return false; // end of input
Expand Down Expand Up @@ -698,102 +698,108 @@ function yy_r3_14()
}

function yy_r3_19()
{

$this->token = Smarty_Internal_Templateparser::TP_SLOGOP;
}

function yy_r3_21()
{

$this->token = Smarty_Internal_Templateparser::TP_TLOGOP;
}

function yy_r3_24()
function yy_r3_25()
{

$this->token = Smarty_Internal_Templateparser::TP_SINGLECOND;
}

function yy_r3_27()
function yy_r3_28()
{

$this->token = Smarty_Internal_Templateparser::TP_NOT;
}

function yy_r3_30()
function yy_r3_31()
{

$this->token = Smarty_Internal_Templateparser::TP_TYPECAST;
}

function yy_r3_34()
function yy_r3_35()
{

$this->token = Smarty_Internal_Templateparser::TP_OPENP;
}

function yy_r3_35()
function yy_r3_36()
{

$this->token = Smarty_Internal_Templateparser::TP_CLOSEP;
}

function yy_r3_36()
function yy_r3_37()
{

$this->token = Smarty_Internal_Templateparser::TP_OPENB;
}

function yy_r3_37()
function yy_r3_38()
{

$this->token = Smarty_Internal_Templateparser::TP_CLOSEB;
}

function yy_r3_38()
function yy_r3_39()
{

$this->token = Smarty_Internal_Templateparser::TP_PTR;
}

function yy_r3_39()
function yy_r3_40()
{

$this->token = Smarty_Internal_Templateparser::TP_APTR;
}

function yy_r3_40()
function yy_r3_41()
{

$this->token = Smarty_Internal_Templateparser::TP_EQUAL;
}

function yy_r3_41()
function yy_r3_42()
{

$this->token = Smarty_Internal_Templateparser::TP_INCDEC;
}

function yy_r3_43()
function yy_r3_44()
{

$this->token = Smarty_Internal_Templateparser::TP_UNIMATH;
}

function yy_r3_45()
function yy_r3_46()
{

$this->token = Smarty_Internal_Templateparser::TP_MATH;
}

function yy_r3_47()
function yy_r3_48()
{

$this->token = Smarty_Internal_Templateparser::TP_AT;
}

function yy_r3_48()
function yy_r3_49()
{

$this->token = Smarty_Internal_Templateparser::TP_HATCH;
}

function yy_r3_49()
function yy_r3_50()
{

// resolve conflicts with shorttag and right_delimiter starting with '='
Expand All @@ -808,86 +814,86 @@ function yy_r3_49()
}
}

function yy_r3_50()
function yy_r3_51()
{

$this->token = Smarty_Internal_Templateparser::TP_NAMESPACE;
}

function yy_r3_53()
function yy_r3_54()
{

$this->token = Smarty_Internal_Templateparser::TP_ID;
}

function yy_r3_54()
function yy_r3_55()
{

$this->token = Smarty_Internal_Templateparser::TP_INTEGER;
}

function yy_r3_55()
function yy_r3_56()
{

$this->token = Smarty_Internal_Templateparser::TP_BACKTICK;
$this->yypopstate();
}

function yy_r3_56()
function yy_r3_57()
{

$this->token = Smarty_Internal_Templateparser::TP_VERT;
}

function yy_r3_57()
function yy_r3_58()
{

$this->token = Smarty_Internal_Templateparser::TP_DOT;
}

function yy_r3_58()
function yy_r3_59()
{

$this->token = Smarty_Internal_Templateparser::TP_COMMA;
}

function yy_r3_59()
function yy_r3_60()
{

$this->token = Smarty_Internal_Templateparser::TP_SEMICOLON;
}

function yy_r3_60()
function yy_r3_61()
{

$this->token = Smarty_Internal_Templateparser::TP_DOUBLECOLON;
}

function yy_r3_61()
function yy_r3_62()
{

$this->token = Smarty_Internal_Templateparser::TP_COLON;
}

function yy_r3_62()
function yy_r3_63()
{

$this->token = Smarty_Internal_Templateparser::TP_QMARK;
}

function yy_r3_63()
function yy_r3_64()
{

$this->token = Smarty_Internal_Templateparser::TP_HEX;
}

function yy_r3_64()
function yy_r3_65()
{

$this->token = Smarty_Internal_Templateparser::TP_SPACE;
}

function yy_r3_65()
function yy_r3_66()
{

$this->token = Smarty_Internal_Templateparser::TP_TEXT;
Expand Down
Loading

0 comments on commit 47237eb

Please sign in to comment.