From b0db6f6f0102da612f0de130905a15b12eecfb22 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maur=C3=ADcio=20Meneghini=20Fauth?= Date: Wed, 18 Nov 2026 21:52:21 -0300 Subject: [PATCH] Fix error handling for invalid SHP files MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: MaurĂ­cio Meneghini Fauth --- data/invalid-shp-multipoint.shp | Bin 0 -> 148 bytes data/invalid-shp-point.shp | Bin 0 -> 128 bytes src/ShapeRecord.php | 99 +++++++++++++++++++++++++++++--- tests/ShapeFileTest.php | 18 ++++++ 4 files changed, 108 insertions(+), 9 deletions(-) create mode 100644 data/invalid-shp-multipoint.shp create mode 100644 data/invalid-shp-point.shp diff --git a/data/invalid-shp-multipoint.shp b/data/invalid-shp-multipoint.shp new file mode 100644 index 0000000000000000000000000000000000000000..06f3762abfe9299f4257b532658cffe5f5740040 GIT binary patch literal 148 jcmZQzQ0HR64vbzfGca&qmm^h_5vYNQ7_%Ba{9*zCu4n_! literal 0 HcmV?d00001 diff --git a/data/invalid-shp-point.shp b/data/invalid-shp-point.shp new file mode 100644 index 0000000000000000000000000000000000000000..13090e921a815c89874cbef18df012617deaae33 GIT binary patch literal 128 kcmZQzQ0HR64vbzfGcYh>mm^h_k%8aI0b$k$d#C{p0HDAFQUCw| literal 0 HcmV?d00001 diff --git a/src/ShapeRecord.php b/src/ShapeRecord.php index 331b5cc..d26bf38 100644 --- a/src/ShapeRecord.php +++ b/src/ShapeRecord.php @@ -30,6 +30,7 @@ use function fwrite; use function in_array; use function is_array; +use function is_int; use function pack; use function sprintf; use function strlen; @@ -134,9 +135,19 @@ public function loadFromFile(ShapeFile &$shapeFile, &$shpFile, &$dbfFile): void break; } + if ($this->lastError !== '') { + return; + } + /* We need to skip rest of the record */ while ($this->read < $this->size) { + $before = $this->read; $this->loadData('V', 4); + if ($this->read === $before || $this->shapeFile->eofSHP()) { + $this->setError('Failed to parse record.'); + + return; + } } /* Check if we didn't read too much */ @@ -340,17 +351,38 @@ private function loadNullRecord(): void private function loadPointRecord(): void { - $this->shpData = $this->loadPoint(); + $point = $this->loadPoint(); + if ($point['x'] === false || $point['y'] === false) { + $this->setError('Failed to parse record.'); + + return; + } + + $this->shpData = $point; } private function loadPointMRecord(): void { - $this->shpData = $this->loadPointM(); + $point = $this->loadPointM(); + if ($point['x'] === false || $point['y'] === false || $point['m'] === false) { + $this->setError('Failed to parse record.'); + + return; + } + + $this->shpData = $point; } private function loadPointZRecord(): void { - $this->shpData = $this->loadPointZ(); + $point = $this->loadPointZ(); + if ($point['x'] === false || $point['y'] === false || $point['m'] === false || $point['z'] === false) { + $this->setError('Failed to parse record.'); + + return; + } + + $this->shpData = $point; } private function savePointRecord(): void @@ -381,10 +413,22 @@ private function loadMultiPointRecord(): void $this->shpData = []; $this->loadBBox(); - $this->shpData['numpoints'] = $this->loadData('V', 4); + $numPoints = $this->loadData('V', 4); + if (! is_int($numPoints) || $numPoints < 0) { + $this->setError('Failed to parse record.'); + return; + } + + $this->shpData['numpoints'] = $numPoints; for ($i = 0; $i < $this->shpData['numpoints']; ++$i) { - $this->shpData['points'][] = $this->loadPoint(); + $point = $this->loadPoint(); + if ($point['x'] === false || $point['y'] === false) { + $this->setError('Failed to parse record.'); + break; + } + + $this->shpData['points'][] = $point; } } @@ -399,7 +443,13 @@ private function loadMultiPointMZRecord(string $type): void $this->shpData[$type . 'max'] = $this->loadData('d', 8); for ($i = 0; $i < $this->shpData['numpoints']; ++$i) { - $this->shpData['points'][$i][$type] = $this->loadData('d', 8); + $point = $this->loadData('d', 8); + if ($point === false) { + $this->setError('Failed to parse record.'); + break; + } + + $this->shpData['points'][$i][$type] = $point; } } @@ -469,9 +519,21 @@ private function loadPolyLineRecord(): void $numparts = $this->shpData['numparts']; $numpoints = $this->shpData['numpoints']; + if (! is_int($numpoints) || $numpoints < 0 || ! is_int($numparts) || $numparts < 0) { + $this->setError('Failed to parse record.'); + + return; + } for ($i = 0; $i < $numparts; ++$i) { - $this->shpData['parts'][$i] = $this->loadData('V', 4); + $partData = $this->loadData('V', 4); + if ($partData === false) { + $this->setError('Failed to parse record.'); + + return; + } + + $this->shpData['parts'][$i] = $partData; } $part = 0; @@ -487,7 +549,14 @@ private function loadPolyLineRecord(): void $this->shpData['parts'][$part] = ['points' => []]; } - $this->shpData['parts'][$part]['points'][] = $this->loadPoint(); + $point = $this->loadPoint(); + if ($point['x'] === false || $point['y'] === false) { + $this->setError('Failed to parse record.'); + + return; + } + + $this->shpData['parts'][$part]['points'][] = $point; } } @@ -503,6 +572,11 @@ private function loadPolyLineMZRecord(string $type): void $numparts = $this->shpData['numparts']; $numpoints = $this->shpData['numpoints']; + if (! is_int($numpoints) || $numpoints < 0 || ! is_int($numparts) || $numparts < 0) { + $this->setError('Failed to parse record.'); + + return; + } $part = 0; for ($i = 0; $i < $numpoints; ++$i) { @@ -510,7 +584,14 @@ private function loadPolyLineMZRecord(string $type): void ++$part; } - $this->shpData['parts'][$part]['points'][$i][$type] = $this->loadData('d', 8); + $point = $this->loadData('d', 8); + if ($point === false) { + $this->setError('Failed to parse record.'); + + return; + } + + $this->shpData['parts'][$part]['points'][$i][$type] = $point; } } diff --git a/tests/ShapeFileTest.php b/tests/ShapeFileTest.php index 3c5d8e0..0de3c50 100644 --- a/tests/ShapeFileTest.php +++ b/tests/ShapeFileTest.php @@ -565,4 +565,22 @@ public function testAllowsNoDbf(): void $shp->setAllowNoDbf(true); self::assertTrue($shp->loadFromFile('data/no-dbf.*')); } + + /** @dataProvider providerForTestInvalidShpFile */ + #[DataProvider('providerForTestInvalidShpFile')] + public function testInvalidShpFile(string $file): void + { + $shp = new ShapeFile(-1); + $shp->setAllowNoDbf(true); + $shp->loadFromFile(__DIR__ . '/../' . $file); + self::assertSame('Failed to parse record.', $shp->lastError); + self::assertSame([], $shp->records); + } + + /** @return iterable */ + public static function providerForTestInvalidShpFile(): iterable + { + yield ['data/invalid-shp-point.*']; + yield ['data/invalid-shp-multipoint.*']; + } }