Commit cae917e2ee for openssl.org
commit cae917e2eedc59d10af19292b0c56db281162389
Author: Bob Beck <beck@openssl.org>
Date: Wed Jun 17 18:51:50 2026 -0600
Convert BIO_snprintf() callers whose existing checks already work
under snprintf semantics.
These call sites already guarded the result with the C99 truncation
idiom, so they can simply use snprintf as a replacement as is.
Reviewed-by: Neil Horman <nhorman@openssl.org>
Reviewed-by: Andrew Dinh <andrewd@openssl.org>
MergeDate: Wed Aug 26 16:20:09 2026
(Merged from https://github.com/openssl/openssl/pull/31640)
diff --git a/apps/lib/http_server.c b/apps/lib/http_server.c
index 1fa65d9212..1c20c9f288 100644
--- a/apps/lib/http_server.c
+++ b/apps/lib/http_server.c
@@ -507,9 +507,9 @@ int http_server_send_asn1_resp(const char *prog, BIO *cbio, int keep_alive,
const ASN1_ITEM *it, const ASN1_VALUE *resp)
{
char buf[200], *p;
- int ret = BIO_snprintf(buf, sizeof(buf), HTTP_1_0 " 200 OK\r\n%s"
- "Content-type: %s\r\n"
- "Content-Length: %d\r\n",
+ int ret = snprintf(buf, sizeof(buf), HTTP_1_0 " 200 OK\r\n%s"
+ "Content-type: %s\r\n"
+ "Content-Length: %d\r\n",
keep_alive ? "Connection: keep-alive\r\n" : "",
content_type,
ASN1_item_i2d(resp, NULL, it));
@@ -533,7 +533,7 @@ int http_server_send_status(const char *prog, BIO *cbio,
int status, const char *reason)
{
char buf[200];
- int ret = BIO_snprintf(buf, sizeof(buf), HTTP_1_0 " %d %s\r\n\r\n",
+ int ret = snprintf(buf, sizeof(buf), HTTP_1_0 " %d %s\r\n\r\n",
/* This implicitly cancels keep-alive */
status, reason);
diff --git a/apps/rehash.c b/apps/rehash.c
index 0f3beef8d9..5755d2b8a5 100644
--- a/apps/rehash.c
+++ b/apps/rehash.c
@@ -408,7 +408,7 @@ static int do_dir(const char *dirname, enum Hash h)
numfiles = sk_OPENSSL_STRING_num(files);
for (n = 0; n < numfiles; ++n) {
filename = sk_OPENSSL_STRING_value(files, n);
- if (BIO_snprintf(buf, buflen, "%s%s%s",
+ if (snprintf(buf, buflen, "%s%s%s",
dirname, pathsep, filename)
>= buflen)
continue;
diff --git a/crypto/cmp/cmp_client.c b/crypto/cmp/cmp_client.c
index 60c769af93..c039941204 100644
--- a/crypto/cmp/cmp_client.c
+++ b/crypto/cmp/cmp_client.c
@@ -310,7 +310,7 @@ static int poll_for_response(OSSL_CMP_CTX *ctx, int sleep, int rid,
}
if (check_after < 0 || (uint64_t)check_after > (sleep ? ULONG_MAX / 1000 : INT_MAX)) {
ERR_raise(ERR_LIB_CMP, CMP_R_CHECKAFTER_OUT_OF_RANGE);
- if (BIO_snprintf(str, OSSL_CMP_PKISI_BUFLEN, "value = %" PRId64,
+ if (snprintf(str, OSSL_CMP_PKISI_BUFLEN, "value = %" PRId64,
check_after)
>= 0)
ERR_add_error_data(1, str);
diff --git a/crypto/cmp/cmp_ctx.c b/crypto/cmp/cmp_ctx.c
index da8277f5fe..5358897107 100644
--- a/crypto/cmp/cmp_ctx.c
+++ b/crypto/cmp/cmp_ctx.c
@@ -377,7 +377,7 @@ int ossl_cmp_print_log(OSSL_CMP_severity level, const OSSL_CMP_CTX *ctx,
if (OSSL_TRACE_ENABLED(CMP)) {
OSSL_TRACE_BEGIN(CMP)
{
- int printed = BIO_snprintf(hugebuf, sizeof(hugebuf),
+ int printed = snprintf(hugebuf, sizeof(hugebuf),
"%s:%s:%d:" OSSL_CMP_LOG_PREFIX "%s: ",
func, file, line, level_str);
if (printed > 0 && (size_t)printed < sizeof(hugebuf)) {
diff --git a/crypto/cmp/cmp_status.c b/crypto/cmp/cmp_status.c
index b13a9a39b0..9b646db682 100644
--- a/crypto/cmp/cmp_status.c
+++ b/crypto/cmp/cmp_status.c
@@ -176,7 +176,7 @@ static char *snprint_PKIStatusInfo_parts(int status, int fail_info,
write_ptr += printed_chars; \
bufsize -= printed_chars;
- printed_chars = BIO_snprintf(write_ptr, bufsize, "%s", status_string);
+ printed_chars = snprintf(write_ptr, bufsize, "%s", status_string);
ADVANCE_BUFFER;
/*
@@ -184,13 +184,13 @@ static char *snprint_PKIStatusInfo_parts(int status, int fail_info,
* if present, print failInfo before statusString because it is more concise
*/
if (fail_info != -1 && fail_info != 0) {
- printed_chars = BIO_snprintf(write_ptr, bufsize, "; PKIFailureInfo: ");
+ printed_chars = snprintf(write_ptr, bufsize, "; PKIFailureInfo: ");
ADVANCE_BUFFER;
for (failure = 0; failure <= OSSL_CMP_PKIFAILUREINFO_MAX; failure++) {
if ((fail_info & (1 << failure)) != 0) {
failure_string = CMP_PKIFAILUREINFO_to_string(failure);
if (failure_string != NULL) {
- printed_chars = BIO_snprintf(write_ptr, bufsize, "%s%s",
+ printed_chars = snprintf(write_ptr, bufsize, "%s%s",
failinfo_found ? ", " : "",
failure_string);
ADVANCE_BUFFER;
@@ -201,19 +201,19 @@ static char *snprint_PKIStatusInfo_parts(int status, int fail_info,
}
if (!failinfo_found && status != OSSL_CMP_PKISTATUS_accepted
&& status != OSSL_CMP_PKISTATUS_grantedWithMods) {
- printed_chars = BIO_snprintf(write_ptr, bufsize, "; <no failure info>");
+ printed_chars = snprintf(write_ptr, bufsize, "; <no failure info>");
ADVANCE_BUFFER;
}
/* statusString sequence is optional and may be empty */
n_status_strings = sk_ASN1_UTF8STRING_num(status_strings);
if (n_status_strings > 0) {
- printed_chars = BIO_snprintf(write_ptr, bufsize, "; StatusString%s: ",
+ printed_chars = snprintf(write_ptr, bufsize, "; StatusString%s: ",
n_status_strings > 1 ? "s" : "");
ADVANCE_BUFFER;
for (i = 0; i < n_status_strings; i++) {
text = sk_ASN1_UTF8STRING_value(status_strings, i);
- printed_chars = BIO_snprintf(write_ptr, bufsize, "\"%.*s\"%s",
+ printed_chars = snprintf(write_ptr, bufsize, "\"%.*s\"%s",
(int)ASN1_STRING_get_length(text),
ASN1_STRING_get0_data(text),
i < n_status_strings - 1 ? ", " : "");
diff --git a/crypto/http/http_client.c b/crypto/http/http_client.c
index 84c511f08c..b9d2c51483 100644
--- a/crypto/http/http_client.c
+++ b/crypto/http/http_client.c
@@ -1508,7 +1508,7 @@ int OSSL_HTTP_proxy_connect(BIO *bio, const char *server, const char *port,
proxyauth = OPENSSL_malloc(len + 1);
if (proxyauth == NULL)
goto end;
- if (BIO_snprintf(proxyauth, len + 1, "%s:%s", proxyuser,
+ if (snprintf(proxyauth, len + 1, "%s:%s", proxyuser,
proxypass != NULL ? proxypass : "")
!= (int)len)
goto proxy_end;
diff --git a/ssl/ssl_cert.c b/ssl/ssl_cert.c
index 3924d963c7..e05c5832e3 100644
--- a/ssl/ssl_cert.c
+++ b/ssl/ssl_cert.c
@@ -986,9 +986,9 @@ int SSL_add_dir_cert_subjects_to_stack(STACK_OF(X509_NAME) *stack,
goto err;
}
#ifdef OPENSSL_SYS_VMS
- r = BIO_snprintf(buf, sizeof(buf), "%s%s", dir, filename);
+ r = snprintf(buf, sizeof(buf), "%s%s", dir, filename);
#else
- r = BIO_snprintf(buf, sizeof(buf), "%s/%s", dir, filename);
+ r = snprintf(buf, sizeof(buf), "%s/%s", dir, filename);
#endif
#ifndef OPENSSL_NO_POSIX_IO
/* Skip subdirectories */
diff --git a/test/cmp_extracerts_dos_test.c b/test/cmp_extracerts_dos_test.c
index 273281c943..bf77c3655c 100644
--- a/test/cmp_extracerts_dos_test.c
+++ b/test/cmp_extracerts_dos_test.c
@@ -124,7 +124,7 @@ static X509 *generate_unique_self_signed_cert(EVP_PKEY *pkey, int index)
ASN1_INTEGER *serial = NULL;
char cn[64];
- BIO_snprintf(cn, sizeof(cn), "attacker-cert-%d", index);
+ snprintf(cn, sizeof(cn), "attacker-cert-%d", index);
if (!TEST_ptr(cert = X509_new())
|| !TEST_true(X509_set_version(cert, X509_VERSION_3)))
diff --git a/test/conf_include_test.c b/test/conf_include_test.c
index cd1d4881b9..f492e98958 100644
--- a/test/conf_include_test.c
+++ b/test/conf_include_test.c
@@ -186,7 +186,11 @@ static int test_check_overflow(void)
char max[(sizeof(long) * 8) / 3 + 3];
char *p;
- p = max + BIO_snprintf(max, sizeof(max), "0%ld", LONG_MAX) - 1;
+ int n = snprintf(max, sizeof(max), "0%ld", LONG_MAX);
+
+ if (!TEST_true(n > 0 && (size_t)n < sizeof(max)))
+ return 0;
+ p = max + n - 1;
setenv("FNORD", max, 1);
if (!TEST_true(NCONF_get_number(NULL, "missing", "FNORD", &val))
|| !TEST_long_eq(val, LONG_MAX))