BIO_printf() can fail to print the last character
authorMatt Caswell <matt@openssl.org>
Fri, 3 Jun 2016 14:53:54 +0000 (15:53 +0100)
committerMatt Caswell <matt@openssl.org>
Fri, 3 Jun 2016 19:29:04 +0000 (20:29 +0100)
If the string to print is exactly 2048 character long (excluding the NULL
terminator) then BIO_printf will chop off the last byte. This is because
it has filled its static buffer but hasn't yet allocated a dynamic buffer.
In cases where we don't have a dynamic buffer we need to truncate but that
is not the case for BIO_printf(). We need to check whether we are able to
have a dynamic buffer buffer deciding to truncate.

Reviewed-by: Rich Salz <rsalz@openssl.org>
crypto/bio/b_print.c

index 1b70bac71b1d6350dfe3d766026ac6e21a3b407c..6808cdc6de65cac84620dc96afafd477575c3c24 100644 (file)
@@ -363,9 +363,15 @@ _dopr(char **sbuffer,
             break;
         }
     }
             break;
         }
     }
-    *truncated = (currlen > *maxlen - 1);
-    if (*truncated)
-        currlen = *maxlen - 1;
+    /*
+     * We have to truncate if there is no dynamic buffer and we have filled the
+     * static buffer.
+     */
+    if (buffer == NULL) {
+        *truncated = (currlen > *maxlen - 1);
+        if (*truncated)
+            currlen = *maxlen - 1;
+    }
     if(!doapr_outch(sbuffer, buffer, &currlen, maxlen, '\0'))
         return 0;
     *retlen = currlen - 1;
     if(!doapr_outch(sbuffer, buffer, &currlen, maxlen, '\0'))
         return 0;
     *retlen = currlen - 1;