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) if ((ret = grib_get_long_array_internal(grib_handle_of_accessor(a), self->array, ar, &size)) != GRIB_SUCCESS)
return ret; return ret;
if (self->element >= size) if (self->element < 0 || self->element >= size) {
return GRIB_INTERNAL_ERROR; 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]; *val = ar[self->element];
the_end:
grib_context_free(c, ar); grib_context_free(c, ar);
return ret; return ret;
} }

View File

@ -15,6 +15,8 @@ tempRef=temp.${label}.ref
tempText=temp.${label}.txt tempText=temp.${label}.txt
tempFilt=temp.${label}.filt tempFilt=temp.${label}.filt
input=$ECCODES_SAMPLES_PATH/reduced_gg_pl_48_grib2.tmpl
# Print the last three entries from the "pl" array # Print the last three entries from the "pl" array
cat > $tempFilt <<EOF cat > $tempFilt <<EOF
meta elemA element(pl, Nj - 3); meta elemA element(pl, Nj - 3);
@ -22,9 +24,22 @@ cat > $tempFilt <<EOF
meta elemC element(pl, Nj - 1); meta elemC element(pl, Nj - 1);
print "elemA=[elemA], elemB=[elemB], elemC=[elemC]"; print "elemA=[elemA], elemB=[elemB], elemC=[elemC]";
EOF EOF
input=$ECCODES_SAMPLES_PATH/reduced_gg_pl_48_grib2.tmpl
${tools_dir}/grib_filter $tempFilt $input > $tempText ${tools_dir}/grib_filter $tempFilt $input > $tempText
echo "elemA=36, elemB=25, elemC=20" > $tempRef echo "elemA=36, elemB=25, elemC=20" > $tempRef
diff $tempRef $tempText 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 rm -f $tempRef $tempText $tempFilt