Skip to content

Commit

Permalink
Allow writing to AVPs with negative index
Browse files Browse the repository at this point in the history
This includes both SET and DELETE operations, for example:
    $(avp(foo)[-1]) = NULL;
    $(avp(foo)[-1]) = "42";

(cherry picked from commit df362fb)
  • Loading branch information
liviuchircu committed Oct 26, 2022
1 parent 5402546 commit ced7b4c
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 7 deletions.
5 changes: 0 additions & 5 deletions pvar.c
Original file line number Diff line number Diff line change
Expand Up @@ -2787,11 +2787,6 @@ int pv_set_avp(struct sip_msg* msg, pv_param_t *param,
destroy_avps(name_type, avp_name, 1);
else
{
if(idx < 0)
{
LM_ERR("Index with negative value\n");
return -1;
}
destroy_index_avp(name_type, avp_name, idx);
}
return 0;
Expand Down
32 changes: 30 additions & 2 deletions usr_avp.c
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,14 @@ int replace_avp(unsigned short flags, int name, int_str val, int index)
struct usr_avp* avp_new, *avp_del;

if(index < 0) {
LM_ERR("Index with negative value\n");
return -1;
/* convert negative index to 0+ */
int pidx = count_avps(flags, name) + index;
if (pidx < 0) {
LM_DBG("AVP with the specified index (%d) not found\n", index);
return -1;
}

index = pidx;
}

avp_del = search_index_avp(flags, name, 0, index);
Expand Down Expand Up @@ -415,6 +421,17 @@ void destroy_index_avp( unsigned short flags, int name, int index)
{
struct usr_avp *avp = NULL;

if(index < 0) {
/* convert negative index to 0+ */
int pidx = count_avps(flags, name) + index;
if (pidx < 0) {
LM_DBG("AVP with the specified index (%d) not found\n", index);
return;
}

index = pidx;
}

avp = search_index_avp(flags, name, 0, index);
if(avp== NULL) {
LM_DBG("AVP with the specified index not found\n");
Expand All @@ -424,6 +441,17 @@ void destroy_index_avp( unsigned short flags, int name, int index)
destroy_avp( avp );
}

int count_avps(unsigned short flags, int name)
{
struct usr_avp *avp = NULL;
int n = 0;

while ((avp=search_first_avp(flags, name, 0, avp)))
n++;

return n;
}

void destroy_avp_list_bulk( struct usr_avp **list )
{
struct usr_avp *avp, *foo;
Expand Down
1 change: 1 addition & 0 deletions usr_avp.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ struct usr_avp *search_index_avp(unsigned short flags,

/* free functions */
void reset_avps( );
int count_avps(unsigned short flags, int name);
void destroy_avp( struct usr_avp *avp);
void destroy_index_avp( unsigned short flags, int name, int index);
int destroy_avps( unsigned short flags, int name, int all);
Expand Down

0 comments on commit ced7b4c

Please sign in to comment.