[openssl-commits] [openssl] master update

paul.dale at oracle.com paul.dale at oracle.com
Wed Jul 5 22:04:49 UTC 2017


The branch master has been updated
       via  b2ac85adac85f95af999df96367872d38068b028 (commit)
      from  9a0953ed768571d2c6077b9698be718cc9a8a367 (commit)


- Log -----------------------------------------------------------------
commit b2ac85adac85f95af999df96367872d38068b028
Author: Pauli <paul.dale at oracle.com>
Date:   Thu Jul 6 08:03:58 2017 +1000

    Rework the append_buf function
    
    It won't overflow the buffer and will allocate new buffers sufficiently large to
    hold new strings longer than the expansion factor.
    
    Reviewed-by: Richard Levitte <levitte at openssl.org>
    (Merged from https://github.com/openssl/openssl/pull/3847)

-----------------------------------------------------------------------

Summary of changes:
 apps/engine.c | 46 ++++++++++++++++++++++++++++------------------
 1 file changed, 28 insertions(+), 18 deletions(-)

diff --git a/apps/engine.c b/apps/engine.c
index 61fb758..5d47082 100644
--- a/apps/engine.c
+++ b/apps/engine.c
@@ -48,28 +48,38 @@ const OPTIONS engine_options[] = {
 
 static int append_buf(char **buf, int *size, const char *s)
 {
-    if (*buf == NULL) {
-        *size = 256;
-        *buf = app_malloc(*size, "engine buffer");
-        **buf = '\0';
-    }
+    const int expand = 256;
+    int len = strlen(s) + 1;
+    char *p = *buf;
+
+    if (p == NULL) {
+        *size = ((len + expand - 1) / expand) * expand;
+        p = *buf = app_malloc(*size, "engine buffer");
+    } else {
+        const int blen = strlen(p);
+
+        if (blen > 0)
+            len += 2 + blen;
+
+        if (len > *size) {
+            *size = ((len + expand - 1) / expand) * expand;
+            p = OPENSSL_realloc(p, *size);
+            if (p == NULL) {
+                OPENSSL_free(*buf);
+                *buf = NULL;
+                return 0;
+            }
+            *buf = p;
+        }
 
-    if (strlen(*buf) + strlen(s) >= (unsigned int)*size) {
-        char *tmp;
-        *size += 256;
-        tmp = OPENSSL_realloc(*buf, *size);
-        if (tmp == NULL) {
-            OPENSSL_free(*buf);
-            *buf = NULL;
-            return 0;
+        if (blen > 0) {
+            p += blen;
+            *p++ = ',';
+            *p++ = ' ';
         }
-        *buf = tmp;
     }
 
-    if (**buf != '\0')
-        strcat(*buf, ", ");
-    strcat(*buf, s);
-
+    strcpy(p, s);
     return 1;
 }
 


More information about the openssl-commits mailing list