From 5999d8b87cf370d15a944e375d56cf0d8ef7a95f Mon Sep 17 00:00:00 2001 From: Alessandro Pasotti Date: Wed, 11 Sep 2024 20:19:15 +0200 Subject: [PATCH] [gdal_rasterize] Fix crash with no options after argparser (#10770) (master only) Fixes #10767 Followup #10741 --- apps/gdal_rasterize_lib.cpp | 5 +++ autotest/utilities/test_gdal_rasterize_lib.py | 34 +++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/apps/gdal_rasterize_lib.cpp b/apps/gdal_rasterize_lib.cpp index a4e1aae5b9f2..6d5de1415250 100644 --- a/apps/gdal_rasterize_lib.cpp +++ b/apps/gdal_rasterize_lib.cpp @@ -1446,6 +1446,11 @@ GDALRasterizeOptionsNew(char **papszArgv, if (!psOptions->osFormat.empty()) psOptionsForBinary->osFormat = psOptions->osFormat; } + else if (psOptions->adfBurnValues.empty() && + psOptions->osBurnAttribute.empty() && !psOptions->b3D) + { + psOptions->adfBurnValues.push_back(255); + } } catch (const std::exception &e) { diff --git a/autotest/utilities/test_gdal_rasterize_lib.py b/autotest/utilities/test_gdal_rasterize_lib.py index 67b2ba8d6d42..fa8103c7adf2 100755 --- a/autotest/utilities/test_gdal_rasterize_lib.py +++ b/autotest/utilities/test_gdal_rasterize_lib.py @@ -792,3 +792,37 @@ def test_gdal_rasterize_lib_dict_arguments(): ind = opt.index("-co") assert opt[ind : ind + 4] == ["-co", "COMPRESS=DEFLATE", "-co", "LEVEL=4"] + + +############################################################################### +# Test doesn't crash without options + + +def test_gdal_rasterize_no_options(tmp_vsimem): + """Test doesn't crash without options""" + + gdal.FileFromMemBuffer( + tmp_vsimem / "test.json", + r"""{ + "type": "FeatureCollection", + "name": "test", + "crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:EPSG::4326" } }, + "features": [ + { "type": "Feature", "properties": { "id": 1 }, "geometry": { "type": "Polygon", "coordinates": [ [ [ 0, 0 ], [ 0, 1 ], [ 1, 1 ], [ 1, 0 ], [ 0, 0 ] ] ] } } + ] + }""", + ) + + # Open the dataset + ds = gdal.OpenEx(tmp_vsimem / "test.json", gdal.OF_VECTOR) + assert ds + + # Create a raster to rasterize into. + target_ds = gdal.GetDriverByName("GTiff").Create( + tmp_vsimem / "out.tif", 10, 10, 1, gdal.GDT_Byte + ) + + assert target_ds + + # Call rasterize + ds = gdal.Rasterize(target_ds, ds)