Browse code

core: k style fixup_free fixes

- always restore the original parameter pointer value in
fixup_free function.
- fixup_uint_*() re-written (don't use fparam and hence avoid
a pkg_malloc()).
- remove fixup_free_uint_*(): since the fixup_uint_*() functions
replace the original string pointer directly with an integer,
it's never possible to restore the original value => it's not
possible to have a proper fixup_free_uint_*().
- fixup_regexp_null() and fixup_free_regexp_null() fixed &
re-written. fixup_regexp_null() now uses a hack to save the
original pointer so that fixup_free_regexp_null() can restore
it (now fixup_free_regexp_null() works).
- fixup_pvar_*() and fixup_free_pvar_*() fixed & re-written
(similar with fixup_regexp_null()).
- fixup_igp_pvar(), fixup_igp_pvar_pvar(), fixup_free_igp_pvar()
and fixup_free_igp_pvar_pvar() fixed & re-written
(similar with fixup_regexp_null()).
- added missing fixup_free_* declarations.

Andrei Pelinescu-Onciul authored on 05/08/2010 20:55:18
Showing 2 changed files
... ...
@@ -51,11 +51,8 @@ int fixup_regexpNL_none(void** param, int param_no); /* textops */
51 51
 	{ \
52 52
 		if ((param_no > (maxp)) || (param_no < (minp))) \
53 53
 			return E_UNSPEC; \
54
-		if (*param){ \
55
-			fparam_free_contents((fparam_t*)*param); \
56
-			pkg_free(*param); \
57
-			*param=0; \
58
-		} \
54
+		if (*param) \
55
+			fparam_free_restore(param); \
59 56
 		return 0; \
60 57
 	}
61 58
 
... ...
@@ -150,7 +147,7 @@ int fixup_regexpNL_none(void** param, int param_no); /* textops */
150 150
 		int ret; \
