Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitf09da16

Browse files
committed
[ExpressionLanguage] Fix matches when the regexp is not valid
1 parentc3ba272 commitf09da16

File tree

2 files changed

+59
-4
lines changed

2 files changed

+59
-4
lines changed

‎src/Symfony/Component/ExpressionLanguage/Node/BinaryNode.php‎

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespaceSymfony\Component\ExpressionLanguage\Node;
1313

1414
useSymfony\Component\ExpressionLanguage\Compiler;
15+
useSymfony\Component\ExpressionLanguage\SyntaxError;
1516

1617
/**
1718
* @author Fabien Potencier <fabien@symfony.com>
@@ -46,8 +47,12 @@ public function compile(Compiler $compiler)
4647
$operator =$this->attributes['operator'];
4748

4849
if ('matches' ==$operator) {
50+
if ($this->nodes['right']instanceof ConstantNode) {
51+
$this->evaluateMatches($this->nodes['right']->evaluate([], []),'');
52+
}
53+
4954
$compiler
50-
->raw('preg_match(')
55+
->raw('(static function ($regexp, $str) { set_error_handler(function ($t, $m) use ($regexp, $str) { throw new \Symfony\Component\ExpressionLanguage\SyntaxError(sprintf(\'Regexp "%s" passed to "matches" is not valid\', $regexp).substr($m, 12)); }); try { returnpreg_match($regexp, $str); } finally { restore_error_handler(); } })(')
5156
->compile($this->nodes['right'])
5257
->raw(',')
5358
->compile($this->nodes['left'])
@@ -159,12 +164,24 @@ public function evaluate($functions, $values)
159164

160165
return$left %$right;
161166
case'matches':
162-
returnpreg_match($right,$left);
167+
return$this->evaluateMatches($right,$left);
163168
}
164169
}
165170

166171
publicfunctiontoArray()
167172
{
168173
return ['(',$this->nodes['left'],''.$this->attributes['operator'].'',$this->nodes['right'],')'];
169174
}
175+
176+
privatefunctionevaluateMatches(string$regexp,string$str):int
177+
{
178+
set_error_handler(function ($t,$m)use ($regexp) {
179+
thrownewSyntaxError(sprintf('Regexp "%s" passed to "matches" is not valid',$regexp).substr($m,12));
180+
});
181+
try {
182+
returnpreg_match($regexp,$str);
183+
}finally {
184+
restore_error_handler();
185+
}
186+
}
170187
}

‎src/Symfony/Component/ExpressionLanguage/Tests/Node/BinaryNodeTest.php‎

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,12 @@
1111

1212
namespaceSymfony\Component\ExpressionLanguage\Tests\Node;
1313

14+
useSymfony\Component\ExpressionLanguage\Compiler;
1415
useSymfony\Component\ExpressionLanguage\Node\ArrayNode;
1516
useSymfony\Component\ExpressionLanguage\Node\BinaryNode;
1617
useSymfony\Component\ExpressionLanguage\Node\ConstantNode;
18+
useSymfony\Component\ExpressionLanguage\Node\NameNode;
19+
useSymfony\Component\ExpressionLanguage\SyntaxError;
1720

1821
class BinaryNodeTestextends AbstractNodeTest
1922
{
@@ -111,7 +114,7 @@ public function getCompileData()
111114

112115
['range(1, 3)',newBinaryNode('..',newConstantNode(1),newConstantNode(3))],
113116

114-
['preg_match("/^[a-z]+/i\$/", "abc")',newBinaryNode('matches',newConstantNode('abc'),newConstantNode('/^[a-z]+/i$/'))],
117+
['(static function ($regexp, $str) { set_error_handler(function ($t, $m) use ($regexp, $str) { throw new \Symfony\Component\ExpressionLanguage\SyntaxError(sprintf(\'Regexp "%s" passed to "matches" is not valid\', $regexp).substr($m, 12)); }); try { returnpreg_match($regexp, $str); } finally { restore_error_handler(); } })("/^[a-z]+\$/", "abc")',newBinaryNode('matches',newConstantNode('abc'),newConstantNode('/^[a-z]+$/'))],
115118
];
116119
}
117120

@@ -160,7 +163,42 @@ public function getDumpData()
160163

161164
['(1 .. 3)',newBinaryNode('..',newConstantNode(1),newConstantNode(3))],
162165

163-
['("abc" matches "/^[a-z]+/i$/")',newBinaryNode('matches',newConstantNode('abc'),newConstantNode('/^[a-z]+/i$/'))],
166+
['("abc" matches "/^[a-z]+$/")',newBinaryNode('matches',newConstantNode('abc'),newConstantNode('/^[a-z]+$/'))],
164167
];
165168
}
169+
170+
publicfunctiontestEvaluateMatchesWithInvalidRegexp()
171+
{
172+
$node =newBinaryNode('matches',newConstantNode('abc'),newConstantNode('this is not a regexp'));
173+
174+
$this->expectExceptionObject(newSyntaxError('Regexp "this is not a regexp" passed to "matches" is not valid: Delimiter must not be alphanumeric or backslash'));
175+
$node->evaluate([], []);
176+
}
177+
178+
publicfunctiontestEvaluateMatchesWithInvalidRegexpAsExpression()
179+
{
180+
$node =newBinaryNode('matches',newConstantNode('abc'),newNameNode('regexp'));
181+
182+
$this->expectExceptionObject(newSyntaxError('Regexp "this is not a regexp" passed to "matches" is not valid: Delimiter must not be alphanumeric or backslash'));
183+
$node->evaluate([], ['regexp' =>'this is not a regexp']);
184+
}
185+
186+
publicfunctiontestCompileMatchesWithInvalidRegexp()
187+
{
188+
$node =newBinaryNode('matches',newConstantNode('abc'),newConstantNode('this is not a regexp'));
189+
190+
$this->expectExceptionObject(newSyntaxError('Regexp "this is not a regexp" passed to "matches" is not valid: Delimiter must not be alphanumeric or backslash'));
191+
$compiler =newCompiler([]);
192+
$node->compile($compiler);
193+
}
194+
195+
publicfunctiontestCompileMatchesWithInvalidRegexpAsExpression()
196+
{
197+
$node =newBinaryNode('matches',newConstantNode('abc'),newNameNode('regexp'));
198+
199+
$this->expectExceptionObject(newSyntaxError('Regexp "this is not a regexp" passed to "matches" is not valid: Delimiter must not be alphanumeric or backslash'));
200+
$compiler =newCompiler([]);
201+
$node->compile($compiler);
202+
eval('$regexp = "this is not a regexp";'.$compiler->getSource().';');
203+
}
166204
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp