diff common/strhelp.c @ 91:80ab2168760f

Also add output size handling to str_append_str
author Andre Heinecke <aheinecke@intevation.de>
date Fri, 21 Mar 2014 09:47:05 +0000
parents 355800cdefcc
children c602d8cfa619
line wrap: on
line diff
--- a/common/strhelp.c	Fri Mar 21 09:45:54 2014 +0000
+++ b/common/strhelp.c	Fri Mar 21 09:47:05 2014 +0000
@@ -68,19 +68,24 @@
     (*pArray)[old_len + 1] = NULL;
 }
 
-void str_append_str(char **pDst, const char *appendage, const size_t len)
+void
+str_append_str(char **pDst, size_t *dst_len, const char *appendage, const size_t len)
 {
     if (!appendage)
         return;
 
-    if (!*pDst) {
+    if (!(*pDst)) {
         *pDst = xstrndup(appendage, len);
+        *dst_len = len;
     } else {
-        size_t old_len = strlen(*pDst);
-        size_t new_len = old_len + len + 1;
-        *pDst = (char *)xrealloc(*pDst, sizeof(char) * (new_len + 1));
-        strncpy(*pDst + old_len, appendage, len);
-        (*pDst)[new_len] = '\0';
+        size_t new_size = (*dst_len) + len + 1;
+        char *p_old = *pDst;
+        *pDst = xmalloc(new_size);
+        strncpy(*pDst, p_old, *dst_len);
+        strncpy(*pDst + *dst_len, appendage, len);
+        *dst_len = new_size - 1;
+        (*pDst)[*dst_len] = '\0';
+        free (p_old);
     }
 }
 

http://wald.intevation.org/projects/trustbridge/