Skip to content

Commit 80b2f15

Browse files
committed
Safely parse preg_match_all matches
fixes #54
1 parent 298f968 commit 80b2f15

File tree

4 files changed

+28
-12
lines changed

4 files changed

+28
-12
lines changed

src/Svg/Tag/AbstractTag.php

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -135,21 +135,19 @@ protected function applyTransform($attributes)
135135

136136
$transform = $attributes["transform"];
137137

138-
$match = array();
138+
$matches = array();
139139
preg_match_all(
140-
'/(matrix|translate|scale|rotate|skewX|skewY)\((.*?)\)/is',
140+
'/(matrix|translate|scale|rotate|skew|skewX|skewY)\((.*?)\)/is',
141141
$transform,
142-
$match,
142+
$matches,
143143
PREG_SET_ORDER
144144
);
145145

146146
$transformations = array();
147-
if (count($match[0])) {
148-
foreach ($match as $_match) {
149-
$arguments = preg_split('/[ ,]+/', $_match[2]);
150-
array_unshift($arguments, $_match[1]);
151-
$transformations[] = $arguments;
152-
}
147+
foreach ($matches as $match) {
148+
$arguments = preg_split('/[ ,]+/', $match[2]);
149+
array_unshift($arguments, $match[1]);
150+
$transformations[] = $arguments;
153151
}
154152

155153
foreach ($transformations as $t) {

src/Svg/Tag/Path.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public static function parse(string $commandSequence): array
6161
$commandLower = strtolower($c[1]);
6262

6363
// arcs have special flags that apparently don't require spaces.
64-
if ($commandLower === 'a' && preg_match_all(static::ARC_REGEXP, $c[2], $matches)) {
64+
if ($commandLower === 'a' && preg_match_all(static::ARC_REGEXP, $c[2], $matches, PREG_PATTERN_ORDER)) {
6565
$numberOfMatches = count($matches[0]);
6666
for ($k = 0; $k < $numberOfMatches; ++$k) {
6767
$path[] = [

src/Svg/Tag/Polygon.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,25 @@ class Polygon extends Shape
1313
public function start($attributes)
1414
{
1515
$tmp = array();
16-
preg_match_all('/([\-]*[0-9\.]+)/', $attributes['points'], $tmp);
16+
preg_match_all('/([\-]*[0-9\.]+)/', $attributes['points'], $tmp, PREG_PATTERN_ORDER);
1717

1818
$points = $tmp[0];
1919
$count = count($points);
2020

21+
if ($count < 4) {
22+
// nothing to draw
23+
return;
24+
}
25+
2126
$surface = $this->document->getSurface();
2227
list($x, $y) = $points;
2328
$surface->moveTo($x, $y);
2429

2530
for ($i = 2; $i < $count; $i += 2) {
31+
if ($i + 1 === $count) {
32+
// invalid trailing point
33+
continue;
34+
}
2635
$x = $points[$i];
2736
$y = $points[$i + 1];
2837
$surface->lineTo($x, $y);

src/Svg/Tag/Polyline.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,25 @@ class Polyline extends Shape
1313
public function start($attributes)
1414
{
1515
$tmp = array();
16-
preg_match_all('/([\-]*[0-9\.]+)/', $attributes['points'], $tmp);
16+
preg_match_all('/([\-]*[0-9\.]+)/', $attributes['points'], $tmp, PREG_PATTERN_ORDER);
1717

1818
$points = $tmp[0];
1919
$count = count($points);
2020

21+
if ($count < 4) {
22+
// nothing to draw
23+
return;
24+
}
25+
2126
$surface = $this->document->getSurface();
2227
list($x, $y) = $points;
2328
$surface->moveTo($x, $y);
2429

2530
for ($i = 2; $i < $count; $i += 2) {
31+
if ($i + 1 === $count) {
32+
// invalid trailing point
33+
continue;
34+
}
2635
$x = $points[$i];
2736
$y = $points[$i + 1];
2837
$surface->lineTo($x, $y);

0 commit comments

Comments
 (0)