Call snmp_pdu_free on req/resp with a consistent, correct pattern

- snmp_pdu_free should be called before snmp_pdu_create is called
  again
- snmp_pdu_free should be called on the resp to snmp_dialog when
  successful

Tested with the following bsnmp commands:

  % export SNMPUSER=bsnmp SNMPPASSWD=bsnmptest
  % SNMP_ARGS="-A proto=sha -C context='' -K -P proto=des -v 3 -r 0"
  % bsnmpset $SNMP_ARGS sysLocation="MyAgent"
  % bsnmpget $SNMP_ARGS sysLocation
  % bsnmpwalk $SNMP_ARGS

MFC after:	12 days
X-MFC with:	r310729, r310892, r310894
This commit is contained in:
Enji Cooper 2017-01-01 00:23:43 +00:00
parent 1cc49661ec
commit 570afb920c
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=310989

View file

@ -400,13 +400,16 @@ snmptool_get(struct snmp_toolinfo *snmptoolctx)
if (snmp_parse_resp(&resp, &req) >= 0) {
snmp_output_resp(snmptoolctx, &resp, NULL);
snmp_pdu_free(&resp);
break;
}
snmp_output_err_resp(snmptoolctx, &resp);
if (GET_PDUTYPE(snmptoolctx) == SNMP_PDU_GETBULK ||
!ISSET_RETRY(snmptoolctx))
!ISSET_RETRY(snmptoolctx)) {
snmp_pdu_free(&resp);
break;
}
/*
* Loop through the object list and set object->error to the
@ -414,8 +417,10 @@ snmptool_get(struct snmp_toolinfo *snmptoolctx)
*/
if (snmp_object_seterror(snmptoolctx,
&(resp.bindings[resp.error_index - 1]),
resp.error_status) <= 0)
resp.error_status) <= 0) {
snmp_pdu_free(&resp);
break;
}
fprintf(stderr, "Retrying...\n");
snmp_pdu_free(&resp);
@ -423,7 +428,6 @@ snmptool_get(struct snmp_toolinfo *snmptoolctx)
}
snmp_pdu_free(&req);
snmp_pdu_free(&resp);
return (0);
}
@ -500,8 +504,10 @@ snmptool_walk(struct snmp_toolinfo *snmptoolctx)
outputs += rc;
if ((u_int)rc < resp.nbindings)
if ((u_int)rc < resp.nbindings) {
snmp_pdu_free(&resp);
break;
}
snmpwalk_nextpdu_create(op,
&(resp.bindings[resp.nbindings - 1].var), &req);
@ -515,10 +521,11 @@ snmptool_walk(struct snmp_toolinfo *snmptoolctx)
if (outputs == 0) {
snmpwalk_nextpdu_create(SNMP_PDU_GET, &root, &req);
if (snmp_dialog(&req, &resp) == SNMP_CODE_OK) {
if (snmp_parse_resp(&resp,&req) < 0)
if (snmp_parse_resp(&resp, &req) < 0)
snmp_output_err_resp(snmptoolctx, &resp);
else
snmp_output_resp(snmptoolctx, &(resp), NULL);
snmp_output_resp(snmptoolctx, &resp,
NULL);
snmp_pdu_free(&resp);
} else
warn("Snmp dialog");
@ -534,7 +541,6 @@ snmptool_walk(struct snmp_toolinfo *snmptoolctx)
}
snmp_pdu_free(&req);
snmp_pdu_free(&resp);
if (rc == 0)
return (0);
@ -1094,25 +1100,29 @@ snmptool_set(struct snmp_toolinfo *snmptoolctx)
if (snmp_pdu_check(&req, &resp) > 0) {
if (GET_OUTPUT(snmptoolctx) != OUTPUT_QUIET)
snmp_output_resp(snmptoolctx, &resp, NULL);
snmp_pdu_free(&resp);
break;
}
snmp_output_err_resp(snmptoolctx, &resp);
if (!ISSET_RETRY(snmptoolctx))
if (!ISSET_RETRY(snmptoolctx)) {
snmp_pdu_free(&resp);
break;
}
if (snmp_object_seterror(snmptoolctx,
&(resp.bindings[resp.error_index - 1]),
resp.error_status) <= 0)
resp.error_status) <= 0) {
snmp_pdu_free(&resp);
break;
}
fprintf(stderr, "Retrying...\n");
snmp_pdu_free(&req);
snmp_pdu_free(&resp);
snmp_pdu_create(&req, SNMP_PDU_SET);
}
snmp_pdu_free(&resp);
snmp_pdu_free(&req);
return (0);
}