Skip to content

Commit 139c91b

Browse files
Safeguard against preg_replace_callback() failure
1 parent 03c0b92 commit 139c91b

2 files changed

Lines changed: 32 additions & 1 deletion

File tree

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php declare(strict_types=1);
2+
/*
3+
* This file is part of phpunit/php-code-coverage.
4+
*
5+
* (c) Sebastian Bergmann <sebastian@phpunit.de>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
namespace SebastianBergmann\CodeCoverage\Serialization;
11+
12+
use RuntimeException;
13+
use SebastianBergmann\CodeCoverage\Exception;
14+
15+
final class PharPrefixCouldNotBeStrippedException extends RuntimeException implements Exception
16+
{
17+
public function __construct()
18+
{
19+
parent::__construct('Failed to strip PHAR prefix from serialized data');
20+
}
21+
}

src/Serialization/Serializer.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@
5757
{
5858
/**
5959
* @param non-empty-string $target
60+
*
61+
* @throws PharPrefixCouldNotBeStrippedException
6062
*/
6163
public function serialize(string $target, CodeCoverage $codeCoverage, bool $includeGitInformation = false): void
6264
{
@@ -120,17 +122,25 @@ public function serialize(string $target, CodeCoverage $codeCoverage, bool $incl
120122
/**
121123
* @param non-empty-string $serialized
122124
*
125+
* @throws PharPrefixCouldNotBeStrippedException
126+
*
123127
* @return non-empty-string
124128
*/
125129
private function stripPharPrefix(string $serialized): string
126130
{
127-
return preg_replace_callback(
131+
$result = preg_replace_callback(
128132
'/([OCs]):(\d+):"(\x00?)PHPUnitPHAR\\\\/',
129133
static function (array $matches): string
130134
{
131135
return $matches[1] . ':' . ((int) $matches[2] - 12) . ':"' . $matches[3];
132136
},
133137
$serialized,
134138
);
139+
140+
if ($result === null) {
141+
throw new PharPrefixCouldNotBeStrippedException;
142+
}
143+
144+
return $result;
135145
}
136146
}

0 commit comments

Comments
 (0)