Skip to content

Commit d172472

Browse files
committed
minor #43778 Use try/finally to restore error handlers (derrabus)
This PR was merged into the 5.4 branch. Discussion ---------- Use try/finally to restore error handlers | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | no | New feature? | no | Deprecations? | no | Tickets | N/A | License | MIT | Doc PR | N/A This PR attempts to make some code dealing with temporary error handlers a bit more robust by making sure the old error handler is always restored. A forgotten error handler is pretty hard to debug, so I'd like our code to be a it more defensive here. Commits ------- 057716c6a2 Use try/finally to restore error handlers
2 parents 962bf47 + 861d094 commit d172472

File tree

1 file changed

+30
-27
lines changed

1 file changed

+30
-27
lines changed

Matcher/Dumper/StaticPrefixCollection.php

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -151,40 +151,43 @@ private function getCommonPrefix(string $prefix, string $anotherPrefix): array
151151
$staticLength = null;
152152
set_error_handler([__CLASS__, 'handleError']);
153153

154-
for ($i = $baseLength; $i < $end && $prefix[$i] === $anotherPrefix[$i]; ++$i) {
155-
if ('(' === $prefix[$i]) {
156-
$staticLength = $staticLength ?? $i;
157-
for ($j = 1 + $i, $n = 1; $j < $end && 0 < $n; ++$j) {
158-
if ($prefix[$j] !== $anotherPrefix[$j]) {
159-
break 2;
154+
try {
155+
for ($i = $baseLength; $i < $end && $prefix[$i] === $anotherPrefix[$i]; ++$i) {
156+
if ('(' === $prefix[$i]) {
157+
$staticLength = $staticLength ?? $i;
158+
for ($j = 1 + $i, $n = 1; $j < $end && 0 < $n; ++$j) {
159+
if ($prefix[$j] !== $anotherPrefix[$j]) {
160+
break 2;
161+
}
162+
if ('(' === $prefix[$j]) {
163+
++$n;
164+
} elseif (')' === $prefix[$j]) {
165+
--$n;
166+
} elseif ('\\' === $prefix[$j] && (++$j === $end || $prefix[$j] !== $anotherPrefix[$j])) {
167+
--$j;
168+
break;
169+
}
160170
}
161-
if ('(' === $prefix[$j]) {
162-
++$n;
163-
} elseif (')' === $prefix[$j]) {
164-
--$n;
165-
} elseif ('\\' === $prefix[$j] && (++$j === $end || $prefix[$j] !== $anotherPrefix[$j])) {
166-
--$j;
171+
if (0 < $n) {
167172
break;
168173
}
169-
}
170-
if (0 < $n) {
171-
break;
172-
}
173-
if (('?' === ($prefix[$j] ?? '') || '?' === ($anotherPrefix[$j] ?? '')) && ($prefix[$j] ?? '') !== ($anotherPrefix[$j] ?? '')) {
174-
break;
175-
}
176-
$subPattern = substr($prefix, $i, $j - $i);
177-
if ($prefix !== $anotherPrefix && !preg_match('/^\(\[[^\]]++\]\+\+\)$/', $subPattern) && !preg_match('{(?<!'.$subPattern.')}', '')) {
178-
// sub-patterns of variable length are not considered as common prefixes because their greediness would break in-order matching
174+
if (('?' === ($prefix[$j] ?? '') || '?' === ($anotherPrefix[$j] ?? '')) && ($prefix[$j] ?? '') !== ($anotherPrefix[$j] ?? '')) {
175+
break;
176+
}
177+
$subPattern = substr($prefix, $i, $j - $i);
178+
if ($prefix !== $anotherPrefix && !preg_match('/^\(\[[^\]]++\]\+\+\)$/', $subPattern) && !preg_match('{(?<!'.$subPattern.')}', '')) {
179+
// sub-patterns of variable length are not considered as common prefixes because their greediness would break in-order matching
180+
break;
181+
}
182+
$i = $j - 1;
183+
} elseif ('\\' === $prefix[$i] && (++$i === $end || $prefix[$i] !== $anotherPrefix[$i])) {
184+
--$i;
179185
break;
180186
}
181-
$i = $j - 1;
182-
} elseif ('\\' === $prefix[$i] && (++$i === $end || $prefix[$i] !== $anotherPrefix[$i])) {
183-
--$i;
184-
break;
185187
}
188+
} finally {
189+
restore_error_handler();
186190
}
187-
restore_error_handler();
188191
if ($i < $end && 0b10 === (\ord($prefix[$i]) >> 6) && preg_match('//u', $prefix.' '.$anotherPrefix)) {
189192
do {
190193
// Prevent cutting in the middle of an UTF-8 characters

0 commit comments

Comments
 (0)