Browse code

core: Zero terminate str copies made with (pkg|shm)_str_dup()

As discussed in #2512, zero-terminating all copies is preferred as most str
usage is already with zero-terminated str.s.

(cherry picked from commit 0ea69b16db298842f58a6fcaaab2c0ee1a137b94)

Alex Hermann authored on 20/10/2020 08:13:29 • Daniel-Constantin Mierla committed on 23/10/2020 08:56:55
Showing 1 changed files
... ...
@@ -748,6 +748,7 @@ static inline int strz2sint(char* _s, int* _r)
748 748
 
749 749
 /**
750 750
  * \brief Make a copy of a str structure to a str using shm_malloc
751
+ *        The copy will be zero-terminated
751 752
  * \param dst destination
752 753
  * \param src source
753 754
  * \return 0 on success, -1 on failure
... ...
@@ -775,7 +776,7 @@ static inline int shm_str_dup(str* dst, const str* src)
775 776
 		dst->len = src->len;
776 777
 	}
777 778
 
778
-	dst->s = (char*)shm_malloc(dst->len);
779
+	dst->s = (char*)shm_malloc(dst->len+1);
779 780
 	if (dst->s == NULL) {
780 781
 		SHM_MEM_ERROR;
781 782
 		return -1;
... ...
@@ -788,6 +789,7 @@ static inline int shm_str_dup(str* dst, const str* src)
788 789
 	}
789 790
 
790 791
 	memcpy(dst->s, src->s, dst->len);
792
+	dst->s[dst->len] = 0;
791 793
 
792 794
 	return 0;
793 795
 }
... ...
@@ -848,6 +850,7 @@ static inline char* shm_str2char_dup(str *src)
848 850
 
849 851
 /**
850 852
  * \brief Make a copy of a str structure using pkg_malloc
853
+ *        The copy will be zero-terminated
851 854
  * \param dst destination
852 855
  * \param src source
853 856
  * \return 0 on success, -1 on failure
... ...
@@ -875,7 +878,7 @@ static inline int pkg_str_dup(str* dst, const str* src)
875 878
 		dst->len = src->len;
876 879
 	}
877 880
 
878
-	dst->s = (char*)pkg_malloc(dst->len);
881
+	dst->s = (char*)pkg_malloc(dst->len+1);
879 882
 	if (dst->s == NULL) {
880 883
 		PKG_MEM_ERROR;
881 884
 		return -1;
... ...
@@ -888,6 +891,7 @@ static inline int pkg_str_dup(str* dst, const str* src)
888 891
 	}
889 892
 
890 893
 	memcpy(dst->s, src->s, dst->len);
894
+	dst->s[dst->len] = 0;
891 895
 
892 896
 	return 0;
893 897
 }