From 56e0410047d08ae4da2051b8df08bdb0294aac2a Mon Sep 17 00:00:00 2001 From: stefan schippers Date: Tue, 4 Mar 2025 09:43:26 +0100 Subject: [PATCH] fix a bug in eval_expr() usage in token.c (duplicate return string to avoid double free) --- src/token.c | 11 ++++++----- xschem_library/devices/res.sym | 4 ++-- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/token.c b/src/token.c index e8f04ea7..d11b7990 100644 --- a/src/token.c +++ b/src/token.c @@ -1200,7 +1200,7 @@ static void print_vhdl_primitive(FILE *fd, int inst) /* netlist primitives, 200 } my_strdup2(_ALLOC_ID_, &result, tcl_hook2(result)); /* tcl evaluation if tcleval(....) */ if(strstr(result, "expr(")) { - result = eval_expr(result); + my_strdup2(_ALLOC_ID_, &result, eval_expr(result)); } dbg(1, "print_vhdl_primitive(): after translate3() result=%s\n", result); } @@ -2153,7 +2153,8 @@ int print_spice_element(FILE *fd, int inst) const char *str_ptr=NULL; register int c, state=TOK_BEGIN, space; char *template=NULL,*format=NULL, *s, *name=NULL, *token=NULL; - const char *lab, *value = NULL; + const char *lab; + char *value = NULL; /* char *translatedvalue = NULL; */ size_t sizetok=0; size_t token_pos=0; @@ -2464,7 +2465,7 @@ int print_spice_element(FILE *fd, int inst) } if(strstr(value, "expr(")) { - value = eval_expr(value); + my_strdup2(_ALLOC_ID_, &value, eval_expr(value)); } /* token=%xxxx and xxxx is not defined in prop_ptr or template: return xxxx */ if(!token_exists && token[0] =='%') { @@ -2519,7 +2520,7 @@ int print_spice_element(FILE *fd, int inst) my_strdup(_ALLOC_ID_, &result, tcl_hook2(result)); } if(strstr(result, "expr(")) { - result = eval_expr(result); + my_strdup2(_ALLOC_ID_, &result, eval_expr(result)); } if(result) fprintf(fd, "%s", result); dbg(1, "print_spice_element(): returning |%s|\n", result); @@ -3156,7 +3157,7 @@ static void print_verilog_primitive(FILE *fd, int inst) /* netlist switch level } my_strdup2(_ALLOC_ID_, &result, tcl_hook2(result)); /* tcl evaluation if tcleval(....) */ if(strstr(result, "expr(")) { - result = eval_expr(result); + my_strdup2(_ALLOC_ID_, &result, eval_expr(result)); } dbg(1, "print_verilog_primitive(): after translate3() result=%s\n", result); } diff --git a/xschem_library/devices/res.sym b/xschem_library/devices/res.sym index b03f4db0..6abdfa63 100644 --- a/xschem_library/devices/res.sym +++ b/xschem_library/devices/res.sym @@ -25,8 +25,8 @@ K {type=resistor function0="1" function1="0" -format="@name @pinlist @value m=@m" - +format="@name @@P @@M R=expr( 100 * (@L - 0.1u) / (@W - 0.1u)) SUB=$XX" + verilog_format="tran @name (@@P\\\\, @@M\\\\);" tedax_format="footprint @name @footprint