Skip to content

Allow Xlsx Reader to Specify ParseHuge Release222 #4517

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

Open
wants to merge 2 commits into
base: release222
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ and this project adheres to [Semantic Versioning](https://semver.org).

- TEXT and TIMEVALUE functions. [Issue #4249](https://github.com/PHPOffice/PhpSpreadsheet/issues/4249) [PR #4354](https://github.com/PHPOffice/PhpSpreadsheet/pull/4354)
- Removing Columns/Rows Containing Merged Cells. Backport of [PR #4465](https://github.com/PHPOffice/PhpSpreadsheet/pull/4465)
- Allow Xlsx Reader to Specify ParseHuge. [Issue #4260](https://github.com/PHPOffice/PhpSpreadsheet/issues/4260) [PR #4517](https://github.com/PHPOffice/PhpSpreadsheet/pull/4517)

# 2025-02-07 - 2.3.8

Expand Down
29 changes: 23 additions & 6 deletions src/PhpSpreadsheet/Reader/Xlsx.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,19 @@ class Xlsx extends BaseReader

private array $sharedFormulae = [];

private bool $parseHuge = false;

/**
* Allow use of LIBXML_PARSEHUGE.
* This option can lead to memory leaks and failures,
* and is not recommended. But some very large spreadsheets
* seem to require it.
*/
public function setParseHuge(bool $parseHuge): void
{
$this->parseHuge = $parseHuge;
}

/**
* Create a new Xlsx Reader instance.
*/
Expand Down Expand Up @@ -119,8 +132,8 @@ private function loadZip(string $filename, string $ns = '', bool $replaceUnclose
}
$rels = @simplexml_load_string(
$this->getSecurityScannerOrThrow()->scan($contents),
'SimpleXMLElement',
0,
SimpleXMLElement::class,
$this->parseHuge ? LIBXML_PARSEHUGE : 0,
$ns
);

Expand All @@ -134,8 +147,8 @@ private function loadZipNonamespace(string $filename, string $ns): SimpleXMLElem
$contents = $this->getFromZipArchive($this->zip, $filename);
$rels = simplexml_load_string(
$this->getSecurityScannerOrThrow()->scan($contents),
'SimpleXMLElement',
0,
SimpleXMLElement::class,
$this->parseHuge ? LIBXML_PARSEHUGE : 0,
($ns === '' ? $ns : '')
);

Expand Down Expand Up @@ -249,7 +262,9 @@ public function listWorksheetInfo(string $filename): array
$this->zip,
$fileWorksheetPath
)
)
),
null,
$this->parseHuge ? LIBXML_PARSEHUGE : 0
);
$xml->setParserProperty(2, true);

Expand Down Expand Up @@ -1977,7 +1992,9 @@ private function readRibbon(Spreadsheet $excel, string $customUITarget, ZipArchi
// exists and not empty if the ribbon have some pictures (other than internal MSO)
$UIRels = simplexml_load_string(
$this->getSecurityScannerOrThrow()
->scan($dataRels)
->scan($dataRels),
SimpleXMLElement::class,
$this->parseHuge ? LIBXML_PARSEHUGE : 0
);
if (false !== $UIRels) {
// we need to save id and target to avoid parsing customUI.xml and "guess" if it's a pseudo callback who load the image
Expand Down