Browse code

msrp: destroy str array on error

- some code reformatting

(cherry picked from commit 4695b48b23e22135049f67a6ef362e933a0cf337)

Daniel-Constantin Mierla authored on 30/04/2021 06:03:10
Showing 1 changed files
... ...
@@ -610,7 +610,7 @@ msrp_hdr_t *msrp_get_hdr_by_id(msrp_frame_t *mf, int hdrid)
610 610
 /**
611 611
  *
612 612
  */
613
-int msrp_explode_str(str **arr, str *in, str *del)
613
+int msrp_explode_str(str_array_t *arr, str *in, str *del)
614 614
 {
615 615
 	str *larr;
616 616
 	int i;
... ...
@@ -633,20 +633,20 @@ int msrp_explode_str(str **arr, str *in, str *del)
633 633
 	}
634 634
 	n++;
635 635
 
636
-	larr = pkg_malloc(n * sizeof(str));
636
+	larr = pkg_mallocxz(n * sizeof(str));
637 637
 	if(larr==NULL)
638 638
 	{
639 639
 		LM_ERR("no more pkg\n");
640 640
 		return -1;
641 641
 	}
642
-	memset(larr, 0, n * sizeof(str));
643 642
 
644 643
 	k = 0;
645 644
 	if(n==1)
646 645
 	{
647 646
 		larr[k].s = in->s;
648 647
 		larr[k].len = in->len;
649
-		*arr = larr;
648
+		arr->list = larr;
649
+		arr->size = n;
650 650
 		return n;
651 651
 	}
652 652
 
... ...
@@ -657,19 +657,23 @@ int msrp_explode_str(str **arr, str *in, str *del)
657 657
 		{
658 658
 			if(in->s[i]==del->s[j])
659 659
 			{
660
-				if(k<n)
660
+				if(k<n) {
661 661
 					larr[k].len = in->s + i - larr[k].s;
662
+				}
662 663
 				k++;
663
-				if(k<n)
664
+				if(k<n) {
664 665
 					larr[k].s = in->s + i + 1;
666
+				}
665 667
 				break;
666 668
 			}
667 669
 		}
668 670
 	}
669
-	if(k<n)
671
+	if(k<n) {
670 672
 		larr[k].len = in->s + i - larr[k].s;
673
+	}
671 674
 
672
-	*arr = larr;
675
+	arr->list = larr;
676
+	arr->size = n;
673 677
 
674 678
 	return n;
675 679
 }
... ...
@@ -677,7 +681,7 @@ int msrp_explode_str(str **arr, str *in, str *del)
677 681
 /**
678 682
  *
679 683
  */
680
-int msrp_explode_strz(str **arr, str *in, char *del)
684
+int msrp_explode_strz(str_array_t *arr, str *in, char *del)
681 685
 {
682 686
 	str s;
683 687
 
... ...
@@ -693,7 +697,7 @@ void msrp_str_array_destroy(void *data)
693 697
 		return;
694 698
 	arr = (str_array_t*)data;
695 699
 	if(arr->list!=NULL)
696
-			pkg_free(arr->list);
700
+		pkg_free(arr->list);
697 701
 	pkg_free(arr);
698 702
 }
699 703
 
... ...
@@ -705,20 +709,19 @@ int msrp_parse_hdr_uri_list(msrp_hdr_t *hdr)
705 709
 	str_array_t *arr;
706 710
 	str s;
707 711
 
708
-	arr = pkg_malloc(sizeof(str_array_t));
712
+	arr = pkg_mallocxz(sizeof(str_array_t));
709 713
 	if(arr==NULL)
710 714
 	{
711 715
 		LM_ERR("no more pkg\n");
712 716
 		return -1;
713 717
 	}
714
-	memset(arr, 0, sizeof(str_array_t));
715 718
 
716 719
 	s = hdr->body;
717 720
 	trim(&s);
718
-	arr->size = msrp_explode_strz(&arr->list, &s, " ");
719
-	if(arr->size<0)
721
+	if(msrp_explode_strz(arr, &s, " ")<0)
720 722
 	{
721 723
 		LM_ERR("failed to explode\n");
724
+		msrp_str_array_destroy(arr);
722 725
 		return -1;
723 726
 	}
724 727
 	hdr->parsed.flags |= MSRP_DATA_SET;