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