changeset 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 899fcddb92d0
children 4980b0deb773
files cinst/main.c common/errorcodes.h common/strhelp.c common/strhelp.h
diffstat 4 files changed, 23 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/cinst/main.c	Fri Mar 21 09:45:54 2014 +0000
+++ b/cinst/main.c	Fri Mar 21 09:47:05 2014 +0000
@@ -60,6 +60,7 @@
 {
     int lines_read = 0;
     int readingList = 0;
+    size_t list_size = 0;
     char buf[MAX_LINE_LENGTH + 1];
 
     if (*certificate_list || *to_install || *to_remove) {
@@ -69,6 +70,10 @@
 
     while (fgets(buf, MAX_LINE_LENGTH + 1, stdin)) {
         size_t len = strlen(buf); /* fgets ensures buf is terminated */
+        if (len < 2) {
+            printf("Line to short.\n");
+            return ERR_INVALID_INPUT;
+        }
         if (lines_read ++ > MAX_LINES) {
             printf("Too many lines\n");
             return ERR_TOO_MUCH_INPUT;
@@ -82,7 +87,7 @@
             continue;
         }
         if (readingList) {
-            str_append_str(certificate_list, buf, len);
+            str_append_str(certificate_list, &list_size, buf, len);
             continue;
         }
         if (*buf == 'I') {
--- a/common/errorcodes.h	Fri Mar 21 09:45:54 2014 +0000
+++ b/common/errorcodes.h	Fri Mar 21 09:47:05 2014 +0000
@@ -15,5 +15,7 @@
 #define ERR_STORE_ACCESS_DENIED 7
 /* Failed to add certificate to store */
 #define ERR_STORE_ADD_FAILURE 7
+/* Generic invalid input */
+#define ERR_INVALID_INPUT 8
 
 #endif
--- 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);
     }
 }
 
--- a/common/strhelp.h	Fri Mar 21 09:45:54 2014 +0000
+++ b/common/strhelp.h	Fri Mar 21 09:47:05 2014 +0000
@@ -34,10 +34,12 @@
 /* @brief append a string to another string.
  *
  * @param[inout] pDst pointer to the string to be extended.
+ * @param[inout] dst_len length of the dst string. Will be modified.
  * @param[in] appendage pointer to the string to append.
  * @param[in] len length of the string to append.
  * */
-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);
 
 void strfreev (char **str_array);
 #endif

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