From f60f37c36290bdfd05f3e810b0cec4187b15632a Mon Sep 17 00:00:00 2001 From: Mats Sibelius Date: Mon, 8 Feb 2021 20:26:11 +0200 Subject: [PATCH] Fix case where mergeComplexNumberFormatMasks would get stuck in endless-loop (#1793) * Fix case where mergeComplexNumberFormatMasks would get stuck in endless-loop if $numbers had many decimals --- src/PhpSpreadsheet/Style/NumberFormat.php | 8 +++++--- tests/data/Style/NumberFormat.php | 20 ++++++++++++++++++++ 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/src/PhpSpreadsheet/Style/NumberFormat.php b/src/PhpSpreadsheet/Style/NumberFormat.php index 9a2549bc..a96d2ac3 100644 --- a/src/PhpSpreadsheet/Style/NumberFormat.php +++ b/src/PhpSpreadsheet/Style/NumberFormat.php @@ -563,9 +563,11 @@ class NumberFormat extends Supervisor do { $tempMask = array_pop($masks); - $postDecimalMasks[] = $tempMask; - $decimalCount -= strlen($tempMask); - } while ($decimalCount > 0); + if ($tempMask !== null) { + $postDecimalMasks[] = $tempMask; + $decimalCount -= strlen($tempMask); + } + } while ($tempMask !== null && $decimalCount > 0); return [ implode('.', $masks), diff --git a/tests/data/Style/NumberFormat.php b/tests/data/Style/NumberFormat.php index 12aec1d3..81bb90ae 100644 --- a/tests/data/Style/NumberFormat.php +++ b/tests/data/Style/NumberFormat.php @@ -362,4 +362,24 @@ return [ 25, '[Green][<>25]"<>25 green";[Red]"else red"', ], + [ + 'pfx. 25.00', + 25, + '"pfx." 0.00;"pfx." -0.00;"pfx." 0.00;', + ], + [ + 'pfx. 25.20', + 25.2, + '"pfx." 0.00;"pfx." -0.00;"pfx." 0.00;', + ], + [ + 'pfx. -25.20', + -25.2, + '"pfx." 0.00;"pfx." -0.00;"pfx." 0.00;', + ], + [ + 'pfx. 25.26', + 25.255555555555555, + '"pfx." 0.00;"pfx." -0.00;"pfx." 0.00;', + ], ];