Detect invalid elements. Also fix memory leak

This commit is contained in:
Shahram Najm 2022-05-08 23:16:52 +01:00
parent 0945d27154
commit d7e96d4d1d
2 changed files with 24 additions and 3 deletions

View File

@ -172,10 +172,16 @@ static int unpack_long(grib_accessor* a, long* val, size_t* len)
if ((ret = grib_get_long_array_internal(grib_handle_of_accessor(a), self->array, ar, &size)) != GRIB_SUCCESS)
return ret;
if (self->element >= size)
return GRIB_INTERNAL_ERROR;
if (self->element < 0 || self->element >= size) {
grib_context_log(c, GRIB_LOG_ERROR, "Invalid element %ld for array '%s'. Value must be between 0 and %lu",
self->element, self->array, size - 1);
ret = GRIB_INVALID_ARGUMENT;
goto the_end;
}
*val = ar[self->element];
the_end:
grib_context_free(c, ar);
return ret;
}

View File

@ -15,6 +15,8 @@ tempRef=temp.${label}.ref
tempText=temp.${label}.txt
tempFilt=temp.${label}.filt
input=$ECCODES_SAMPLES_PATH/reduced_gg_pl_48_grib2.tmpl
# Print the last three entries from the "pl" array
cat > $tempFilt <<EOF
meta elemA element(pl, Nj - 3);
@ -22,9 +24,22 @@ cat > $tempFilt <<EOF
meta elemC element(pl, Nj - 1);
print "elemA=[elemA], elemB=[elemB], elemC=[elemC]";
EOF
input=$ECCODES_SAMPLES_PATH/reduced_gg_pl_48_grib2.tmpl
${tools_dir}/grib_filter $tempFilt $input > $tempText
echo "elemA=36, elemB=25, elemC=20" > $tempRef
diff $tempRef $tempText
# Invalid element
cat > $tempFilt <<EOF
meta badElem element(pl, -1);
print "[badElem]";
EOF
set +e
${tools_dir}/grib_filter $tempFilt $input > $tempText 2>&1
status=$?
set -e
[ $status -ne 0 ]
grep -q "Invalid element.*Value must be between 0 and 95" $tempText
rm -f $tempRef $tempText $tempFilt