Skip to content

Commit f889151

Browse files
authored
Merge pull request OSGeo#11561 from rouault/ovr_invalid_factor
gdaladdo / GDALDataset::BuildOverviews(): validate values of decimation factors
2 parents 41ffedc + fa8d6a4 commit f889151

File tree

4 files changed

+78
-1
lines changed

4 files changed

+78
-1
lines changed

apps/gdaladdo.cpp

+16
Original file line numberDiff line numberDiff line change
@@ -692,7 +692,23 @@ MAIN_START(nArgc, papszArgv)
692692
{
693693
for (const auto &level : *levels)
694694
{
695+
if (CPLGetValueType(level.c_str()) != CPL_VALUE_INTEGER)
696+
{
697+
CPLError(
698+
CE_Failure, CPLE_IllegalArg,
699+
"Value '%s' is not a positive integer subsampling factor",
700+
level.c_str());
701+
std::exit(1);
702+
}
695703
anLevels.push_back(atoi(level.c_str()));
704+
if (anLevels.back() <= 0)
705+
{
706+
CPLError(
707+
CE_Failure, CPLE_IllegalArg,
708+
"Value '%s' is not a positive integer subsampling factor",
709+
level.c_str());
710+
std::exit(1);
711+
}
696712
if (anLevels.back() == 1)
697713
{
698714
printf(

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

autotest/utilities/test_gdaladdo.py

+30
Original file line numberDiff line numberDiff line change
@@ -455,3 +455,33 @@ def test_gdaladdo_partial_refresh_from_source_timestamp_gti(gdaladdo_path, tmp_p
455455
ovr_data_refreshed[idx] = ovr_data_ori[idx]
456456
assert ovr_data_refreshed == ovr_data_ori
457457
ds = None
458+
459+
460+
###############################################################################
461+
#
462+
463+
464+
def test_gdaladdo_illegal_factor(gdaladdo_path, tmp_path):
465+
466+
shutil.copyfile("../gcore/data/byte.tif", f"{tmp_path}/byte.tif")
467+
468+
_, err = gdaltest.runexternal_out_and_err(
469+
f"{gdaladdo_path} -r average {tmp_path}/byte.tif invalid"
470+
)
471+
assert "Value 'invalid' is not a positive integer subsampling factor" in err
472+
with gdal.Open(f"{tmp_path}/byte.tif") as ds:
473+
assert ds.GetRasterBand(1).GetOverviewCount() == 0
474+
475+
_, err = gdaltest.runexternal_out_and_err(
476+
f"{gdaladdo_path} -r average {tmp_path}/byte.tif 0"
477+
)
478+
assert "Value '0' is not a positive integer subsampling factor" in err
479+
with gdal.Open(f"{tmp_path}/byte.tif") as ds:
480+
assert ds.GetRasterBand(1).GetOverviewCount() == 0
481+
482+
_, err = gdaltest.runexternal_out_and_err(
483+
f"{gdaladdo_path} -r average {tmp_path}/byte.tif -1"
484+
)
485+
assert "Value '-1' is not a positive integer subsampling factor" in err
486+
with gdal.Open(f"{tmp_path}/byte.tif") as ds:
487+
assert ds.GetRasterBand(1).GetOverviewCount() == 0

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)