From bb9ad82d80a4d2a66654d4aa5eeb60819d7496e2 Mon Sep 17 00:00:00 2001 From: stefan schippers Date: Sat, 27 May 2023 14:13:50 +0200 Subject: [PATCH] fix memory leak when using new resolved_net() fn --- src/hilight.c | 3 ++- src/scheduler.c | 6 ++++-- src/token.c | 9 ++++++--- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/hilight.c b/src/hilight.c index 42e36ead..9a74ee77 100644 --- a/src/hilight.c +++ b/src/hilight.c @@ -1878,7 +1878,8 @@ void select_hilight_net(void) /* returns the full path name of "net" recursively resolving port connections * propagating lower level nets to upper levels. - * "net" can be a bussed net. */ + * "net" can be a bussed net. + * caller *MUST* free returned string */ char *resolved_net(const char *net) { char *rnet = NULL; diff --git a/src/scheduler.c b/src/scheduler.c index 4d0dd580..a1cf16a0 100644 --- a/src/scheduler.c +++ b/src/scheduler.c @@ -2933,7 +2933,7 @@ int xschem(ClientData clientdata, Tcl_Interp *interp, int argc, const char * arg * nets connected to I/O ports are mapped to upper level recursively */ else if(!strcmp(argv[1], "resolved_net")) { - char *net = NULL; + char *net = NULL, *rn = NULL; Tcl_ResetResult(interp); prepare_netlist_structs(0); if(xctx->lastsel == 1) { @@ -2952,7 +2952,9 @@ int xschem(ClientData clientdata, Tcl_Interp *interp, int argc, const char * arg } } } - Tcl_AppendResult(interp, resolved_net(net), NULL); + rn = resolved_net(net); + Tcl_AppendResult(interp, rn, NULL); + my_free(_ALLOC_ID_, &rn); } /* rotate diff --git a/src/token.c b/src/token.c index d85d1d95..0aac9866 100644 --- a/src/token.c +++ b/src/token.c @@ -3290,10 +3290,13 @@ const char *translate(int inst, const char* s) /* @#n:resolved_net attribute (n = pin number or name) will translate to hierarchy-resolved net */ if(!pin_attr_value && !strcmp(pin_attr, "resolved_net")) { + char *rn = NULL; prepare_netlist_structs(0); - my_strdup2(_ALLOC_ID_, &pin_attr_value, - xctx->inst[inst].node && xctx->inst[inst].node[n] ? - resolved_net(xctx->inst[inst].node[n]) : "?"); + if(xctx->inst[inst].node && xctx->inst[inst].node[n]) { + rn = resolved_net(xctx->inst[inst].node[n]); + } + my_strdup2(_ALLOC_ID_, &pin_attr_value, rn ? rn : "?"); + if(rn) my_free(_ALLOC_ID_, &rn); } if(!pin_attr_value ) my_strdup(_ALLOC_ID_, &pin_attr_value, "--UNDEF--");