diff options
author | Richard Levitte <levitte@openssl.org> | 2016-05-21 03:46:43 +0200 |
---|---|---|
committer | Richard Levitte <levitte@openssl.org> | 2016-05-27 17:47:25 +0200 |
commit | 230c691a5218f355a63ff12cd72ce99178378c64 (patch) | |
tree | 116c3f4b57f44c4e84dcc3860bfe8b78d04cb1be /crypto/bio/b_print.c | |
parent | ac1a998d04a66e69ad36141a0254cf8baf3aa5d0 (diff) | |
download | openssl-new-230c691a5218f355a63ff12cd72ce99178378c64.tar.gz |
Fix fmtstr for BIO_printf() et al
- If we have a maximum amount of characters permitted to be printed
(for example "%.2s", which allows for a maximum of 2 chars), we
minimize the number of characters from the string to printed to
that size.
- If there is space for padding and there is a maximum amount of
characters to print (for example "%3.2s", which shall give at
least a 1 space padding), the amount of characters to pad with
gets added to the maximum so the minimum field size (3 in this
example) gets filled out.
Reviewed-by: Matt Caswell <matt@openssl.org>
Diffstat (limited to 'crypto/bio/b_print.c')
-rw-r--r-- | crypto/bio/b_print.c | 15 |
1 files changed, 8 insertions, 7 deletions
diff --git a/crypto/bio/b_print.c b/crypto/bio/b_print.c index d52ad7cdf5..545c469810 100644 --- a/crypto/bio/b_print.c +++ b/crypto/bio/b_print.c @@ -10,7 +10,7 @@ #include <stdio.h> #include <string.h> #include <ctype.h> -#include <limits.h> +#include "internal/numbers.h" #include "internal/cryptlib.h" #ifndef NO_SYS_TYPES_H # include <sys/types.h> @@ -385,28 +385,29 @@ fmtstr(char **sbuffer, if (value == 0) value = "<NULL>"; - strln = strlen(value); - if (strln > INT_MAX) - strln = INT_MAX; + strln = OPENSSL_strnlen(value, max < 0 ? SIZE_MAX : (size_t)max); padlen = min - strln; if (min < 0 || padlen < 0) padlen = 0; + if (max >= 0) + max += padlen; /* The maximum output including padding */ if (flags & DP_F_MINUS) padlen = -padlen; - while ((padlen > 0) && (cnt < max)) { + while ((padlen > 0) && (max < 0 || cnt < max)) { if(!doapr_outch(sbuffer, buffer, currlen, maxlen, ' ')) return 0; --padlen; ++cnt; } - while (*value && (cnt < max)) { + while (strln > 0 && (max < 0 || cnt < max)) { if(!doapr_outch(sbuffer, buffer, currlen, maxlen, *value++)) return 0; + --strln; ++cnt; } - while ((padlen < 0) && (cnt < max)) { + while ((padlen < 0) && (max < 0 || cnt < max)) { if(!doapr_outch(sbuffer, buffer, currlen, maxlen, ' ')) return 0; ++padlen; |