Skip to content

Commit 3ed66f7

Browse files
davidbendougch
authored andcommitted
Silence -Wformat-truncation warning in crypto/err/err.c
This warning was being tripped because lib_buf and reason_buf made GCC, incorrectly, believe that the strings could get that long, and then attempted to sum up the snprintf to 120, obtained by inlining some things. Those buffers were larger than they needed to be, so bringing it down is sufficient to silence things. That said, the buffer bounds are supplied by the caller and it is expected that truncation can occur, so the warning is just incorrect. The warning can also be silenced by checking the snprintf return value. As we're already trying to detect truncation, we may as well do it with the return value and skip the extra strlen call. Either of the two changes is sufficient to suppress the warning, but both seem worthwhile, so I've done them both. Change-Id: Ia1b1de67bba55da6f0d07e3682165a1820ce2c9e Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/61805 Auto-Submit: David Benjamin <davidben@google.com> Reviewed-by: Bob Beck <bbe@google.com> Commit-Queue: Bob Beck <bbe@google.com> (cherry picked from commit 6bd1e1504670dc96a76eb9858da4117bba586a41)
1 parent 1f65721 commit 3ed66f7

File tree

1 file changed

+9
-10
lines changed

1 file changed

+9
-10
lines changed

crypto/err/err.c

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -557,22 +557,21 @@ char *ERR_error_string_n(uint32_t packed_error, char *buf, size_t len) {
557557
const char *lib_str = err_lib_error_string(packed_error);
558558
const char *reason_str = err_reason_error_string(packed_error);
559559

560-
char lib_buf[64], reason_buf[64];
560+
char lib_buf[32], reason_buf[32];
561561
if (lib_str == NULL) {
562562
snprintf(lib_buf, sizeof(lib_buf), "lib(%u)", lib);
563563
lib_str = lib_buf;
564564
}
565565

566-
if (reason_str == NULL) {
567-
snprintf(reason_buf, sizeof(reason_buf), "reason(%u)", reason);
568-
reason_str = reason_buf;
569-
}
570-
571-
snprintf(buf, len, "error:%08" PRIx32 ":%s:OPENSSL_internal:%s", packed_error,
572-
lib_str, reason_str);
566+
if (reason_str == NULL) {
567+
snprintf(reason_buf, sizeof(reason_buf), "reason(%u)", reason);
568+
reason_str = reason_buf;
569+
}
573570

574-
if (strlen(buf) == len - 1) {
575-
// output may be truncated; make sure we always have 5 colon-separated
571+
int ret = snprintf(buf, len, "error:%08" PRIx32 ":%s:OPENSSL_internal:%s",
572+
packed_error, lib_str, reason_str);
573+
if (ret >= 0 && (size_t)ret >= len) {
574+
// The output was truncated; make sure we always have 5 colon-separated
576575
// fields, i.e. 4 colons.
577576
static const unsigned num_colons = 4;
578577
unsigned i;

0 commit comments

Comments
 (0)