Skip to content
Merged
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
Binary file added data/invalid-shp-multipoint.shp
Binary file not shown.
Binary file added data/invalid-shp-point.shp
Binary file not shown.
99 changes: 90 additions & 9 deletions src/ShapeRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 */
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
}

Expand All @@ -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;
}
}

Expand Down Expand Up @@ -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;
Expand All @@ -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;
}
}

Expand All @@ -503,14 +572,26 @@ 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) {
if ($part + 1 < $numparts && $i === $this->shpData['parts'][$part + 1]) {
++$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;
}
}

Expand Down
18 changes: 18 additions & 0 deletions tests/ShapeFileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<int, array{string}> */
public static function providerForTestInvalidShpFile(): iterable
{
yield ['data/invalid-shp-point.*'];
yield ['data/invalid-shp-multipoint.*'];
}
}
Loading