Skip to content

Commit 5af615d

Browse files
committed
GDALDataset::BuildOverviews(): validate values of decimation factors
1 parent 8d5ca35 commit 5af615d

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

autotest/gcore/tiff_ovr.py

+18
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,24 @@ def test_tiff_ovr_3(mfloat32_tif, both_endian):
165165
src_ds = None
166166

167167

168+
###############################################################################
169+
#
170+
171+
172+
@gdaltest.enable_exceptions()
173+
def test_tiff_ovr_invalid_ovr_factor(tmp_path):
174+
tif_fname = str(tmp_path / "byte.tif")
175+
176+
shutil.copyfile("data/byte.tif", tif_fname)
177+
178+
ds = gdal.Open(tif_fname, gdal.GA_Update)
179+
with pytest.raises(
180+
Exception,
181+
match=r"panOverviewList\[1\] = 0 is invalid\. It must be a positive value",
182+
):
183+
ds.BuildOverviews(overviewlist=[2, 0])
184+
185+
168186
###############################################################################
169187
# Test generation
170188

gcore/gdaldataset.cpp

+14-1
Original file line numberDiff line numberDiff line change
@@ -2104,7 +2104,8 @@ CPLErr GDALSetGCPs2(GDALDatasetH hDS, int nGCPCount, const GDAL_GCP *pasGCPList,
21042104
* "BILINEAR", "CUBIC", "CUBICSPLINE", "GAUSS", "LANCZOS", "MODE", "NEAREST",
21052105
* or "NONE" controlling the downsampling method applied.
21062106
* @param nOverviews number of overviews to build, or 0 to clean overviews.
2107-
* @param panOverviewList the list of overview decimation factors to build, or
2107+
* @param panOverviewList the list of overview decimation factors (positive
2108+
* integers, normally larger or equal to 2) to build, or
21082109
* NULL if nOverviews == 0.
21092110
* @param nListBands number of bands to build overviews for in panBandList.
21102111
* Build for all bands if this is 0.
@@ -2151,6 +2152,18 @@ CPLErr GDALDataset::BuildOverviews(const char *pszResampling, int nOverviews,
21512152
if (pfnProgress == nullptr)
21522153
pfnProgress = GDALDummyProgress;
21532154

2155+
for (int i = 0; i < nOverviews; ++i)
2156+
{
2157+
if (panOverviewList[i] <= 0)
2158+
{
2159+
CPLError(CE_Failure, CPLE_IllegalArg,
2160+
"panOverviewList[%d] = %d is invalid. It must be a "
2161+
"positive value",
2162+
i, panOverviewList[i]);
2163+
return CE_Failure;
2164+
}
2165+
}
2166+
21542167
// At time of writing, all overview generation options are actually
21552168
// expected to be passed as configuration options.
21562169
std::vector<std::unique_ptr<CPLConfigOptionSetter>> apoConfigOptionSetter;

0 commit comments

Comments
 (0)