@@ -2206,18 +2206,17 @@ static int pv_get_msg_branch_fields(struct sip_msg *msg, pv_param_t *param,
22062206 if (msg == NULL || res == NULL )
22072207 return -1 ;
22082208
2209- if ( (size = get_dset_size ()) == 0 )
2210- return pv_get_null (msg , param , res );
2209+ size = get_dset_size () + 1 ;
22112210
22122211 /* get the index */
22132212 if (pv_get_spec_index (msg , param , & idx , & idxf )!= 0 ) {
22142213 LM_ERR ("invalid index\n" );
22152214 return -1 ;
22162215 }
22172216
2218- if (idxf != PV_IDX_ALL && idx == 0 ) {
2217+ if (idxf == 0 && idx == 0 ) {
22192218 /* no index specified -> operate with the last branch */
2220- return get_msg_branch_field ( msg , size - 1 , field , res );
2219+ return get_msg_branch_field ( msg , size - 1 , field , res );
22212220 }
22222221
22232222 if (idxf == PV_IDX_ALL ) {
@@ -2327,16 +2326,19 @@ static int pv_get_msg_branch_attr(struct sip_msg *msg, pv_param_t *param,
23272326 return -1 ;
23282327 }
23292328
2329+ size = get_dset_size () + 1 /* ruri branch*/ ;
2330+ /* if no branch set, consider the last one */
2331+ if (idxf == 0 && idx == 0 )
2332+ idx = size - 1 ;
2333+
23302334 /* numerical index */
23312335 if (idx < 0 ) {
2332- size = get_dset_size () + 1 /* ruri branch*/ ;
23332336 /* index from the end */
23342337 if (- idx > size )
2335- return pv_get_null ( msg , param , res ) ;
2338+ return -1 ;
23362339 idx = size + idx ;
23372340 }
23382341
2339-
23402342 n = get_msg_branch_attr ( idx , attr_name , & attr_flags , & val );
23412343 if ( n < 0 || attr_flags & AVP_VAL_NULL )
23422344 return pv_get_null (msg , param , res );
@@ -2357,9 +2359,16 @@ static int pv_get_msg_branch_attr(struct sip_msg *msg, pv_param_t *param,
23572359static int pv_get_msg_branch_lastidx (struct sip_msg * msg , pv_param_t * param ,
23582360 pv_value_t * res )
23592361{
2362+ int idx ;
2363+
23602364 /* the last index is the size -1, counting all branches, as RURI as
23612365 * branch 0 followed by the added branches in dset */
2362- return get_dset_size ();
2366+ idx = get_dset_size ();
2367+ res -> flags = PV_VAL_STR ;
2368+ res -> rs .s = sint2str ( idx , & res -> rs .len );
2369+ res -> ri = idx ;
2370+ res -> flags |= PV_VAL_INT |PV_TYPE_INT ;
2371+ return 0 ;
23632372}
23642373
23652374
@@ -3486,29 +3495,37 @@ static int _int_pv_set_branch_fields(struct sip_msg* msg, pv_param_t *param,
34863495 return -1 ;
34873496 }
34883497
3489- if (idxf == PV_IDX_ALL ) {
3498+ if (idxf == PV_IDX_ALL ) {
34903499 LM_ERR ("SCRIPT BUG - * not allowed in branch assignment\n" );
34913500 return -1 ;
34923501 }
34933502
34943503 if (has_ruri_branch ) {
3504+ size = get_dset_size () + 1 ;
3505+ /* no branch set, consider the last one */
3506+ if (idxf == 0 && idx == 0 )
3507+ idx = size - 1 ;
3508+ else
3509+ /* if negative, count from the end */
3510+ if (idx < 0 )
3511+ idx = size + idx ;
3512+ /* now evaluate and ajust the idx to dset only */
34953513 if (idx == 0 ) {
34963514 is_ruri_branch = 1 ;
34973515 } else {
34983516 /* offset with -1 */
34993517 idx -- ;
35003518 }
3519+ } else {
3520+ size = get_dset_size ();
3521+ /* if negative, count from the end */
3522+ if (idx < 0 )
3523+ idx = size + idx ;
35013524 }
35023525
35033526 if (!is_ruri_branch ) {
35043527 /* if not RURI branch, it is a msg branch, so check the idx*/
3505- size = get_dset_size ();
3506-
3507- if (idx < 0 ) {
3508- idx = size + idx ;
3509- }
3510-
3511- if (idx < 0 || idx >=size ) {
3528+ if (idx < 0 || idx >=get_dset_size ()) {
35123529 LM_ERR ("inexisting branch assignment [%d/%d]\n" , size , idx );
35133530 return -1 ;
35143531 }
@@ -3666,9 +3683,13 @@ static int pv_set_msg_branch_attr(struct sip_msg* msg, pv_param_t *param,
36663683 return -1 ;
36673684 }
36683685
3686+ size = get_dset_size () + 1 /* ruri branch*/ ;
3687+ /* if no branch set, consider the last one */
3688+ if (idxf == 0 && idx == 0 )
3689+ idx = size - 1 ;
3690+
36693691 /* numerical index */
36703692 if (idx < 0 ) {
3671- size = get_dset_size () + 1 /* ruri branch*/ ;
36723693 /* index from the end */
36733694 if (- idx > size )
36743695 return -1 ;
@@ -3691,7 +3712,6 @@ static int pv_set_msg_branch_attr(struct sip_msg* msg, pv_param_t *param,
36913712}
36923713
36933714
3694-
36953715static int pv_set_force_sock (struct sip_msg * msg , pv_param_t * param ,
36963716 int op , pv_value_t * val )
36973717{
@@ -4272,7 +4292,11 @@ static int msg_branch_flag_set(struct sip_msg* msg, pv_param_t *param, int op,
42724292 return -1 ;
42734293 }
42744294
4275- size = get_dset_size ();
4295+ size = get_dset_size () + 1 ;
4296+
4297+ /* if no idx, work with the last branch */
4298+ if (idxf == 0 && idx == 0 )
4299+ idx = size - 1 ;
42764300
42774301 if (idx < 0 ) {
42784302 idx = size + idx ;
0 commit comments