Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3326da0

Browse files
ausifabpot
authored andcommitted
[ExpressionLanguage] Fix matching null against a regular expression
1 parent 8d3d625 commit 3326da0

File tree

2 files changed

+6
-4
lines changed

2 files changed

+6
-4
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public function compile(Compiler $compiler)
5252
}
5353

5454
$compiler
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 { return preg_match($regexp, $str); } finally { restore_error_handler(); } })(')
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 { return preg_match($regexp, (string) $str); } finally { restore_error_handler(); } })(')
5656
->compile($this->nodes['right'])
5757
->raw(', ')
5858
->compile($this->nodes['left'])
@@ -173,13 +173,13 @@ public function toArray()
173173
return ['(', $this->nodes['left'], ' '.$this->attributes['operator'].' ', $this->nodes['right'], ')'];
174174
}
175175

176-
private function evaluateMatches(string $regexp, string $str): int
176+
private function evaluateMatches(string $regexp, ?string $str): int
177177
{
178178
set_error_handler(function ($t, $m) use ($regexp) {
179179
throw new SyntaxError(sprintf('Regexp "%s" passed to "matches" is not valid', $regexp).substr($m, 12));
180180
});
181181
try {
182-
return preg_match($regexp, $str);
182+
return preg_match($regexp, (string) $str);
183183
} finally {
184184
restore_error_handler();
185185
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ public function getEvaluateData()
6666
[[1, 2, 3], new BinaryNode('..', new ConstantNode(1), new ConstantNode(3))],
6767

6868
[1, new BinaryNode('matches', new ConstantNode('abc'), new ConstantNode('/^[a-z]+$/'))],
69+
[0, new BinaryNode('matches', new ConstantNode(''), new ConstantNode('/^[a-z]+$/'))],
70+
[0, new BinaryNode('matches', new ConstantNode(null), new ConstantNode('/^[a-z]+$/'))],
6971
];
7072
}
7173

@@ -114,7 +116,7 @@ public function getCompileData()
114116

115117
['range(1, 3)', new BinaryNode('..', new ConstantNode(1), new ConstantNode(3))],
116118

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 { return preg_match($regexp, $str); } finally { restore_error_handler(); } })("/^[a-z]+\$/", "abc")', new BinaryNode('matches', new ConstantNode('abc'), new ConstantNode('/^[a-z]+$/'))],
119+
['(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 { return preg_match($regexp, (string) $str); } finally { restore_error_handler(); } })("/^[a-z]+\$/", "abc")', new BinaryNode('matches', new ConstantNode('abc'), new ConstantNode('/^[a-z]+$/'))],
118120
];
119121
}
120122

0 commit comments

Comments
 (0)