Skip to content

Commit

Permalink
GRIB2: fix crash and invalid metadata when processing index .idx file…
Browse files Browse the repository at this point in the history
… with sub-messages (fixes #6613)
  • Loading branch information
rouault authored and github-actions[bot] committed Nov 4, 2022
1 parent 3615980 commit 0b288a2
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
2 changes: 2 additions & 0 deletions autotest/gdrivers/data/grib/subgrids.grib2.idx
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
1.1:0:d=2020092600:UGRD:planetary boundary layer:anl:
1.2:0:d=2020092600:VGRD:planetary boundary layer:anl:
32 changes: 30 additions & 2 deletions autotest/gdrivers/grib.py
Original file line number Diff line number Diff line change
Expand Up @@ -1894,19 +1894,47 @@ def test_grib_grib2_scan_flag_not_64():
# Test reading message with subgrids


def test_grib_grib2_read_subgrids():
@pytest.mark.parametrize("use_idx", [True, False])
def test_grib_grib2_read_subgrids(use_idx):

# data/grib/subgrids.grib2 generated with:
# gdal_translate ../autotest/gcore/data/byte.tif band1.tif
# gdal_translate ../autotest/gcore/data/byte.tif band2.tif -scale 0 255 255 0
# gdalbuildvrt -separate tmp.vrt band1.tif band2.tif
# gdal_translate tmp.vrt ../autotest/gdrivers/data/grib/subgrids.grib2 -co "BAND_1_PDS_TEMPLATE_ASSEMBLED_VALUES=2 2 2 0 84 0 0 1 0 220 0 0 255 0 0" -co "BAND_2_PDS_TEMPLATE_ASSEMBLED_VALUES=2 3 2 0 84 0 0 1 0 220 0 0 255 0 0" -co "IDS=CENTER=7(US-NCEP) SUBCENTER=0 MASTER_TABLE=2 LOCAL_TABLE=1 SIGNF_REF_TIME=1(Start_of_Forecast) REF_TIME=2020-09-26T00:00:00Z PROD_STATUS=0(Operational) TYPE=1(Forecast)" -co WRITE_SUBGRIDS=YES
ds = gdal.Open("data/grib/subgrids.grib2")
ds = gdal.OpenEx(
"data/grib/subgrids.grib2",
open_options=(["USE_IDX=YES"] if use_idx else ["USE_IDX=NO"]),
)
assert ds.GetRasterBand(1).Checksum() == 4672
assert ds.GetRasterBand(2).Checksum() == 4563
expected_ids = "CENTER=7(US-NCEP) SUBCENTER=0 MASTER_TABLE=2 LOCAL_TABLE=0 SIGNF_REF_TIME=1(Start_of_Forecast) REF_TIME=2020-09-26T00:00:00Z PROD_STATUS=0(Operational) TYPE=1(Forecast)"
assert ds.GetRasterBand(1).GetMetadataItem("GRIB_IDS") == expected_ids
assert (
ds.GetRasterBand(1).GetMetadataItem("GRIB_COMMENT")
== "u-component of wind [m/s]"
)
if use_idx:
assert (
ds.GetRasterBand(1).GetDescription() == "UGRD:planetary boundary layer:anl"
)
assert (
ds.GetRasterBand(2).GetDescription() == "VGRD:planetary boundary layer:anl"
)
else:
assert (
ds.GetRasterBand(1).GetDescription()
== "0[-] RESERVED(220) (Reserved for local use)"
)
assert (
ds.GetRasterBand(2).GetDescription()
== "0[-] RESERVED(220) (Reserved for local use)"
)
assert ds.GetRasterBand(2).GetMetadataItem("GRIB_IDS") == expected_ids
assert (
ds.GetRasterBand(2).GetMetadataItem("GRIB_COMMENT")
== "v-component of wind [m/s]"
)
assert (
ds.GetRasterBand(1).GetMetadataItem("GRIB_PDS_TEMPLATE_ASSEMBLED_VALUES")
== "2 2 2 0 84 0 0 1 0 220 0 0 255 0 0"
Expand Down
7 changes: 7 additions & 0 deletions frmts/grib/gribdataset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ static CPLMutex *hGRIBMutex = nullptr;

static CPLString ConvertUnitInText( bool bMetricUnits, const char *pszTxt )
{
if( pszTxt == nullptr )
return CPLString();
if( !bMetricUnits )
return pszTxt;

Expand Down Expand Up @@ -1193,6 +1195,11 @@ class InventoryWrapperSidecar : public gdal::grib::InventoryWrapper
inv_[i].subgNum =
static_cast<unsigned short>(strtol(aosNum[1], &endptr, 10));
if (*endptr != 0) goto err_sidecar;
if( inv_[i].subgNum <= 0 )
goto err_sidecar;
// .idx file use a 1-based indexing, whereas DEGRIB uses a
// 0-based one
-- inv_[i].subgNum;
}

inv_[i].start = strtoll(aosTokens[1], &endptr, 10);
Expand Down

0 comments on commit 0b288a2

Please sign in to comment.