#if HAVE_NETDB_H
#include <netdb.h>
#endif
+#if HAVE_ASSERT_H
+#include <assert.h>
+#endif
#include "asn1.h"
#include "snmp.h"
snmplib_debug(4, "snmp_msg_Decode:Error decoding SNMP Message Header (Version)!\n");
ASN_PARSE_ERROR(NULL);
}
- int terminatorPos = *CommLenP - 1;
+ int communityBufferLimit = *CommLenP;
+
bufp = asn_parse_string(bufp, PacketLenP, &type, Community, CommLenP);
if (bufp == NULL) {
snmplib_debug(4, "snmp_msg_Decode:Error decoding SNMP Message Header (Community)!\n");
ASN_PARSE_ERROR(NULL);
}
- if (*CommLenP < terminatorPos) {
- terminatorPos = *CommLenP;
+
+ if (*CommLenP == communityBufferLimit) {
+ snmplib_debug(4, "snmp_msg_Decode:Cannot zero-terminate a %d byte-long Community value\n", *CommLenP);
+ ASN_PARSE_ERROR(NULL);
+ }
+ assert(*CommLenP >= 0);
+ assert(*CommLenP < communityBufferLimit);
+ Community[*CommLenP] = '\0';
+
+ if (memchr(Community, '\0', (size_t)*CommLenP)) {
+ snmplib_debug(4, "snmp_msg_Decode:Community contained an unsupported ASCII nul character\n");
+ ASN_PARSE_ERROR(NULL);
}
- Community[terminatorPos] = '\0';
if ((*Version != SNMP_VERSION_1) &&
(*Version != SNMP_VERSION_2)) {
case ASN_OCTET_STR:
case SMI_IPADDRESS:
case SMI_OPAQUE:
- Var->val_len = *&ThisVarLen; /* String is this at most */
- Var->val.string = (u_char *) xmalloc((unsigned) Var->val_len);
+ Var->val_len = ThisVarLen >= 0 ? ThisVarLen : 0; // input contains at most this many bytes
+ Var->val.string = (u_char *) xmalloc((unsigned) Var->val_len + 1);
if (Var->val.string == NULL) {
snmp_set_api_error(SNMPERR_OS_ERR);
PARSE_ERROR;
}
- int terminatorPos = Var->val_len - 1;
bufp = asn_parse_string(DataPtr, &ThisVarLen,
&Var->type, Var->val.string,
&Var->val_len);
- if (Var->val_len < terminatorPos) {
- terminatorPos = Var->val_len;
- }
- Var->val.string[terminatorPos] = '\0';
+ Var->val.string[Var->val_len] = '\0'; // for cases where the parsed value is treated as a c-string
#if DEBUG_VARS_DECODE
printf("VARS: Decoded string '%s' (length %d) (%d bytes left)\n",
(Var->val.string), Var->val_len, ThisVarLen);