svn commit: r311591 - stable/11/usr.sbin/bsnmpd/tools/libbsnmptools
Ngie Cooper
ngie at FreeBSD.org
Sat Jan 7 08:28:53 UTC 2017
Author: ngie
Date: Sat Jan 7 08:28:51 2017
New Revision: 311591
URL: https://svnweb.freebsd.org/changeset/base/311591
Log:
MFC r310954,r310987,r311222:
r310954:
Set value->v.octetstring.len to a correct value on malloc success/failure
The previous code always set value->v.octetstring.len to len, regardless
of the result from the malloc call. This misleads the caller on malloc
failure. Set .len to len on success and 0 on failure.
CID: 1007590
r310987:
snmp_output_err_resp, snmp_output_resp: allocate `object` using calloc, not
on the stack
Some of the callers try to determine whether or not `object` is valid by
testing the value for NULL, which will never be true if it's a stack value,
so in order to be clear and correct down the call stack, use a heap
allocated object.
This also addresses a Coverity issue by initializing all of `object` via
calloc
CID: 1006392
r311222:
Fix logical inversion when checking result from calloc
in snmp_output_err_resp(..)
CID: 1368195
Modified:
stable/11/usr.sbin/bsnmpd/tools/libbsnmptools/bsnmptc.c
stable/11/usr.sbin/bsnmpd/tools/libbsnmptools/bsnmptools.c
Directory Properties:
stable/11/ (props changed)
Modified: stable/11/usr.sbin/bsnmpd/tools/libbsnmptools/bsnmptc.c
==============================================================================
--- stable/11/usr.sbin/bsnmpd/tools/libbsnmptools/bsnmptc.c Sat Jan 7 08:28:41 2017 (r311590)
+++ stable/11/usr.sbin/bsnmpd/tools/libbsnmptools/bsnmptc.c Sat Jan 7 08:28:51 2017 (r311591)
@@ -266,13 +266,13 @@ parse_octetstring(struct snmp_value *val
return (-1);
}
- value->v.octetstring.len = len;
-
- if((value->v.octetstring.octets = malloc(len)) == NULL) {
+ if ((value->v.octetstring.octets = malloc(len)) == NULL) {
+ value->v.octetstring.len = 0;
syslog(LOG_ERR,"malloc failed: %s", strerror(errno));
return (-1);
}
+ value->v.octetstring.len = len;
memcpy(value->v.octetstring.octets, val, len);
value->syntax = SNMP_SYNTAX_OCTETSTRING;
Modified: stable/11/usr.sbin/bsnmpd/tools/libbsnmptools/bsnmptools.c
==============================================================================
--- stable/11/usr.sbin/bsnmpd/tools/libbsnmptools/bsnmptools.c Sat Jan 7 08:28:41 2017 (r311590)
+++ stable/11/usr.sbin/bsnmpd/tools/libbsnmptools/bsnmptools.c Sat Jan 7 08:28:51 2017 (r311591)
@@ -2008,20 +2008,25 @@ snmp_output_object(struct snmp_toolinfo
void
snmp_output_err_resp(struct snmp_toolinfo *snmptoolctx, struct snmp_pdu *pdu)
{
+ struct snmp_object *object;
char buf[ASN_OIDSTRLEN];
- struct snmp_object object;
if (pdu == NULL || (pdu->error_index > (int32_t) pdu->nbindings)) {
- fprintf(stdout,"Invalid error index in PDU\n");
+ fprintf(stdout, "Invalid error index in PDU\n");
+ return;
+ }
+
+ if ((object = calloc(1, sizeof(struct snmp_object))) == NULL) {
+ fprintf(stdout, "calloc: %s", strerror(errno));
return;
}
fprintf(stdout, "Agent %s:%s returned error \n", snmp_client.chost,
snmp_client.cport);
- if (!ISSET_NUMERIC(snmptoolctx) && (snmp_fill_object(snmptoolctx, &object,
+ if (!ISSET_NUMERIC(snmptoolctx) && (snmp_fill_object(snmptoolctx, object,
&(pdu->bindings[pdu->error_index - 1])) > 0))
- snmp_output_object(snmptoolctx, &object);
+ snmp_output_object(snmptoolctx, object);
else {
asn_oid2str_r(&(pdu->bindings[pdu->error_index - 1].var), buf);
fprintf(stdout,"%s", buf);
@@ -2033,17 +2038,23 @@ snmp_output_err_resp(struct snmp_toolinf
fprintf(stdout, "%s\n", error_strings[pdu->error_status].str);
else
fprintf(stdout,"%s\n", error_strings[SNMP_ERR_UNKNOWN].str);
+
+ free(object);
+ object = NULL;
}
int32_t
snmp_output_resp(struct snmp_toolinfo *snmptoolctx, struct snmp_pdu *pdu,
struct asn_oid *root)
{
- struct snmp_object object;
+ struct snmp_object *object;
char p[ASN_OIDSTRLEN];
int32_t error;
uint32_t i;
+ if ((object = calloc(1, sizeof(struct snmp_object))) == NULL)
+ return (-1);
+
i = error = 0;
while (i < pdu->nbindings) {
if (root != NULL && !(asn_is_suboid(root,
@@ -2052,18 +2063,22 @@ snmp_output_resp(struct snmp_toolinfo *s
if (GET_OUTPUT(snmptoolctx) != OUTPUT_QUIET) {
if (!ISSET_NUMERIC(snmptoolctx) &&
- (snmp_fill_object(snmptoolctx, &object,
+ (snmp_fill_object(snmptoolctx, object,
&(pdu->bindings[i])) > 0))
- snmp_output_object(snmptoolctx, &object);
+ snmp_output_object(snmptoolctx, object);
else {
asn_oid2str_r(&(pdu->bindings[i].var), p);
fprintf(stdout, "%s", p);
}
}
- error |= snmp_output_numval(snmptoolctx, &(pdu->bindings[i]), object.info);
+ error |= snmp_output_numval(snmptoolctx, &(pdu->bindings[i]),
+ object->info);
i++;
}
+ free(object);
+ object = NULL;
+
if (error)
return (-1);
More information about the svn-src-stable
mailing list