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

Skip to content

Commit b63926b

Browse files
ymc-dabefabpot
authored andcommitted
[Finder] Escape location for regex searches
1 parent 3da421f commit b63926b

File tree

3 files changed

+22
-8
lines changed

3 files changed

+22
-8
lines changed

src/Symfony/Component/Finder/Adapter/AbstractFindAdapter.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ private function buildPathsFiltering(Command $command, $dir, array $paths, $not
216216
// Fixes 'not search' regex problems.
217217
if ($expr->isRegex()) {
218218
$regex = $expr->getRegex();
219-
$regex->prepend($regex->hasStartFlag() ? $dir.DIRECTORY_SEPARATOR : '.*')->setEndJoker(!$regex->hasEndFlag());
219+
$regex->prepend($regex->hasStartFlag() ? preg_quote($dir).DIRECTORY_SEPARATOR : '.*')->setEndJoker(!$regex->hasEndFlag());
220220
} else {
221221
$expr->prepend('*')->append('*');
222222
}
@@ -247,7 +247,7 @@ private function buildSizesFiltering(Command $command, array $sizes)
247247
$command->add('-size -'.($size->getTarget() + 1).'c');
248248
break;
249249
case '>=':
250-
$command->add('-size +'. ($size->getTarget() - 1).'c');
250+
$command->add('-size +'.($size->getTarget() - 1).'c');
251251
break;
252252
case '>':
253253
$command->add('-size +'.$size->getTarget().'c');

src/Symfony/Component/Finder/Tests/FinderTest.php

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,20 @@ public function testIteratorKeys(Adapter\AdapterInterface $adapter)
569569
}
570570
}
571571

572+
/**
573+
* @dataProvider getAdaptersTestData
574+
*/
575+
public function testRegexSpecialCharsLocationWithPathRestrictionContainingStartFlag(Adapter\AdapterInterface $adapter)
576+
{
577+
$finder = $this->buildFinder($adapter);
578+
$finder->in(__DIR__.DIRECTORY_SEPARATOR.'Fixtures'.DIRECTORY_SEPARATOR.'r+e.g?e*x[c]a(r)s')
579+
->path('/^dir/');
580+
581+
$expected = array('r+e.g?e*x[c]a(r)s'.DIRECTORY_SEPARATOR.'dir',
582+
'r+e.g?e*x[c]a(r)s'.DIRECTORY_SEPARATOR.'dir'.DIRECTORY_SEPARATOR.'bar.dat',);
583+
$this->assertIterator($this->toAbsoluteFixtures($expected), $finder);
584+
}
585+
572586
public function testAdaptersOrdering()
573587
{
574588
$finder = Finder::create()
@@ -673,23 +687,23 @@ public function getTestPathData()
673687
$tests = array(
674688
array('', '', array()),
675689
array('/^A\/B\/C/', '/C$/',
676-
array('A'.DIRECTORY_SEPARATOR.'B'.DIRECTORY_SEPARATOR.'C'.DIRECTORY_SEPARATOR.'abc.dat')
690+
array('A'.DIRECTORY_SEPARATOR.'B'.DIRECTORY_SEPARATOR.'C'.DIRECTORY_SEPARATOR.'abc.dat'),
677691
),
678692
array('/^A\/B/', 'foobar',
679693
array(
680694
'A'.DIRECTORY_SEPARATOR.'B',
681695
'A'.DIRECTORY_SEPARATOR.'B'.DIRECTORY_SEPARATOR.'C',
682696
'A'.DIRECTORY_SEPARATOR.'B'.DIRECTORY_SEPARATOR.'ab.dat',
683697
'A'.DIRECTORY_SEPARATOR.'B'.DIRECTORY_SEPARATOR.'C'.DIRECTORY_SEPARATOR.'abc.dat',
684-
)
698+
),
685699
),
686700
array('A/B/C', 'foobar',
687701
array(
688702
'A'.DIRECTORY_SEPARATOR.'B'.DIRECTORY_SEPARATOR.'C',
689703
'A'.DIRECTORY_SEPARATOR.'B'.DIRECTORY_SEPARATOR.'C'.DIRECTORY_SEPARATOR.'abc.dat',
690704
'copy'.DIRECTORY_SEPARATOR.'A'.DIRECTORY_SEPARATOR.'B'.DIRECTORY_SEPARATOR.'C',
691705
'copy'.DIRECTORY_SEPARATOR.'A'.DIRECTORY_SEPARATOR.'B'.DIRECTORY_SEPARATOR.'C'.DIRECTORY_SEPARATOR.'abc.dat.copy',
692-
)
706+
),
693707
),
694708
array('A/B', 'foobar',
695709
array(
@@ -703,12 +717,12 @@ public function getTestPathData()
703717
'A'.DIRECTORY_SEPARATOR.'B'.DIRECTORY_SEPARATOR.'C'.DIRECTORY_SEPARATOR.'abc.dat',
704718
'copy'.DIRECTORY_SEPARATOR.'A'.DIRECTORY_SEPARATOR.'B'.DIRECTORY_SEPARATOR.'ab.dat.copy',
705719
'copy'.DIRECTORY_SEPARATOR.'A'.DIRECTORY_SEPARATOR.'B'.DIRECTORY_SEPARATOR.'C'.DIRECTORY_SEPARATOR.'abc.dat.copy',
706-
)
720+
),
707721
),
708722
array('/^with space\//', 'foobar',
709723
array(
710724
'with space'.DIRECTORY_SEPARATOR.'foo.txt',
711-
)
725+
),
712726
),
713727
);
714728

@@ -808,7 +822,7 @@ private function getValidAdapters()
808822
array(
809823
new Adapter\BsdFindAdapter(),
810824
new Adapter\GnuFindAdapter(),
811-
new Adapter\PhpAdapter()
825+
new Adapter\PhpAdapter(),
812826
),
813827
function (Adapter\AdapterInterface $adapter) {
814828
return $adapter->isSupported();

src/Symfony/Component/Finder/Tests/Fixtures/r+e.g?e*x[c]a(r)s/dir/bar.dat

Whitespace-only changes.

0 commit comments

Comments
 (0)