[sr-dev] git:5.4:4e509306: core: Zero terminate str copies made with (pkg|shm)_str_dup()

Daniel-Constantin Mierla miconda at gmail.com
Fri Oct 23 11:09:25 CEST 2020


Module: kamailio
Branch: 5.4
Commit: 4e50930695e288b0285370cc7fd26801cbf751de
URL: https://github.com/kamailio/kamailio/commit/4e50930695e288b0285370cc7fd26801cbf751de

Author: Alex Hermann <alex at hexla.nl>
Committer: Daniel-Constantin Mierla <miconda at gmail.com>
Date: 2020-10-23T10:56:55+02:00

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)

---

Modified: src/core/ut.h

---

Diff:  https://github.com/kamailio/kamailio/commit/4e50930695e288b0285370cc7fd26801cbf751de.diff
Patch: https://github.com/kamailio/kamailio/commit/4e50930695e288b0285370cc7fd26801cbf751de.patch

---

diff --git a/src/core/ut.h b/src/core/ut.h
index 56c1f9e157..51d14d9bb9 100644
--- a/src/core/ut.h
+++ b/src/core/ut.h
@@ -748,6 +748,7 @@ static inline int strz2sint(char* _s, int* _r)
 
 /**
  * \brief Make a copy of a str structure to a str using shm_malloc
+ *        The copy will be zero-terminated
  * \param dst destination
  * \param src source
  * \return 0 on success, -1 on failure
@@ -775,7 +776,7 @@ static inline int shm_str_dup(str* dst, const str* src)
 		dst->len = src->len;
 	}
 
-	dst->s = (char*)shm_malloc(dst->len);
+	dst->s = (char*)shm_malloc(dst->len+1);
 	if (dst->s == NULL) {
 		SHM_MEM_ERROR;
 		return -1;
@@ -788,6 +789,7 @@ static inline int shm_str_dup(str* dst, const str* src)
 	}
 
 	memcpy(dst->s, src->s, dst->len);
+	dst->s[dst->len] = 0;
 
 	return 0;
 }
@@ -848,6 +850,7 @@ static inline char* shm_str2char_dup(str *src)
 
 /**
  * \brief Make a copy of a str structure using pkg_malloc
+ *        The copy will be zero-terminated
  * \param dst destination
  * \param src source
  * \return 0 on success, -1 on failure
@@ -875,7 +878,7 @@ static inline int pkg_str_dup(str* dst, const str* src)
 		dst->len = src->len;
 	}
 
-	dst->s = (char*)pkg_malloc(dst->len);
+	dst->s = (char*)pkg_malloc(dst->len+1);
 	if (dst->s == NULL) {
 		PKG_MEM_ERROR;
 		return -1;
@@ -888,6 +891,7 @@ static inline int pkg_str_dup(str* dst, const str* src)
 	}
 
 	memcpy(dst->s, src->s, dst->len);
+	dst->s[dst->len] = 0;
 
 	return 0;
 }




More information about the sr-dev mailing list