From 574f744cc44f5ecfa1451d7021dc75b9a4036de1 Mon Sep 17 00:00:00 2001 From: Vlad Patrascu Date: Mon, 18 Mar 2019 19:31:37 +0200 Subject: [PATCH] Allow fixups to be run for omitted optional parameters --- mod_fix.c | 40 +++++++++++++++++++++++++++++++--------- sr_module.h | 4 ++++ 2 files changed, 35 insertions(+), 9 deletions(-) diff --git a/mod_fix.c b/mod_fix.c index 9c9781b1550..fb305ee82f5 100644 --- a/mod_fix.c +++ b/mod_fix.c @@ -70,6 +70,20 @@ int fixup_regcomp(regex_t **re, str *re_str, int dup_nt) return 0; } +static inline gparam_p alloc_gp(void) +{ + gparam_p gp; + + gp = pkg_malloc(sizeof *gp); + if (!gp) { + LM_ERR("no more pkg memory\n"); + return NULL; + } + memset(gp, 0, sizeof *gp); + + return gp; +} + int fix_cmd(struct cmd_param *params, action_elem_t *elems) { int i; @@ -82,22 +96,30 @@ int fix_cmd(struct cmd_param *params, action_elem_t *elems) for (param=params, i=1; param->flags; param++, i++) { if ((elems[i].type == NOSUBTYPE) || (elems[i].type == NULLV_ST)) { - if (param->flags & CMD_PARAM_OPT) + if (param->flags & CMD_PARAM_OPT) { + if (param->fixup && (param->flags & CMD_PARAM_FIX_NULL)) { + if ((gp = alloc_gp()) == NULL) + return E_OUT_OF_MEM; + + gp->v.val = NULL; + if (param->fixup(&gp->v.val) < 0) { + LM_ERR("Fixup failed for param [%d]\n", i); + ret = E_UNSPEC; + goto error; + } + gp->type = GPARAM_TYPE_FIXUP; + } + continue; - else { + } else { LM_BUG("Mandatory parameter missing\n"); ret = E_BUG; goto error; } } - gp = pkg_malloc(sizeof *gp); - if (!gp) { - LM_ERR("no more pkg memory\n"); - ret = E_OUT_OF_MEM; - goto error; - } - memset(gp, 0, sizeof *gp); + if ((gp = alloc_gp()) == NULL) + return E_OUT_OF_MEM; if (param->flags & CMD_PARAM_INT) { diff --git a/sr_module.h b/sr_module.h index 5e3d64a2b07..6f3a291c5cd 100644 --- a/sr_module.h +++ b/sr_module.h @@ -108,11 +108,15 @@ typedef int (*mod_proc_wrapper)(); #define MAX_CMD_PARAMS (MAX_ACTION_ELEMS-1) + +/* parameter type flags */ #define CMD_PARAM_INT (1<<0) /* integer parameter */ #define CMD_PARAM_STR (1<<1) /* string parameter */ #define CMD_PARAM_VAR (1<<2) /* PV spec parameter */ #define CMD_PARAM_REGEX (1<<3) /* regexp string parameter */ + #define CMD_PARAM_OPT (1<<4) /* optional parameter */ +#define CMD_PARAM_FIX_NULL (1<<5) /* run fixup even if optional parameter is omitted */ struct cmd_param { int flags; /* parameter flags */