OSGeo / gdal

GDAL is an open source MIT licensed translator library for raster and vector geospatial data formats.
https://gdal.org
Other
4.77k stars 2.5k forks source link

Heap corruption in shapefile driver #10451

Closed fortinalex closed 3 weeks ago

fortinalex commented 1 month ago

What is the bug?

There is a heap corruption issue caused by an array overflow in the shapefile driver. In function Repack() of ogrshapelayer.cpp the following line (line 2864 in version 3.9.1) can write outside of the array.

panRecordsToDelete[nDeleteCount] = -1; This happens when the number of deleted items (nDeleteCount) is exactly equal to the size of the allocated array.

An easy fix is to change this line (line 2819 in version 3.9.1)

if (nDeleteCount == nDeleteCountAlloc) to if (nDeleteCount == nDeleteCountAlloc - 1)

Steps to reproduce the issue

Running in Debug, delete exactly 202 features from a shapefile and call the Repack function. The problem should happen also when deleting exactly 128 features but it has not been tested. In Release mode the heap corruption may not be apparent each time.

Versions and provenance

The problem was first detected in GDAL 3.7.0 and is still present in version 3.9.1. The line numbers in the bug report refer to the file contained in the 2024-06-26 gdal-3.9.1.tar.gz archive as downloaded on July 18th 2024.

Additional context

No response

rouault commented 1 month ago

@fortinalex Thanks for the report and fix suggestion. Fixed in a more robust & straightforward way using std::vector in PR #10452