Skip to content

Commit

Permalink
gdaladdo: validate values of decimation factors
Browse files Browse the repository at this point in the history
  • Loading branch information
rouault committed Dec 30, 2024
1 parent 5af615d commit fa8d6a4
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
16 changes: 16 additions & 0 deletions apps/gdaladdo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -692,7 +692,23 @@ MAIN_START(nArgc, papszArgv)
{
for (const auto &level : *levels)
{
if (CPLGetValueType(level.c_str()) != CPL_VALUE_INTEGER)
{
CPLError(
CE_Failure, CPLE_IllegalArg,
"Value '%s' is not a positive integer subsampling factor",
level.c_str());
std::exit(1);
}
anLevels.push_back(atoi(level.c_str()));
if (anLevels.back() <= 0)
{
CPLError(
CE_Failure, CPLE_IllegalArg,
"Value '%s' is not a positive integer subsampling factor",
level.c_str());
std::exit(1);
}
if (anLevels.back() == 1)
{
printf(
Expand Down
30 changes: 30 additions & 0 deletions autotest/utilities/test_gdaladdo.py
Original file line number Diff line number Diff line change
Expand Up @@ -455,3 +455,33 @@ def test_gdaladdo_partial_refresh_from_source_timestamp_gti(gdaladdo_path, tmp_p
ovr_data_refreshed[idx] = ovr_data_ori[idx]
assert ovr_data_refreshed == ovr_data_ori
ds = None


###############################################################################
#


def test_gdaladdo_illegal_factor(gdaladdo_path, tmp_path):

shutil.copyfile("../gcore/data/byte.tif", f"{tmp_path}/byte.tif")

_, err = gdaltest.runexternal_out_and_err(
f"{gdaladdo_path} -r average {tmp_path}/byte.tif invalid"
)
assert "Value 'invalid' is not a positive integer subsampling factor" in err
with gdal.Open(f"{tmp_path}/byte.tif") as ds:
assert ds.GetRasterBand(1).GetOverviewCount() == 0

_, err = gdaltest.runexternal_out_and_err(
f"{gdaladdo_path} -r average {tmp_path}/byte.tif 0"
)
assert "Value '0' is not a positive integer subsampling factor" in err
with gdal.Open(f"{tmp_path}/byte.tif") as ds:
assert ds.GetRasterBand(1).GetOverviewCount() == 0

_, err = gdaltest.runexternal_out_and_err(
f"{gdaladdo_path} -r average {tmp_path}/byte.tif -1"
)
assert "Value '-1' is not a positive integer subsampling factor" in err
with gdal.Open(f"{tmp_path}/byte.tif") as ds:
assert ds.GetRasterBand(1).GetOverviewCount() == 0

0 comments on commit fa8d6a4

Please sign in to comment.