From b5f270ad0923e0e12584ca341df8fc1b11213225 Mon Sep 17 00:00:00 2001 From: Donald Sharp Date: Mon, 4 Dec 2017 18:59:47 -0500 Subject: [PATCH 1/2] lib: Allow memory to be cleaned up for error cases in ptm ptm_lib.c had no way to cleanup after itself when an error was detected. This adds a function to cleanup context in such a case. A followup commit will use this new functionality. Signed-off-by: Donald Sharp --- lib/ptm_lib.c | 19 +++++++++++++++++++ lib/ptm_lib.h | 1 + 2 files changed, 20 insertions(+) diff --git a/lib/ptm_lib.c b/lib/ptm_lib.c index e881d49225..28d26149e5 100644 --- a/lib/ptm_lib.c +++ b/lib/ptm_lib.c @@ -223,6 +223,25 @@ int ptm_lib_init_msg(ptm_lib_handle_t *hdl, int cmd_id, int type, void *in_ctxt, return 0; } +int ptm_lib_cleanup_msg(ptm_lib_handle_t *hdl, void *ctxt) +{ + ptm_lib_msg_ctxt_t *p_ctxt = ctxt; + csv_t *csv; + + if (!p_ctxt) { + ERRLOG("%s: no context \n", __FUNCTION__); + return -1; + } + + csv = p_ctxt->csv; + + csv_clean(csv); + csv_free(csv); + free(p_ctxt); + + return 0; +} + int ptm_lib_complete_msg(ptm_lib_handle_t *hdl, void *ctxt, char *buf, int *len) { ptm_lib_msg_ctxt_t *p_ctxt = ctxt; diff --git a/lib/ptm_lib.h b/lib/ptm_lib.h index bc8fe4ac54..fc4d520dcb 100644 --- a/lib/ptm_lib.h +++ b/lib/ptm_lib.h @@ -63,5 +63,6 @@ int ptm_lib_find_key_in_msg(void *, const char *, char *); int ptm_lib_init_msg(ptm_lib_handle_t *, int, int, void *, void **); int ptm_lib_append_msg(ptm_lib_handle_t *, void *, const char *, const char *); int ptm_lib_complete_msg(ptm_lib_handle_t *, void *, char *, int *); +int ptm_lib_cleanup_msg(ptm_lib_handle_t *, void *); #endif From a928d46462f10284cdc2da06bf6f3eb8eec7ce05 Mon Sep 17 00:00:00 2001 From: Donald Sharp Date: Mon, 4 Dec 2017 19:03:51 -0500 Subject: [PATCH 2/2] zebra: Cleanup leaked context information on failure When we get a STREAM_GET failure of some sort we need to handle the failure case here and safely free up stored memory/context and return gracefully. Signed-off-by: Donald Sharp --- zebra/zebra_ptm.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/zebra/zebra_ptm.c b/zebra/zebra_ptm.c index dcd4fb0239..769d2f5666 100644 --- a/zebra/zebra_ptm.c +++ b/zebra/zebra_ptm.c @@ -817,6 +817,7 @@ int zebra_ptm_bfd_dst_register(struct zserv *client, u_short length, zebra_ptm_send_message(ptm_cb.out_data, data_len); stream_failure: + ptm_lib_cleanup_msg(ptm_hdl, out_ctxt); return 0; } @@ -946,6 +947,7 @@ int zebra_ptm_bfd_dst_deregister(struct zserv *client, u_short length, zebra_ptm_send_message(ptm_cb.out_data, data_len); stream_failure: + ptm_lib_cleanup_msg(ptm_hdl, out_ctxt); return 0; }