Browse code

core: clone define value to avoid losing the reference

- impacted also by substdef
- GH #2736

Daniel-Constantin Mierla authored on 25/05/2021 08:24:23
Showing 4 changed files
... ...
@@ -1337,7 +1337,7 @@ IMPORTFILE      "import_file"
1337 1337
 <DEFINE_DATA>\\{CR}		{	count(); ksr_cfg_print_part(yytext); } /* eat the escaped CR */
1338 1338
 <DEFINE_DATA>{CR}		{	count();
1339 1339
 							ksr_cfg_print_part(yytext);
1340
-							if (pp_define_set(strlen(s_buf.s), s_buf.s)) return 1;
1340
+							if (pp_define_set(strlen(s_buf.s), s_buf.s, KSR_PPDEF_NORMAL)) return 1;
1341 1341
 							memset(&s_buf, 0, sizeof(s_buf));
1342 1342
 							state = INITIAL;
1343 1343
 							ksr_cfg_print_initial_state();
... ...
@@ -2007,7 +2007,7 @@ int pp_define(int len, const char *text)
2007 2007
 	return 0;
2008 2008
 }
2009 2009
 
2010
-int pp_define_set(int len, char *text)
2010
+int pp_define_set(int len, char *text, int mode)
2011 2011
 {
2012 2012
 	int ppos;
2013 2013
 
... ...
@@ -2090,7 +2090,7 @@ int pp_define_env(const char *text, int len)
2090 2090
 		LM_ERR("cannot set define name [%s]\n", (char*)text);
2091 2091
 		return -1;
2092 2092
 	}
2093
-	if(pp_define_set(defvalue.len, defvalue.s)<0) {
2093
+	if(pp_define_set(defvalue.len, defvalue.s, KSR_PPDEF_NORMAL)<0) {
2094 2094
 		LM_ERR("cannot set define value [%s]\n", (char*)text);
2095 2095
 		return -1;
2096 2096
 	}
... ...
@@ -143,20 +143,14 @@ found_repl:
143 143
 		LM_ERR("cannot set define name\n");
144 144
 		goto error;
145 145
 	}
146
-	if(mode==1) {
147
-		/* define the value enclosed in double quotes */
148
-		*(defvalue.s-1) = '"';
149
-		defvalue.s[defvalue.len] = '"';
150
-		defvalue.s--;
151
-		defvalue.len += 2;
152
-	}
153 146
 	if(memchr(defvalue.s, '$', defvalue.len) != NULL) {
154 147
 		fmsg = faked_msg_get_next();
155 148
 		if(pv_eval_str(fmsg, &newval, &defvalue)>=0) {
156 149
 			defvalue = newval;
157 150
 		}
158 151
 	}
159
-	if(pp_define_set(defvalue.len, defvalue.s)<0) {
152
+	if(pp_define_set(defvalue.len, defvalue.s,
153
+				(mode==1)?KSR_PPDEF_QUOTED:KSR_PPDEF_NORMAL)<0) {
160 154
 		LM_ERR("cannot set define value\n");
161 155
 		goto error;
162 156
 	}
... ...
@@ -25,6 +25,9 @@
25 25
 
26 26
 #include "str.h"
27 27
 
28
+#define KSR_PPDEF_NORMAL (0)     /* define normal value */
29
+#define KSR_PPDEF_QUOTED (1<<0)  /* define quoted value */
30
+
28 31
 typedef struct ksr_ppdefine {
29 32
 	str name;
30 33
 	str value;
... ...
@@ -39,7 +42,7 @@ int pp_substdef_add(char *data, int mode);
39 42
 int pp_subst_run(char **data);
40 43
 
41 44
 int  pp_define(int len, const char *text);
42
-int  pp_define_set(int len, char *text);
45
+int  pp_define_set(int len, char *text, int mode);
43 46
 int  pp_define_set_type(int type);
44 47
 str *pp_define_get(int len, const char * text);
45 48
 int  pp_define_env(const char * text, int len);
... ...
@@ -2264,7 +2264,7 @@ int main(int argc, char** argv)
2264 2264
 					}
2265 2265
 					if(p) {
2266 2266
 						p++;
2267
-						if(pp_define_set(strlen(p), p)<0) {
2267
+						if(pp_define_set(strlen(p), p, KSR_PPDEF_NORMAL)<0) {
2268 2268
 							fprintf(stderr, "error at define value: -A %s\n",
2269 2269
 								optarg);
2270 2270
 							goto error;