From ce1206d7fe8421c46a7e77c0944dc67eac0cf880 Mon Sep 17 00:00:00 2001 From: Henrik Nordstrom Date: Wed, 18 Jun 2008 20:49:32 +0200 Subject: [PATCH] Fix SNMP reporting of counters with a value >= 0xFF800000 The ASN.1 encoding of unsigned integers incorrectly compressed the value as if it was a signed integer, truncating leading 0xFF octest, causing 0xFF800000 to be send ax 0x800000, 0xFFFF8000 as 0x8000 etc.. --- snmplib/asn1.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/snmplib/asn1.c b/snmplib/asn1.c index bfdd3fda04..5ad7d7515b 100644 --- a/snmplib/asn1.c +++ b/snmplib/asn1.c @@ -324,10 +324,10 @@ asn_build_unsigned_int(u_char * data, int *datalength, return (NULL); } integer = *intp; - mask = (u_int) 0xFF << (8 * (sizeof(int) - 1)); - /* mask is 0xFF000000 on a big-endian machine */ - if ((u_char) ((integer & mask) >> (8 * (sizeof(int) - 1))) & 0x80) { - /* if MSB is set */ + mask = (u_int) 0x80 << (8 * (sizeof(int) - 1)); + /* mask is 0x80000000 on a big-endian machine */ + if ((integer & mask) != 0) { + /* add a null byte if MSB is set, to prevent sign extension */ add_null_byte = 1; intsize++; } @@ -336,11 +336,11 @@ asn_build_unsigned_int(u_char * data, int *datalength, * this 2's complement integer. * There should be no sequence of 9 consecutive 1's or 0's at the * most significant end of the integer. + * The 1's case is taken care of above by adding a null byte. */ mask = (u_int) 0x1FF << ((8 * (sizeof(int) - 1)) - 1); /* mask is 0xFF800000 on a big-endian machine */ - while ((((integer & mask) == 0) - || ((integer & mask) == mask)) && intsize > 1) { + while (((integer & mask) == 0) && intsize > 1) { intsize--; integer <<= 8; } -- 2.47.2