Browse code

core: fix building quoted value for #!subsdefs

Daniel-Constantin Mierla authored on 01/07/2021 09:17:56
Showing 4 changed files
... ...
@@ -2049,11 +2049,12 @@ int pp_define_set(int len, char *text, int mode)
2049 2049
 
2050 2050
 	pp_defines[ppos].value.len = len;
2051 2051
 	pp_defines[ppos].value.s = text;
2052
-	LM_DBG("### setting define ID [%.*s] value [%.*s]\n",
2052
+	LM_DBG("### setting define ID [%.*s] value [%.*s] (mode: %d)\n",
2053 2053
 			pp_defines[ppos].name.len,
2054 2054
 			pp_defines[ppos].name.s,
2055 2055
 			pp_defines[ppos].value.len,
2056
-			pp_defines[ppos].value.s);
2056
+			pp_defines[ppos].value.s,
2057
+			mode);
2057 2058
 	return 0;
2058 2059
 }
2059 2060
 
... ...
@@ -2213,9 +2213,9 @@ event_route_stm:
2213 2213
 preprocess_stm:
2214 2214
 	SUBST STRING { if(pp_subst_add($2)<0) YYERROR; }
2215 2215
 	| SUBST error { yyerror("invalid subst preprocess statement"); }
2216
-	| SUBSTDEF STRING { if(pp_substdef_add($2, 0)<0) YYERROR; }
2216
+	| SUBSTDEF STRING { if(pp_substdef_add($2, KSR_PPDEF_NORMAL)<0) YYERROR; }
2217 2217
 	| SUBSTDEF error { yyerror("invalid substdef preprocess statement"); }
2218
-	| SUBSTDEFS STRING { if(pp_substdef_add($2, 1)<0) YYERROR; }
2218
+	| SUBSTDEFS STRING { if(pp_substdef_add($2, KSR_PPDEF_QUOTED)<0) YYERROR; }
2219 2219
 	| SUBSTDEFS error { yyerror("invalid substdefs preprocess statement"); }
2220 2220
 	;
2221 2221
 
... ...
@@ -30,6 +30,7 @@
30 30
 #include "ut.h"
31 31
 #include "re.h"
32 32
 #include "pvar.h"
33
+#include "pvapi.h"
33 34
 #include "str_list.h"
34 35
 #include "dprint.h"
35 36
 
... ...
@@ -149,25 +150,40 @@ found_repl:
149 150
 	if(memchr(defvalue.s, '$', defvalue.len) != NULL) {
150 151
 		fmsg = faked_msg_get_next();
151 152
 		if(pv_eval_str(fmsg, &newval, &defvalue)>=0) {
152
-			sb = str_list_block_add(&_ksr_substdef_strlist, newval.s, newval.len);
153
-			if(sb==NULL) {
154
-				LM_ERR("failed to handle substdef: [%s]\n", data);
155
-				return -1;
153
+			if(mode!=KSR_PPDEF_QUOTED) {
154
+				sb = str_list_block_add(&_ksr_substdef_strlist, newval.s, newval.len);
155
+				if(sb==NULL) {
156
+					LM_ERR("failed to handle substdef: [%s]\n", data);
157
+					return -1;
158
+				}
159
+				defvalue = sb->s;
160
+			} else {
161
+				defvalue = newval;
156 162
 			}
157
-			defvalue = sb->s;
158 163
 		}
159 164
 	}
160
-	if(pp_define_set(defvalue.len, defvalue.s,
161
-				(mode==1)?KSR_PPDEF_QUOTED:KSR_PPDEF_NORMAL)<0) {
165
+	if(mode==KSR_PPDEF_QUOTED) {
166
+		if(pv_get_buffer_size() < defvalue.len + 4) {
167
+			LM_ERR("defined value is too large\n");
168
+			return -1;
169
+		}
170
+		newval.s = pv_get_buffer();
171
+		newval.s[0] = '"';
172
+		memcpy(newval.s + 1, defvalue.s, defvalue.len);
173
+		newval.s[defvalue.len + 1] = '"';
174
+		newval.s[defvalue.len + 2] = '\0';
175
+		newval.len = defvalue.len + 2;
176
+		sb = str_list_block_add(&_ksr_substdef_strlist, newval.s, newval.len);
177
+		if(sb==NULL) {
178
+			LM_ERR("failed to handle substdef: [%s]\n", data);
179
+			return -1;
180
+		}
181
+		defvalue = sb->s;
182
+	}
183
+	if(pp_define_set(defvalue.len, defvalue.s, KSR_PPDEF_QUOTED)<0) {
162 184
 		LM_ERR("cannot set define value\n");
163 185
 		goto error;
164 186
 	}
165
-	if(mode==1) {
166
-		defvalue.s++;
167
-		defvalue.len -= 2;
168
-		*(defvalue.s-1) = c;
169
-		defvalue.s[defvalue.len] = c;
170
-	}
171 187
 
172 188
 	LM_DBG("### added substdef: [%.*s]=[%.*s] (%d)\n", defname.len, defname.s,
173 189
 			defvalue.len, defvalue.s, mode);
... ...
@@ -2349,7 +2349,7 @@ int main(int argc, char** argv)
2349 2349
 						fprintf(stderr, "bad substdef parameter\n");
2350 2350
 						goto error;
2351 2351
 					}
2352
-					if(pp_substdef_add(optarg, 0)<0) {
2352
+					if(pp_substdef_add(optarg, KSR_PPDEF_NORMAL)<0) {
2353 2353
 						LM_ERR("failed to add substdef expression: %s\n", optarg);
2354 2354
 						goto error;
2355 2355
 					}
... ...
@@ -2359,7 +2359,7 @@ int main(int argc, char** argv)
2359 2359
 						fprintf(stderr, "bad substdefs parameter\n");
2360 2360
 						goto error;
2361 2361
 					}
2362
-					if(pp_substdef_add(optarg, 1)<0) {
2362
+					if(pp_substdef_add(optarg, KSR_PPDEF_QUOTED)<0) {
2363 2363
 						LM_ERR("failed to add substdefs expression: %s\n", optarg);
2364 2364
 						goto error;
2365 2365
 					}