Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PHP Fatal error: Uncaught TypeError: Unsupported operand types: string / string in AdvancedValueBinder.php #3861

Closed
1 of 8 tasks
patrickvuarnoz opened this issue Jan 8, 2024 · 0 comments · Fixed by #3862

Comments

@patrickvuarnoz
Copy link

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

oleibman added a commit to oleibman/PhpSpreadsheet that referenced this issue Jan 10, 2024
Fix PHPOffice#3861. Strings ending in `/` were inappropriately identifed as fractions. Fix regexp accordingly.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging a pull request may close this issue.

1 participant