Commit c97647ae53 for openssl.org

commit c97647ae534577f8f7b05fb98f3fa6afe1b1eee2
Author: Bob Beck <beck@openssl.org>
Date:   Wed Jun 17 20:52:22 2026 -0600

    Detect error-string truncation from snprintf's return value.

    ossl_err_string_int() compared strlen(buf) == len - 1 after snprintf
    to decide the full format was truncated and fell back to a minimal
    one. This was an off by one and used the minimal message when the
    full message would actually fit. Check snprintf's return value
    correctly instead.

    Reviewed-by: Neil Horman <nhorman@openssl.org>
    Reviewed-by: Andrew Dinh <andrewd@openssl.org>
    MergeDate: Wed Aug 26 16:20:33 2026
    (Merged from https://github.com/openssl/openssl/pull/31640)

diff --git a/crypto/err/err.c b/crypto/err/err.c
index 5bc39e9dbe..e945f7c860 100644
--- a/crypto/err/err.c
+++ b/crypto/err/err.c
@@ -516,6 +516,7 @@ void ossl_err_string_int(unsigned long e, const char *func,
     char lsbuf[64], rsbuf[256];
     const char *ls, *rs = NULL;
     unsigned long l, r;
+    int n;

     if (len == 0)
         return;
@@ -548,11 +549,10 @@ void ossl_err_string_int(unsigned long e, const char *func,
         rs = rsbuf;
     }

-    snprintf(buf, len, "error:%08lX:%s:%s:%s", e, ls, func, rs);
-    if (strlen(buf) == len - 1) {
+    n = snprintf(buf, len, "error:%08lX:%s:%s:%s", e, ls, func, rs);
+    if (n < 0 || (size_t)n >= len)
         /* Didn't fit; use a minimal format. */
         snprintf(buf, len, "err:%lx:%lx:%lx:%lx", e, l, 0L, r);
-    }
 }

 void ERR_error_string_n(unsigned long e, char *buf, size_t len)