PHP Fatal error: Uncaught TypeError: Unsupported operand types: string / string in AdvancedValueBinder.php #3861
Description
This is:
- [x] a bug report
- [ ] a feature request
- [ ] **not** a usage question (ask them on https://stackoverflow.com/questions/tagged/phpspreadsheet or https://gitter.im/PHPOffice/PhpSpreadsheet)
What is the expected behavior?
No fatal PHP error is issued.
What is the current behavior?
Fatal PHP error is issued:
PHP Fatal error: Uncaught TypeError: Unsupported operand types: string / string in phpoffice/phpspreadsheet/src/PhpSpreadsheet/Cell/AdvancedValueBinder.php:117
What are the steps to reproduce?
The error can be triggered if the advanced value binder is used and a value of "407 /" is set at a cell.
<?php
require __DIR__ . '/vendor/autoload.php';
// Create new Spreadsheet object
$spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
// Use the advanced value binder
\PhpOffice\PhpSpreadsheet\Cell\Cell::setValueBinder(new \PhpOffice\PhpSpreadsheet\Cell\AdvancedValueBinder());
// Create spreadsheet
$spreadsheet = new Spreadsheet();
$activeWorksheet = $spreadsheet->getActiveSheet();
$activeWorksheet->setCellValue('A1', '16 3/4');
// Error on the following line
$activeWorksheet->setCellValue('A2', '407 / ');
What features do you think are causing the issue
- Reader
- Writer
- Styles
- Data Validations
- Formula Calculations
- Charts
- AutoFilter
- Form Elements
The problem comes from the advanced value binder. It is issued at the method setImproperFraction() at line 117 where the matches of a regexp are used in a division. The method setImproperFraction() is called from bindValue() on line 51.
The regular expression on line 50 is faulty. It looks like this: /^([+-]?)(\d*) +(\d*)\s?\/\s*(\d*)$/
. Because the \d
are marked with a *
they can be omitted completely. So even a string like /
would match that regular expression. In my opinion the *
should be +
so that at least one digit is required. It should look like this: /^([+-]?)(\d+) +(\d+)\s?\/\s*(\d+)$/
.
Interestingly the regexp just two lines above (line 48) uses \d+
, so it might be just an oversight that it is not also fixed on line 50.
Does an issue affect all spreadsheet file formats? If not, which formats are affected?
Yes.
Which versions of PhpSpreadsheet and PHP are affected?
1.29.0