ECC-1275: grib_set: Setting unsigned values to -1 sets the maximum value for that key

This commit is contained in:
Shahram Najm 2021-08-27 19:38:46 +01:00
parent 97710440d4
commit 5bee4847be
2 changed files with 39 additions and 11 deletions

View File

@ -235,14 +235,14 @@ int pack_long_unsigned_helper(grib_accessor* a, const long* val, size_t* len, in
/* Check if value fits into number of bits */ /* Check if value fits into number of bits */
if (check) { if (check) {
const long nbits = self->nbytes * 8;
/* See GRIB-23 and GRIB-262 */
if (!value_is_missing(v)) {
if (v < 0) { if (v < 0) {
grib_context_log(a->context, GRIB_LOG_ERROR, grib_context_log(a->context, GRIB_LOG_ERROR,
"Key \"%s\": Trying to encode a negative value of %ld for key of type unsigned\n", a->name, v); "Key \"%s\": Trying to encode a negative value of %ld for key of type unsigned\n", a->name, v);
return GRIB_ENCODING_ERROR; return GRIB_ENCODING_ERROR;
} }
/* See GRIB-23 and GRIB-262 */
if (!value_is_missing(v)) {
const long nbits = self->nbytes * 8;
if (nbits < 33) { if (nbits < 33) {
unsigned long maxval = (1UL << nbits) - 1; unsigned long maxval = (1UL << nbits) - 1;
if (maxval > 0 && v > maxval) { /* See ECC-1002 */ if (maxval > 0 && v > maxval) { /* See ECC-1002 */

View File

@ -13,7 +13,8 @@
REDIRECT=/dev/null REDIRECT=/dev/null
infile=${data_dir}/regular_gaussian_surface.grib1 infile=${data_dir}/regular_gaussian_surface.grib1
outfile=${data_dir}/set.grib1 outfile=${data_dir}/temp.grib_set.grib
temp=temp.grib_set.out
rm -f $outfile rm -f $outfile
@ -52,13 +53,37 @@ levtype=`${tools_dir}/grib_get -p levtype:l $outfile`
centre=`${tools_dir}/grib_get -p centre:l $outfile` centre=`${tools_dir}/grib_get -p centre:l $outfile`
[ $centre -eq 80 ] [ $centre -eq 80 ]
# Set without -s. Expected to fail
# ----------------------------------------------------
set +e set +e
# This is expected to fail
${tools_dir}/grib_set -p levtype $infile $outfile 2> $REDIRECT > $REDIRECT ${tools_dir}/grib_set -p levtype $infile $outfile 2> $REDIRECT > $REDIRECT
[ $? -ne 0 ] status=$?
set -e set -e
[ $status -ne 0 ]
# GRIB-941: encoding of grib2 angles # Out-of-bounds value. Expected to fail
# ----------------------------------------------------
input=${data_dir}/reduced_gaussian_sub_area.grib2
set +e
${tools_dir}/grib_set -s perturbationNumber=1000 $input $outfile 2>$temp
status=$?
set -e
[ $status -ne 0 ]
grep -q "Trying to encode value of 1000 but the maximum allowable value is 255 (number of bits=8)" $temp
# Negative value for an unsigned key. Expected to fail
# ----------------------------------------------------
input=${data_dir}/reduced_gaussian_sub_area.grib2
set +e
${tools_dir}/grib_set -s perturbationNumber=-1 $input $outfile 2>$temp
status=$?
set -e
[ $status -ne 0 ]
grep -q "Trying to encode a negative value of -1 for key of type unsigned" $temp
# GRIB-941: encoding of GRIB2 angles
# -----------------------------------
angleInDegrees=130.9989 angleInDegrees=130.9989
angleInMicroDegrees=130998900 angleInMicroDegrees=130998900
files="GRIB2.tmpl regular_gg_pl_grib2.tmpl reduced_gg_pl_320_grib2.tmpl polar_stereographic_pl_grib2.tmpl" files="GRIB2.tmpl regular_gg_pl_grib2.tmpl reduced_gg_pl_320_grib2.tmpl polar_stereographic_pl_grib2.tmpl"
@ -69,14 +94,17 @@ for f in $files; do
done done
# GRIB-943: centre code table # GRIB-943: centre code table
# ----------------------------
${tools_dir}/grib_set -s centre=289 $ECCODES_SAMPLES_PATH/GRIB2.tmpl $outfile ${tools_dir}/grib_set -s centre=289 $ECCODES_SAMPLES_PATH/GRIB2.tmpl $outfile
${tools_dir}/grib_dump -O $outfile | grep -q 'centre = 289.*Zambia' ${tools_dir}/grib_dump -O $outfile | grep -q 'centre = 289.*Zambia'
# ECC-539: avoid output being the same as input # ECC-539: avoid output being the same as input
# -----------------------------------------------
set +e set +e
${tools_dir}/grib_set -s centre=0 $outfile $outfile ${tools_dir}/grib_set -s centre=0 $outfile $outfile
status=$? status=$?
set -e set -e
[ $status -ne 0 ] [ $status -ne 0 ]
rm -f $outfile
rm -f $outfile $temp