Avoid negative array index in BIO_debug_callback()
authorBenjamin Kaduk <bkaduk@akamai.com>
Tue, 8 Mar 2016 22:44:57 +0000 (16:44 -0600)
committerRich Salz <rsalz@akamai.com>
Thu, 10 Mar 2016 01:52:19 +0000 (20:52 -0500)
BIO_snprintf() can return -1 on truncation (and overflow as of commit
9cb177301fdab492e4cfef376b28339afe3ef663).  Though neither can
realistically occur while printing a pointer and short fixed string into
a buffer of length 256, the analysis to confirm that this the case goes
somewhat far up the call chain, and not all static analyzers can
successfully follow the chain of logic.

It's easy enough to clamp the returned length to be nonnegative before
continuing, which appeases the static analyzer and does not harm the
subsequent code.

Reviewed-by: Richard Levitte <levitte@openssl.org>
Reviewed-by: Rich Salz <rsalz@openssl.org>
crypto/bio/bio_cb.c

index 4d3365e4eccf6bf1294a027154ea236014bf02e6..ec484b697b296b03ce52b8a773cc2d5c516286e5 100644 (file)
@@ -77,6 +77,9 @@ long BIO_debug_callback(BIO *bio, int cmd, const char *argp,
 
     len = BIO_snprintf(buf,sizeof buf,"BIO[%p]: ",(void *)bio);
 
+    /* Ignore errors and continue printing the other information. */
+    if (len < 0)
+        len = 0;
     p = buf + len;
     p_maxlen = sizeof(buf) - len;