From 3b3192933d51d7d6242639dcdb021a90a86f5084 Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Fri, 2 Dec 2022 07:48:44 -0500 Subject: [PATCH 1/2] Ensure null values aren't processed in Content\Text\Markdown::toBBCode - Address part of https://github.com/friendica/friendica/issues/12011#issuecomment-1335124938 --- src/Content/Text/Markdown.php | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/Content/Text/Markdown.php b/src/Content/Text/Markdown.php index 3575c69a8..53983113b 100644 --- a/src/Content/Text/Markdown.php +++ b/src/Content/Text/Markdown.php @@ -21,6 +21,8 @@ namespace Friendica\Content\Text; +use Friendica\Core\Logger; +use Friendica\Core\System; use Friendica\DI; use Friendica\Model\Contact; @@ -106,8 +108,18 @@ class Markdown * So we'll use that to convert to HTML, then convert the HTML back to bbcode, * and then clean up a few Diaspora specific constructs. */ - public static function toBBCode($s) + public static function toBBCode($s): string { + // @TODO Temporary until we find the source of the null value to finally set the correct type-hint + if (is_null($s)) { + Logger::warning('Received null value', ['callstack' => System::callstack()]); + return ''; + } + + if (!$s) { + return $s; + } + DI::profiler()->startRecording('rendering'); // The parser cannot handle paragraphs correctly From e6f8b8c6e0ab6889881fb208bbff4d47676ce528 Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Fri, 2 Dec 2022 08:10:16 -0500 Subject: [PATCH 2/2] Ward against preg_replace_callback null return value in Strings::performWithEscapedBlocks - Add logging to troubleshoot potential issue with regex - Address part of https://github.com/friendica/friendica/issues/12011#issuecomment-1335124938 --- src/Util/Strings.php | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/Util/Strings.php b/src/Util/Strings.php index 0d7bf6691..379f2a252 100644 --- a/src/Util/Strings.php +++ b/src/Util/Strings.php @@ -23,6 +23,7 @@ namespace Friendica\Util; use Friendica\Content\ContactSelector; use Friendica\Core\Logger; +use Friendica\Core\System; use ParagonIE\ConstantTime\Base64; /** @@ -480,7 +481,7 @@ class Strings $blocks = []; - $text = preg_replace_callback($regex, + $return = preg_replace_callback($regex, function ($matches) use ($executionId, &$blocks) { $return = '«block-' . $executionId . '-' . count($blocks) . '»'; @@ -491,7 +492,11 @@ class Strings $text ); - $text = $callback($text) ?? ''; + if (is_null($return)) { + Logger::warning('Received null value from preg_replace_callback', ['text' => $text, 'regex' => $regex, 'blocks' => $blocks, 'executionId' => $executionId, 'callstack' => System::callstack(10)]); + } + + $text = $callback($return ?? $text) ?? ''; // Restore code blocks $text = preg_replace_callback('/«block-' . $executionId . '-([0-9]+)»/iU',