Page MenuHomePhorge

Fix PHP 8.1 "preg_match()" exception when pasting malformed Raw Diff into "Create Diff"

Authored by aklapper on May 3 2023, 19:51.
Referenced Files
Unknown Object (File)
Wed, May 31, 06:35
Unknown Object (File)
Tue, May 30, 14:07
Unknown Object (File)
Sat, May 27, 17:10
Unknown Object (File)
Wed, May 24, 06:28
Unknown Object (File)
Sun, May 21, 20:50
Unknown Object (File)
Thu, May 18, 05:38
Unknown Object (File)
Thu, May 11, 19:34



Entering a malformed string (like "pHoRgE rOcKs!!") into the "Raw Diff" field, Arcanist's tryMatchHeader() function is first called in $ok = $this->tryMatchHeader($patterns, $line, $match) with a non-null $line value (the first line entered in the "Raw Diff" field) being passed.

Afterwards, tryMatchHeader() is called for a second time after assigning $line = $this->nextLineThatLooksLikeDiffStart().
This time $line is null and a RuntimeException is thrown, as tryMatchHeader() calls preg_match() which does not accept passing null as the $subject string parameter in PHP 8.1.

Thus add a phutil_nonempty_string() check if the $subject parameter (in this case, $line) is a non-empty string.

Arcanist's tryMatchHeader() function is not called outside of the file in which it is defined.
Thus catch the exception in the second call to tryMatchHeader() and not in the code of the tryMatchHeader() function itself.

Closes T15338

Test Plan

After adding the additional check, /differential/diff/create/ showed the expected Diff Parse Exception instead.

Diff Detail

rARC Arcanist
Lint Not Applicable
Tests Not Applicable

Event Timeline

aklapper requested review of this revision.May 3 2023, 19:51

Ouch reading this description I feel it was a painful troubleshooting

Thank you so much also for this patch,

I tested this locally, examining $line with pht() and testing both in PHP 7.4 and PHP 8.1, following the test plan and some other things, without any unexpected nuclear implosion.

The function phutil_nonempty_string() will report any value that is not null and is not a string, and that is OK.

Green light


This revision is now accepted and ready to land.May 4 2023, 06:42
This revision was landed with ongoing or failed builds.May 4 2023, 08:56
This revision was automatically updated to reflect the committed changes.