From 1b135e4f4764c960744ffa87bc64166c6b46ce30 Mon Sep 17 00:00:00 2001 From: Enji Cooper Date: Sat, 31 Dec 2016 10:01:25 +0000 Subject: [PATCH] snmp_pdu_free the right object at the right time in snmptool_walk r310892 was on the right track, but unfortunately it was resolving the problem incorrectly and accidentally leaking memory in the process. - Call snmp_pdu_free on req before calling snmp_pdu_create on it at the bottom of the outer while loop - Call snmp_pdu_free on resp after calling snmpwalk_nextpdu_create in the inner loop MFC after: 12 days X-MFC with: r310729, r310892 Reported by: valgrind --- usr.sbin/bsnmpd/tools/bsnmptools/bsnmpget.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/usr.sbin/bsnmpd/tools/bsnmptools/bsnmpget.c b/usr.sbin/bsnmpd/tools/bsnmptools/bsnmpget.c index 9b3475fb38f9..0f74eb2ae768 100644 --- a/usr.sbin/bsnmpd/tools/bsnmptools/bsnmpget.c +++ b/usr.sbin/bsnmpd/tools/bsnmptools/bsnmpget.c @@ -422,6 +422,7 @@ snmptool_get(struct snmp_toolinfo *snmptoolctx) snmp_pdu_create(&req, GET_PDUTYPE(snmptoolctx)); } + snmp_pdu_free(&req); snmp_pdu_free(&resp); return (0); @@ -507,6 +508,7 @@ snmptool_walk(struct snmp_toolinfo *snmptoolctx) if (op == SNMP_PDU_GETBULK) snmpget_fix_getbulk(&req, GET_MAXREP(snmptoolctx), GET_NONREP(snmptoolctx)); + snmp_pdu_free(&resp); } /* Just in case our root was a leaf. */ @@ -517,6 +519,7 @@ snmptool_walk(struct snmp_toolinfo *snmptoolctx) snmp_output_err_resp(snmptoolctx, &resp); else snmp_output_resp(snmptoolctx, &(resp), NULL); + snmp_pdu_free(&resp); } else warn("Snmp dialog"); } @@ -526,11 +529,13 @@ snmptool_walk(struct snmp_toolinfo *snmptoolctx) break; } - snmp_pdu_free(&resp); - + snmp_pdu_free(&req); snmp_pdu_create(&req, op); } + snmp_pdu_free(&req); + snmp_pdu_free(&resp); + if (rc == 0) return (0); else