Skip to content

Commit fa8d6a4

Browse files
committed
gdaladdo: validate values of decimation factors
1 parent 5af615d commit fa8d6a4

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
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/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

0 commit comments

Comments
 (0)