From 9547cc66f5ee35209da988717556dcaf1fb28a23 Mon Sep 17 00:00:00 2001 From: William Tu Date: Wed, 6 Jan 2016 18:35:23 -0800 Subject: [PATCH] test-aa: Fix memory leak reported by valgrind. test case 1698: auto-attach - packet tests Report several leaks at lldp_create_dummy(), the patch fixes the following 3 leaks: {lldp_send (lldp.c:334), lldp_decode (lldp.c:374), lldp_create_dummy (ovs-lldp.c:890)} test_aa_send (test-aa.c:252) test_aa_main (test-aa.c:281) Comments: 1. Create a new function "lldp_destroy_dummy()" because many structures and its elements, ex: lldp_hardware and lldp_chassis, are from stack not heap (see test_aa_send). As a result, calling lldpd_cleanup() is incorrect. 2. Remove lchassis->c_id = xmalloc(ETH_ADDR_LEN); because it is overwritten at test_aa_send() 3. remove memcpy(&hw->h_lport.p_element.system_id.system_mac, lchassis->c_id, lchassis->c_id_len); because the source buf is empty Signed-off-by: William Tu Signed-off-by: Ben Pfaff --- lib/ovs-lldp.c | 34 ++++++++++++++++++++++++++++++---- lib/ovs-lldp.h | 1 + tests/test-aa.c | 5 +++++ 3 files changed, 36 insertions(+), 4 deletions(-) diff --git a/lib/ovs-lldp.c b/lib/ovs-lldp.c index bd1729c72..16225b511 100644 --- a/lib/ovs-lldp.c +++ b/lib/ovs-lldp.c @@ -887,7 +887,6 @@ lldp_create_dummy(void) lchassis->c_cap_enabled = LLDP_CAP_BRIDGE; lchassis->c_id_subtype = LLDP_CHASSISID_SUBTYPE_LLADDR; lchassis->c_id_len = ETH_ADDR_LEN; - lchassis->c_id = xmalloc(ETH_ADDR_LEN); list_init(&lchassis->c_mgmt); lchassis->c_ttl = LLDP_CHASSIS_TTL; @@ -911,8 +910,6 @@ lldp_create_dummy(void) /* Auto Attach element tlv */ hw->h_lport.p_element.type = LLDP_TLV_AA_ELEM_TYPE_CLIENT_VIRTUAL_SWITCH; hw->h_lport.p_element.mgmt_vlan = 0; - memcpy(&hw->h_lport.p_element.system_id.system_mac, - lchassis->c_id, lchassis->c_id_len); hw->h_lport.p_element.system_id.conn_type = LLDP_TLV_AA_ELEM_CONN_TYPE_SINGLE; hw->h_lport.p_element.system_id.rsvd = 0; @@ -950,7 +947,7 @@ lldp_unref(struct lldp *lldp) free(lldp); } -/* Unreference a specific LLDP instance. +/* Reference a specific LLDP instance. */ struct lldp * lldp_ref(const struct lldp *lldp_) @@ -961,3 +958,32 @@ lldp_ref(const struct lldp *lldp_) } return lldp; } + +void +lldp_destroy_dummy(struct lldp *lldp) +{ + struct lldpd_hardware *hw, *hw_next; + struct lldpd_chassis *chassis, *chassis_next; + struct lldpd *cfg; + + if (!lldp) { + return; + } + + cfg = lldp->lldpd; + + LIST_FOR_EACH_SAFE (hw, hw_next, h_entries, &cfg->g_hardware) { + list_remove(&hw->h_entries); + free(hw->h_lport.p_lastframe); + free(hw); + } + + LIST_FOR_EACH_SAFE (chassis, chassis_next, list, &cfg->g_chassis) { + list_remove(&chassis->list); + free(chassis); + } + + free(lldp->lldpd); + free(lldp); +} + diff --git a/lib/ovs-lldp.h b/lib/ovs-lldp.h index e780e5b56..71dff4491 100644 --- a/lib/ovs-lldp.h +++ b/lib/ovs-lldp.h @@ -103,5 +103,6 @@ int aa_mapping_unregister(void *aux); /* Used by unit tests */ struct lldp * lldp_create_dummy(void); +void lldp_destroy_dummy(struct lldp *); #endif /* OVS_LLDP_H */ diff --git a/tests/test-aa.c b/tests/test-aa.c index 2da572d1b..eedefeb72 100644 --- a/tests/test-aa.c +++ b/tests/test-aa.c @@ -267,6 +267,11 @@ test_aa_send(void) /* Verify auto attach values */ check_received_aa(&hardware.h_lport, nport, map_init); + lldpd_chassis_cleanup(nchassis, true); + lldpd_port_cleanup(nport, true); + free(nport); + lldp_destroy_dummy(lldp); + return 0; } -- 2.20.1