Skip to content

Commit 4f60ca0

Browse files
authored
Merge pull request #370 from essell/fix/laravel-context-parsing
Fix PCRE stack limit for context parsing, omitting preg
2 parents 02d5e4f + 1b76585 commit 4f60ca0

File tree

1 file changed

+27
-5
lines changed

1 file changed

+27
-5
lines changed

src/Logs/LaravelLog.php

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,19 +106,17 @@ protected static function regexPattern(): string
106106

107107
protected function extractContextsFromFullText(): void
108108
{
109-
// The regex pattern to find JSON strings.
110-
$pattern = '/(\{(?:[^{}]|(?R))*\}|\[(?:[^\[\]]|(?R))*\])/';
111109
$contexts = [];
112110

113111
// Find matches.
114-
preg_match_all($pattern, $this->text, $matches);
112+
$json_strings = $this->getJsonStringsFromFullText();
115113

116-
if (! isset($matches[0])) {
114+
if (empty($json_strings)) {
117115
return;
118116
}
119117

120118
// Loop through the matches.
121-
foreach ($matches[0] as $json_string) {
119+
foreach ($json_strings as $json_string) {
122120
// Try to decode the JSON string. If it fails, json_last_error() will return a non-zero value.
123121
$json_data = json_decode(trim($json_string), true);
124122

@@ -181,4 +179,28 @@ protected function extractMailPreview(): void
181179
'size_formatted' => Utils::bytesForHumans($message->getSize()),
182180
];
183181
}
182+
183+
protected function getJsonStringsFromFullText(): array
184+
{
185+
$json = '';
186+
$json_strings = [];
187+
$in = 0;
188+
foreach (str_split($this->text) as $char) {
189+
if ($char == '{' || $char == '[') {
190+
$in++;
191+
}
192+
if ($in) {
193+
$json .= $char;
194+
}
195+
if ($char == '}' || $char == ']') {
196+
$in--;
197+
if ($in == 0) {
198+
$json_strings[] = $json;
199+
$json = '';
200+
}
201+
}
202+
}
203+
204+
return $json_strings;
205+
}
184206
}

0 commit comments

Comments
 (0)