Check max value

This commit is contained in:
shahramn 2024-11-14 11:36:34 +00:00
parent 2f04dee967
commit 1053a594b9
1 changed files with 14 additions and 22 deletions

View File

@ -385,23 +385,19 @@ size_t grib_decode_size_t(const unsigned char* p, long* bitp, long nbits)
int grib_encode_unsigned_longb(unsigned char* p, unsigned long val, long* bitp, long nb) int grib_encode_unsigned_longb(unsigned char* p, unsigned long val, long* bitp, long nb)
{ {
long i = 0;
if (nb > max_nbits) { if (nb > max_nbits) {
fprintf(stderr, "Number of bits (%ld) exceeds maximum number of bits (%d)\n", nb, max_nbits); fprintf(stderr, "Number of bits (%ld) exceeds maximum number of bits (%d)\n", nb, max_nbits);
Assert(0); Assert(0);
return GRIB_INTERNAL_ERROR; return GRIB_INTERNAL_ERROR;
} }
#ifdef DEBUG
{ const unsigned long maxV = codes_power<double>(nb, 2) - 1;
unsigned long maxV = codes_power<double>(nb, 2);
if (val > maxV) { if (val > maxV) {
fprintf(stderr, "grib_encode_unsigned_longb: Value=%lu, but number of bits=%ld!\n", val, nb); fprintf(stderr, "ECCODES WARNING : %s: Trying to encode value of %lu but the maximum allowable value is %lu (number of bits=%ld)\n",
Assert(0); __func__, val, maxV, nb);
} }
}
#endif for (long i = nb - 1; i >= 0; i--) {
for (i = nb - 1; i >= 0; i--) {
if (test(val, i)) if (test(val, i))
grib_set_bit_on(p, bitp); grib_set_bit_on(p, bitp);
else else
@ -415,22 +411,18 @@ int grib_encode_unsigned_longb(unsigned char* p, unsigned long val, long* bitp,
*/ */
int grib_encode_size_tb(unsigned char* p, size_t val, long* bitp, long nb) int grib_encode_size_tb(unsigned char* p, size_t val, long* bitp, long nb)
{ {
long i = 0;
if (nb > max_nbits_size_t) { if (nb > max_nbits_size_t) {
fprintf(stderr, "Number of bits (%ld) exceeds maximum number of bits (%d)\n", nb, max_nbits_size_t); fprintf(stderr, "Number of bits (%ld) exceeds maximum number of bits (%d)\n", nb, max_nbits_size_t);
Assert(0); Assert(0);
} }
#ifdef DEBUG
{ const size_t maxV = codes_power<double>(nb, 2) - 1;
size_t maxV = codes_power<double>(nb, 2);
if (val > maxV) { if (val > maxV) {
fprintf(stderr, "grib_encode_size_tb: Value=%lu, but number of bits=%ld!\n", val, nb); fprintf(stderr, "ECCODES WARNING : %s: Trying to encode value of %zu but the maximum allowable value is %zu (number of bits=%ld)\n",
Assert(0); __func__, val, maxV, nb);
} }
}
#endif for (long i = nb - 1; i >= 0; i--) {
for (i = nb - 1; i >= 0; i--) {
if (test(val, i)) if (test(val, i))
grib_set_bit_on(p, bitp); grib_set_bit_on(p, bitp);
else else