Tolerate empty OTL data when slicing a line of text (mirrors mpdf/mpdf#2197) - #15
Merged
Merged
Conversation
applyOTL() resets $this->OTLdata to [] and returns early for a blank string, but MultiCell() takes that empty array as "there is OTL data here" and hands it to sliceOTLdata(), which reads 'group' and 'GPOSinfo' unconditionally. On PHP 8 that is two "Undefined array key" warnings plus a null-to-substr() deprecation, for every MultiCell() call with an empty or whitespace-only string. Guard the two reads the way 'char_data' is already guarded, rather than changing what applyOTL() leaves behind — ten call sites read $this->otl->OTLdata and most test it for truthiness. Mirrors mpdf#2197 (mpdf#2158). Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This mirrors mpdf#2197, which fixes mpdf#2158, so the fork carries the fix while the upstream PR sits open.
Otl::applyOTL()opens by resetting$this->OTLdatato[]and returning early when the string is blank.MultiCell()then does$OTLdata = $this->otl->OTLdata, and every laterisset($OTLdata)is satisfied by that empty array — so the empty array travels on intosliceOTLdata(), which reads$OTLdata['group']and$OTLdata['GPOSinfo']without checking. On PHP 8 that is two Undefined array key warnings and a Passing null to substr() deprecation for everyMultiCell()call with an empty or whitespace-only string, on any non-core font.sliceOTLdata()already guardschar_data. This guards the other two the same way.Try it
On
gravitypdf, withdisplay_errorson:On this branch, nothing.
Test plan
composer test— 1052 tests, 2538 assertions, green (gravitypdfis at 1048).composer cs— clean.gravitypdf, none new.gravitypdfand pass here. PHPUnit already turns PHP warnings into test errors, so the assertions are the plain ones the repo uses elsewhere.More info
Where it differs from upstream
$OTLdataparameter, then reads it backtests/Issues/Issue2158Test.phpinstalls aset_error_handlerto catch the warningassertStringStartsWith('%PDF-', …), as in the ~100 othertests/IssuescasessliceOTLdata()call site (Mpdf.php:6109, the explicit-line-break branch, rather thanMpdf.php:6350, the last-chunk branch)tests/Mpdf/OtlTest.php, pinningsliceOTLdata()directly: one populated slice, and one empty-input caseCHANGELOG.mdWhy the guard, not the root cause
The tidier-looking fix is to have
applyOTL()leave a complete-but-empty structure (['group' => '', 'GPOSinfo' => [], 'char_data' => []]) instead of[]. Ten call sites read$this->otl->OTLdata—Mpdf::MultiCell(),Mpdf::Cell(),DirectWrite,Formtwice,Svg— and several test it for truthiness. Making it non-empty flips those branches for every blank string in the document. Not worth it for a warning.sliceOTLdata()is a public method that already treatschar_dataas optional; treating the whole structure as optional is consistent, and it is the only place that reads these keys unguarded.