diff common/strhelp.c @ 60:6acb1dae6185

Use strn functions and improve error handling. Even if we know the strings are NULL terminated we use length terminated string functions after a first strlen this makes it easier to assert that at one point we know the string is terminated and afterwards use the length of that.
author Andre Heinecke <aheinecke@intevation.de>
date Tue, 18 Mar 2014 11:28:02 +0000
parents 3f6378647371
children 355800cdefcc
line wrap: on
line diff
--- a/common/strhelp.c	Tue Mar 18 10:04:30 2014 +0000
+++ b/common/strhelp.c	Tue Mar 18 11:28:02 2014 +0000
@@ -29,24 +29,14 @@
 }
 
 char *
-xstrdup( const char *string )
+xstrndup( const char *string, const size_t len )
 {
-    void *p = xmalloc( strlen(string)+1 );
-    strcpy( p, string );
+    char *p = xmalloc( len + 1 );
+    memcpy( p, string, len );
+    p[len] = '\0';
     return p;
 }
 
-
-/**
- * strv_length:
- * @str_array: a %NULL-terminated array of strings
- *
- * Returns the length of the given %NULL-terminated
- * string array @str_array.
- *
- * Return value: length of @str_array.
- *
- */
 unsigned int
 strv_length (char **str_array)
 {
@@ -61,39 +51,37 @@
     return i;
 }
 
-/* @brief append a string to a NULL terminated array of strings.
- *
- * @param[inout] array pointer to the NULL terminated list of string pointers.
- * @param[in] string pointer to the string to append to the list.
- * */
-void array_append_str(char ***pArray, const char *string)
+void array_append_str(char ***pArray, const char *string, const size_t len)
 {
-    unsigned int old_len = strv_length(*pArray);
+    unsigned int old_len = 0;
+
+    if (!*pArray) {
+        *pArray = xmalloc(2 * sizeof(char*));
+        (*pArray)[0] = xstrndup(string, len);
+        (*pArray)[1] = NULL;
+        return;
+    }
+    old_len = strv_length(*pArray);
     *pArray = xrealloc(*pArray, sizeof(char**) * (old_len + 2));
 
-    *pArray[old_len] = xstrdup(string);
-    *pArray[old_len + 1] = NULL;
+    (*pArray)[old_len] = xstrndup(string, len);
+    (*pArray)[old_len + 1] = NULL;
 }
 
-/* @brief append a string to another string.
- *
- * @param[inout] pDst pointer to the string to be extended.
- * @param[in] appendage pointer to the string to append.
- * */
-void str_append_str(char **pDst, const char *appendage)
+void str_append_str(char **pDst, const char *appendage, const size_t len)
 {
-    size_t old_len = strlen(*pDst),
-           added_len = strlen(appendage);
-    size_t new_len = old_len + added_len + 1;
-
     if (!appendage)
         return;
 
-    *pDst = (char *)xrealloc(*pDst, sizeof(char) * (new_len));
-
-    strcpy(*pDst + old_len, appendage);
-
-    *pDst[new_len - 1] = '\0';
+    if (!*pDst) {
+        *pDst = xstrndup(appendage, len);
+    } else {
+        size_t old_len = strlen(*pDst);
+        size_t new_len = old_len + len + 1;
+        *pDst = (char *)xrealloc(*pDst, sizeof(char) * (new_len));
+        strncpy(*pDst + old_len, appendage, new_len);
+        *pDst[new_len] = '\0';
+    }
 }
 
 void

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