151 151
 		if (param && *param){ \
152 152
 			p=(param_no>(no1))? *param - (long)&((fparam_t*)0)->v : *param;\
153
-			if ((ret=fixup_free_fpt_##suffix(&p, param_no))==0) *param=0; \
153
+			if ((ret=fixup_free_fpt_##suffix(&p, param_no))==0) *param=p; \
154 154
 			return ret; \
155 155
 		} \
156 156
 		return 0; \
... ...
@@ -169,24 +166,286 @@ int fixup_regexpNL_none(void** param, int param_no); /* textops */
169 169
 
170 170
 FIXUP_F1T(str_null, 1, 1, FPARAM_STR)
171 171
 FIXUP_F1T(str_str, 1, 2,  FPARAM_STR)
172
+FIXUP_F1T(str_all, 1, 100,  FPARAM_STR)
172 173
 
173
-/* TODO: int can be converted in place, no need for pkg_malloc'ed fparam_t*/
174
+/*
175
+  no free fixups possible for unit_*
176
+  (they overwrite the pointer with the converted number => the original
177
+   value cannot be recovered)
174 178
 FIXUP_F1T(uint_null, 1, 1, FPARAM_INT)
175 179
 FIXUP_F1T(uint_uint, 1, 2, FPARAM_INT)
180
+*/
181
+
182
+
183
+
184
+int fixup_uint_uint(void** param, int param_no)
185
+{
186
+	str s;
187
+	unsigned int num;
188
+	
189
+	s.s = *param;
190
+	s.len = strlen(s.s);
191
+	if (likely(str2int(&s, &num) == 0)) {
192
+		*param = (void*)(long)num;
193
+	} else
194
+		/* not a number */
195
+		return E_UNSPEC;
196
+	return 0;
197
+}
198
+
199
+
200
+
201
+int fixup_uint_null(void** param, int param_no)
202
+{
203
+	if (param_no == 1)
204
+		return fixup_uint_uint(param, param_no);
205
+	return E_UNSPEC;
206
+}
207
+
176 208
 
209
+/* fixup_regexp_null() has to be written "by hand", since
210
+   it needs to save the original pointer (the fixup users expects
211
+   a pointer to the regex in *param and hence the original value
212
+   needed on free cannot be recovered directly).
177 213
 FIXUP_F1T(regexp_null, 1, 1, FPARAM_REGEX)
214
+*/
178 215
 
216
+struct regex_fixup {
217
+	regex_t regex; /* compiled regex */
218
+	void* orig;    /* original pointer */
219
+};
220
+
221
+int fixup_regexp_null(void** param, int param_no)
222
+{
223
+	struct regex_fixup* re;
224
+	
225
+	if (param_no != 1)
226
+		return E_UNSPEC;
227
+	if ((re=pkg_malloc(sizeof(*re))) ==0) {
228
+		ERR("No memory left\n");
229
+		goto error;
230
+	}
231
+	if (regcomp(&re->regex, *param,
232
+				REG_EXTENDED|REG_ICASE|REG_NEWLINE))
233
+		goto error;
234
+	re->orig = *param;
235
+	*param = re;
236
+	return 0;
237
+error:
238
+	if (re)
239
+		pkg_free(re);
240
+	return E_UNSPEC;
241
+}
242
+
243
+
244
+int fixup_free_regexp_null(void** param, int param_no)
245
+{
246
+	struct regex_fixup* re;
247
+	
248
+	if (param_no != 1)
249
+		return E_UNSPEC;
250
+	if (*param) {
251
+		re = *param;
252
+		*param = re->orig;
253
+		regfree(&re->regex);
254
+		pkg_free(re);
255
+	}
256
+	return 0;
257
+}
258
+
259
+/* fixup_pvar_*() has to be written "by hand", since
260
+   it needs to save the original pointer (the fixup users expects
261
+   a pointer to the pv_spec_t in *param and hence the original value
262
+   needed on free cannot be recovered directly).
179 263
 FIXUP_F1T(pvar_null, 1, 1, FPARAM_PVS)
180 264
 FIXUP_F1T(pvar_pvar, 1, 2, FPARAM_PVS)
265
+*/
266
+
267
+struct pvs_fixup {
268
+	pv_spec_t pvs; /* parsed pv spec */
269
+	void* orig;    /* original pointer */
270
+};
271
+
272
+int fixup_pvar_all(void** param, int param_no)
273
+{
274
+	struct pvs_fixup* pvs_f;
275
+	str name;
276
+	
277
+	pvs_f = 0;
278
+	name.s = *param;
279
+	name.len = strlen(name.s);
280
+	trim(&name);
281
+	if (name.len == 0 || name.s[0] != '$')
282
+		/* not a pvs id */
283
+		goto error;
284
+	if ((pvs_f=pkg_malloc(sizeof(*pvs_f))) == 0) {
285
+		ERR("No memory left\n");
286
+		goto error;
287
+	}
288
+	if (pv_parse_spec2(&name, &pvs_f->pvs, 1) == 0)
289
+		/* not a valid pvs identifier */
290
+		goto error;
291
+	pvs_f->orig = *param;
292
+	*param = pvs_f;
293
+	return 0;
294
+error:
295
+	if (pvs_f)
296
+		pkg_free(pvs_f);
297
+	return E_UNSPEC;
298
+}
299
+
300
+
301
+
302
+int fixup_free_pvar_all(void** param, int param_no)
303
+{
304
+	struct pvs_fixup* pvs_f;
305
+	
306
+	if (*param) {
307
+		pvs_f = *param;
308
+		*param = pvs_f->orig;
309
+		pv_spec_free_contents(&pvs_f->pvs);
310
+		pkg_free(pvs_f);
311
+	}
312
+	return 0;
313
+}
314
+
315
+
316
+
317
+int fixup_pvar_pvar(void** param, int param_no)
318
+{
319
+	if (param_no > 2)
320
+		return E_UNSPEC;
321
+	return fixup_free_pvar_all(param, param_no);
322
+}
323
+
324
+
325
+
326
+int fixup_free_pvar_pvar(void** param, int param_no)
327
+{
328
+	if (param_no > 2)
329
+		return E_UNSPEC;
330
+	return fixup_free_pvar_all(param, param_no);
331
+}
332
+
333
+
334
+
335
+int fixup_pvar_null(void** param, int param_no)
336
+{
337
+	if (param_no != 1)
338
+		return E_UNSPEC;
339
+	return fixup_pvar_all(param, param_no);
340
+}
181 341
 
342
+
343
+
344
+int fixup_free_pvar_null(void** param, int param_no)
345
+{
346
+	if (param_no != 1)
347
+		return E_UNSPEC;
348
+	return fixup_free_pvar_all(param, param_no);
349
+}
350
+
351
+/* must be written "by hand", see above (fixup_pvar_pvar).
182 352
 FIXUP_F2T(pvar_str, 1, 2, 1, FPARAM_PVS, FPARAM_STR)
183 353
 FIXUP_F2T(pvar_str_str, 1, 3, 1, FPARAM_PVS, FPARAM_STR)
354
+*/
355
+
356
+int fixup_pvar_str(void** param, int param_no)
357
+{
358
+	if (param_no == 1)
359
+		return fixup_pvar_all(param, param_no);
360
+	else if (param_no == 2)
361
+		return fixup_str_str(param, param_no);
362
+	return E_UNSPEC;
363
+}
364
+
365
+
366
+
367
+int fixup_free_pvar_str(void** param, int param_no)
368
+{
369
+	if (param_no == 1)
370
+		return fixup_free_pvar_all(param, param_no);
371
+	else if (param_no == 2)
372
+		return fixup_free_str_str(param, param_no);
373
+	return E_UNSPEC;
374
+}
375
+
376
+
377
+
378
+int fixup_pvar_str_str(void** param, int param_no)
379
+{
380
+	if (param_no == 1)
381
+		return fixup_pvar_all(param, param_no);
382
+	else if (param_no == 2 || param_no == 3)
383
+		return fixup_str_all(param, param_no);
384
+	return E_UNSPEC;
385
+}
386
+
387
+
388
+
389
+int fixup_free_pvar_str_str(void** param, int param_no)
390
+{
391
+	if (param_no == 1)
392
+		return fixup_free_pvar_all(param, param_no);
393
+	else if (param_no == 2 || param_no == 3)
394
+		return fixup_free_str_all(param, param_no);
395
+	return E_UNSPEC;
396
+}
397
+
398
+
184 399
 
185 400
 FIXUP_F2FP(igp_null, 1, 1, 1, FPARAM_INT|FPARAM_PVS, 0)
186 401
 FIXUP_F2FP(igp_igp, 1, 2, 2,  FPARAM_INT|FPARAM_PVS, 0)
187
-FIXUP_F2FP(igp_pvar, 1, 2, 1,  FPARAM_INT|FPARAM_PVS, FPARAM_PVS)
188 402
 
403
+/* must be declared by hand, because of the pvar special handling
404
+   (see above)
405
+FIXUP_F2FP(igp_pvar, 1, 2, 1,  FPARAM_INT|FPARAM_PVS, FPARAM_PVS)
189 406
 FIXUP_F2FP_T(igp_pvar_pvar, 1, 3, 1, FPARAM_INT|FPARAM_PVS, FPARAM_PVS)
407
+*/
408
+
409
+int fixup_igp_pvar(void** param, int param_no)
410
+{
411
+	if (param_no == 1)
412
+		return fixup_igp_null(param, param_no);
413
+	else if (param_no == 2)
414
+		return fixup_pvar_all(param, param_no);
415
+	return E_UNSPEC;
416
+}
417
+
418
+
419
+
420
+int fixup_free_igp_pvar(void** param, int param_no)
421
+{
422
+	if (param_no == 1)
423
+		return fixup_free_igp_null(param, param_no);
424
+	else if (param_no == 2)
425
+		return fixup_free_pvar_all(param, param_no);
426
+	return E_UNSPEC;
427
+}
428
+
429
+
430
+
431
+int fixup_igp_pvar_pvar(void** param, int param_no)
432
+{
433
+	if (param_no == 1)
434
+		return fixup_igp_null(param, param_no);
435
+	else if (param_no == 2 || param_no == 3)
436
+		return fixup_pvar_all(param, param_no);
437
+	return E_UNSPEC;
438
+}
439
+
440
+
441
+
442
+int fixup_free_igp_pvar_pvar(void** param, int param_no)
443
+{
444
+	if (param_no == 1)
445
+		return fixup_free_igp_null(param, param_no);
446
+	else if (param_no == 2 || param_no == 3)
447
+		return fixup_free_pvar_all(param, param_no);
448
+	return E_UNSPEC;
449
+}
450
+
451
+
190 452
 
191 453
 /** macro for declaring a spve fixup and the corresponding free_fixup
192 454
   * for a function expecting first no1 params as fparam converted spve 
... ...
@@ -203,21 +462,16 @@ FIXUP_F2FP_T(igp_pvar_pvar, 1, 3, 1, FPARAM_INT|FPARAM_PVS, FPARAM_PVS)
203 203
 	int fixup_##suffix (void** param, int param_no) \
204 204
 	{ \
205 205
 		int ret; \
206
-		char * bkp; \
207 206
 		fparam_t* fp; \
208 207
 		if (param_no<=(no1)){ \
209 208
 			if ((ret=fix_param_types(FPARAM_PVE, param))<0){ \
210
-				ERR("Cannot convert function parameter %d to" #type2 "\n", \
209
+				ERR("Cannot convert function parameter %d to spve \n", \
211 210
 						param_no);\
212 211
 				return E_UNSPEC; \
213 212
 			} else{ \
214 213
 				fp=(fparam_t*)*param; \
215 214
 				if ((ret==0) && (fp->v.pve->spec.getf==0)){ \
216
-					bkp=fp->orig; \
217
-					fp->orig=0; /* make sure orig string is not freed */ \
218
-					fparam_free_contents(fp); \
219
-					pkg_free(fp); \
220
-					*param=bkp; \
215
+					fparam_free_restore(param); \
221 216
 					return fix_param_types(FPARAM_STR, param); \
222 217
 				} else if (ret==1) \
223 218
 					return fix_param_types(FPARAM_STR, param); \
... ...
@@ -229,11 +483,9 @@ FIXUP_F2FP_T(igp_pvar_pvar, 1, 3, 1, FPARAM_INT|FPARAM_PVS, FPARAM_PVS)
229 229
 	int fixup_free_##suffix (void** param, int param_no) \
230 230
 	{ \
231 231
 		if (param && *param){ \
232
-			if (param_no<=(no1)){ \
233
-				fparam_free_contents((fparam_t*)*param); \
234
-				pkg_free(*param); \
235
-				*param=0; \
236
-			} else \
232
+			if (param_no<=(no1)) \
233
+				fparam_free_restore(param); \
234
+			else \
237 235
 				return fixup_free_spvet_##suffix(param, param_no); \
238 236
 		} \
239 237
 		return 0; \
... ...
@@ -106,7 +106,9 @@ int fixup_pvar_str_str(void** param, int param_no);
106 106
 int fixup_free_pvar_str_str(void** param, int param_no);
107 107
 
108 108
 int fixup_igp_igp(void** param, int param_no);
109
+int fixup_free_igp_igp(void** param, int param_no);
109 110
 int fixup_igp_null(void** param, int param_no);
111
+int fixup_free_igp_null(void** param, int param_no);
110 112
 int fixup_get_ivalue(struct sip_msg* msg, gparam_p gp, int *val);
111 113
 
112 114
 int fixup_igp_pvar(void** param, int param_no);
... ...
@@ -116,8 +118,11 @@ int fixup_igp_pvar_pvar(void** param, int param_no);
116 116
 int fixup_free_igp_pvar_pvar(void** param, int param_no);
117 117
 
118 118
 int fixup_spve_spve(void** param, int param_no);
119
+int fixup_free_spve_spve(void** param, int param_no);
119 120
 int fixup_spve_null(void** param, int param_no);
121
+int fixup_free_spve_null(void** param, int param_no);
120 122
 int fixup_spve_uint(void** param, int param_no);
121 123
 int fixup_spve_str(void** param, int param_no);
124
+int fixup_free_spve_str(void** param, int param_no);
122 125
 
123 126
 #endif