Skip to content

Commit 7da6149

Browse files
authored
Merge pull request #7792 from dgarske/sprintf
Fix for using sprintf in test.h
2 parents 3fc7be8 + f9dc5e9 commit 7da6149

9 files changed

Lines changed: 58 additions & 76 deletions

File tree

src/bio.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3345,21 +3345,22 @@ int wolfSSL_BIO_dump(WOLFSSL_BIO *bio, const char *buf, int length)
33453345
return wolfSSL_BIO_write(bio, "\tNULL", 5);
33463346
}
33473347

3348-
XSPRINTF(line, "%04x - ", lineOffset);
3348+
(void)XSNPRINTF(line, sizeof(line), "%04x - ", lineOffset);
33493349
o = 7;
33503350
for (i = 0; i < BIO_DUMP_LINE_LEN; i++) {
33513351
if (i < length)
3352-
XSPRINTF(line + o,"%02x ", (unsigned char)buf[i]);
3352+
(void)XSNPRINTF(line + o, (int)sizeof(line) - o,
3353+
"%02x ", (unsigned char)buf[i]);
33533354
else
3354-
XSPRINTF(line + o, " ");
3355+
(void)XSNPRINTF(line + o, (int)sizeof(line) - o, " ");
33553356
if (i == 7)
3356-
XSPRINTF(line + o + 2, "-");
3357+
(void)XSNPRINTF(line + o + 2, (int)sizeof(line) - (o + 2), "-");
33573358
o += 3;
33583359
}
3359-
XSPRINTF(line + o, " ");
3360+
(void)XSNPRINTF(line + o, (int)sizeof(line) - o, " ");
33603361
o += 2;
33613362
for (i = 0; (i < BIO_DUMP_LINE_LEN) && (i < length); i++) {
3362-
XSPRINTF(line + o, "%c",
3363+
(void)XSNPRINTF(line + o, (int)sizeof(line) - o, "%c",
33633364
((31 < buf[i]) && (buf[i] < 127)) ? buf[i] : '.');
33643365
o++;
33653366
}

src/x509.c

Lines changed: 32 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -7040,18 +7040,13 @@ int wolfSSL_X509_signature_print(WOLFSSL_BIO *bp,
70407040

70417041
for (i = 0; i < length; ++i) {
70427042
char hex_digits[4];
7043-
#ifdef XSNPRINTF
70447043
if (XSNPRINTF(hex_digits, sizeof(hex_digits), "%c%02X", i>0 ? ':' : ' ',
70457044
(unsigned int)sigalg->algorithm->obj[idx+i])
70467045
>= (int)sizeof(hex_digits))
70477046
{
70487047
WOLFSSL_MSG("buffer overrun");
70497048
return WOLFSSL_FAILURE;
70507049
}
7051-
#else
7052-
XSPRINTF(hex_digits, "%c%02X", i>0 ? ':' : ' ',
7053-
(unsigned int)sigalg->algorithm->obj[idx+i]);
7054-
#endif
70557050
if (wolfSSL_BIO_puts(bp, hex_digits) <= 0)
70567051
return WOLFSSL_FAILURE;
70577052
}
@@ -9005,14 +9000,13 @@ int wolfSSL_X509_VERIFY_PARAM_set1_ip(WOLFSSL_X509_VERIFY_PARAM* param,
90059000
if (iplen == 4) {
90069001
/* ipv4 www.xxx.yyy.zzz max 15 length + Null termination */
90079002
buf = (char*)XMALLOC(16, NULL, DYNAMIC_TYPE_TMP_BUFFER);
9008-
90099003
if (!buf) {
90109004
WOLFSSL_MSG("failed malloc");
90119005
return ret;
90129006
}
90139007

9014-
XSPRINTF(buf, "%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);
9015-
buf[15] = '\0';
9008+
(void)XSNPRINTF(buf, 16, "%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);
9009+
buf[15] = '\0'; /* null terminate */
90169010
}
90179011
else if (iplen == 16) {
90189012
/* ipv6 normal address scheme
@@ -9041,47 +9035,46 @@ int wolfSSL_X509_VERIFY_PARAM_set1_ip(WOLFSSL_X509_VERIFY_PARAM* param,
90419035
* to re-construct IP address in ascii.
90429036
*/
90439037
buf = (char*)XMALLOC(max_ipv6_len, NULL, DYNAMIC_TYPE_TMP_BUFFER);
9044-
90459038
if (!buf) {
90469039
WOLFSSL_MSG("failed malloc");
90479040
return ret;
90489041
}
90499042
p = buf;
90509043
for (i = 0; i < 16; i += 2) {
9051-
val = (((word32)(ip[i]<<8)) | (ip[i+1])) & 0xFFFF;
9052-
if (val == 0){
9053-
if (!write_zero) {
9044+
val = (((word32)(ip[i]<<8)) | (ip[i+1])) & 0xFFFF;
9045+
if (val == 0){
9046+
if (!write_zero) {
90549047
*p = ':';
9055-
}
9056-
p++;
9057-
*p = '\0';
9058-
write_zero = 1;
9059-
}
9060-
else {
9061-
if (i != 0)
9062-
*p++ = ':';
9063-
XSPRINTF(p, "%x", val);
9064-
}
9065-
/* sanity check */
9066-
if (XSTRLEN(buf) > max_ipv6_len) {
9067-
WOLFSSL_MSG("The target ip address exceeds buffer length(40)");
9068-
XFREE(buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
9069-
buf = NULL;
9070-
break;
9071-
}
9072-
/* move the pointer to the last */
9073-
/* XSTRLEN includes NULL because of XSPRINTF use */
9074-
p = buf + (XSTRLEN(buf));
9048+
}
9049+
p++;
9050+
*p = '\0';
9051+
write_zero = 1;
9052+
}
9053+
else {
9054+
if (i != 0) {
9055+
*p++ = ':';
9056+
}
9057+
(void)XSNPRINTF(p, max_ipv6_len - (size_t)(p - buf), "%x", val);
9058+
}
9059+
/* sanity check */
9060+
if (XSTRLEN(buf) > max_ipv6_len) {
9061+
WOLFSSL_MSG("The target ip address exceeds buffer length(40)");
9062+
XFREE(buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
9063+
buf = NULL;
9064+
break;
9065+
}
9066+
/* move the pointer to the last */
9067+
/* XSTRLEN includes NULL because of XSPRINTF use */
9068+
p = buf + (XSTRLEN(buf));
90759069
}
90769070
/* termination */
9077-
if(i == 16 && buf) {
9071+
if (i == 16 && buf) {
90789072
p--;
90799073
if ((*p) == ':') {
9080-
/* when the last character is :, the following segments are zero
9081-
* Therefore, adding : and null termination
9082-
*/
9083-
p++;
9084-
*p++ = ':';
9074+
/* when the last character is :, the following segments are zero
9075+
* Therefore, adding : and null termination */
9076+
p++;
9077+
*p++ = ':';
90859078
*p = '\0';
90869079
}
90879080
}
@@ -9092,7 +9085,7 @@ int wolfSSL_X509_VERIFY_PARAM_set1_ip(WOLFSSL_X509_VERIFY_PARAM* param,
90929085
}
90939086

90949087
if (buf) {
9095-
/* set address to ip asc */
9088+
/* set address to ip asc */
90969089
ret = wolfSSL_X509_VERIFY_PARAM_set1_ip_asc(param, buf);
90979090
XFREE(buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
90989091
}

tests/api.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58278,7 +58278,7 @@ static int test_wolfSSL_BIO_connect(void)
5827858278
server_args.signal = &ready;
5827958279
start_thread(test_server_nofail, &server_args, &serverThread);
5828058280
wait_tcp_ready(&server_args);
58281-
ExpectIntGT(XSPRINTF(buff, "%d", ready.port), 0);
58281+
ExpectIntGT(XSNPRINTF(buff, sizeof(buff), "%d", ready.port), 0);
5828258282

5828358283
/* Start the test proper */
5828458284
/* Setup the TCP BIO */
@@ -58325,7 +58325,7 @@ static int test_wolfSSL_BIO_connect(void)
5832558325
server_args.signal = &ready;
5832658326
start_thread(test_server_nofail, &server_args, &serverThread);
5832758327
wait_tcp_ready(&server_args);
58328-
ExpectIntGT(XSPRINTF(buff, "%d", ready.port), 0);
58328+
ExpectIntGT(XSNPRINTF(buff, sizeof(buff), "%d", ready.port), 0);
5832958329

5833058330
ExpectNotNull(sslBio = BIO_new_ssl_connect(ctx));
5833158331
ExpectIntEQ(BIO_set_conn_hostname(sslBio, (char*)wolfSSLIP), 1);

tests/quic.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -848,7 +848,7 @@ static void check_crypto_records(QuicTestContext *from, OutputBuffer *out, int i
848848
rec_name = "Finished";
849849
break;
850850
default:
851-
sprintf(lbuffer, "%d", rec_type);
851+
(void)XSNPRINTF(lbuffer, sizeof(lbuffer), "%d", rec_type);
852852
rec_name = lbuffer;
853853
break;
854854
}

testsuite/testsuite.c

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ static int test_crl_monitor(void)
300300

301301
printf("\nRunning CRL monitor test\n");
302302

303-
sprintf(rounds, "%d", CRL_MONITOR_TEST_ROUNDS);
303+
(void)XSNPRINTF(rounds, sizeof(rounds), "%d", CRL_MONITOR_TEST_ROUNDS);
304304

305305
XMEMSET(&server_args, 0, sizeof(func_args));
306306
XMEMSET(&client_args, 0, sizeof(func_args));
@@ -320,18 +320,19 @@ static int test_crl_monitor(void)
320320
InitTcpReady(&ready);
321321
start_thread(server_test, &server_args, &serverThread);
322322
wait_tcp_ready(&server_args);
323-
sprintf(portNum, "%d", server_args.signal->port);
323+
(void)XSNPRINTF(portNum, sizeof(portNum), "%d", server_args.signal->port);
324324

325325
for (i = 0; i < CRL_MONITOR_TEST_ROUNDS; i++) {
326326
int expectFail;
327327
if (i % 2 == 0) {
328+
328329
/* succeed on even rounds */
329-
sprintf(buf, "%s/%s", tmpDir, "crl.pem");
330+
(void)XSNPRINTF(buf, sizeof(buf), "%s/%s", tmpDir, "crl.pem");
330331
if (STAGE_FILE("certs/crl/crl.pem", buf) != 0) {
331332
fprintf(stderr, "[%d] Failed to copy file to %s\n", i, buf);
332333
goto cleanup;
333334
}
334-
sprintf(buf, "%s/%s", tmpDir, "crl.revoked");
335+
(void)XSNPRINTF(buf, sizeof(buf), "%s/%s", tmpDir, "crl.revoked");
335336
/* The monitor can be holding the file handle and this will cause
336337
* the remove call to fail. Let's give the monitor a some time to
337338
* finish up. */
@@ -349,12 +350,12 @@ static int test_crl_monitor(void)
349350
}
350351
else {
351352
/* fail on odd rounds */
352-
sprintf(buf, "%s/%s", tmpDir, "crl.revoked");
353+
(void)XSNPRINTF(buf, sizeof(buf), "%s/%s", tmpDir, "crl.revoked");
353354
if (STAGE_FILE("certs/crl/crl.revoked", buf) != 0) {
354355
fprintf(stderr, "[%d] Failed to copy file to %s\n", i, buf);
355356
goto cleanup;
356357
}
357-
sprintf(buf, "%s/%s", tmpDir, "crl.pem");
358+
(void)XSNPRINTF(buf, sizeof(buf), "%s/%s", tmpDir, "crl.pem");
358359
/* The monitor can be holding the file handle and this will cause
359360
* the remove call to fail. Let's give the monitor a some time to
360361
* finish up. */
@@ -395,9 +396,9 @@ static int test_crl_monitor(void)
395396
cleanup:
396397
if (ret != 0 && i >= 0)
397398
fprintf(stderr, "test_crl_monitor failed on iteration %d\n", i);
398-
sprintf(buf, "%s/%s", tmpDir, "crl.pem");
399+
(void)XSNPRINTF(buf, sizeof(buf), "%s/%s", tmpDir, "crl.pem");
399400
rem_file(buf);
400-
sprintf(buf, "%s/%s", tmpDir, "crl.revoked");
401+
(void)XSNPRINTF(buf, sizeof(buf), "%s/%s", tmpDir, "crl.revoked");
401402
rem_file(buf);
402403
(void)rem_dir(tmpDir);
403404
return ret;

wolfcrypt/src/asn.c

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15071,19 +15071,13 @@ int GetFormattedTime(void* currTime, byte* buf, word32 len)
1507115071
hour = ts->tm_hour;
1507215072
mini = ts->tm_min;
1507315073
sec = ts->tm_sec;
15074-
#if defined(WOLF_C89)
1507515074
if (len < ASN_UTC_TIME_SIZE) {
1507615075
WOLFSSL_MSG("buffer for GetFormattedTime is too short.");
1507715076
return BUFFER_E;
1507815077
}
15079-
ret = XSPRINTF((char*)buf,
15080-
"%02d%02d%02d%02d%02d%02dZ", year, mon, day,
15081-
hour, mini, sec);
15082-
#else
1508315078
ret = XSNPRINTF((char*)buf, len,
1508415079
"%02d%02d%02d%02d%02d%02dZ", year, mon, day,
1508515080
hour, mini, sec);
15086-
#endif
1508715081
}
1508815082
else {
1508915083
/* GeneralizedTime */
@@ -15093,19 +15087,13 @@ int GetFormattedTime(void* currTime, byte* buf, word32 len)
1509315087
hour = ts->tm_hour;
1509415088
mini = ts->tm_min;
1509515089
sec = ts->tm_sec;
15096-
#if defined(WOLF_C89)
1509715090
if (len < ASN_GENERALIZED_TIME_SIZE) {
1509815091
WOLFSSL_MSG("buffer for GetFormattedTime is too short.");
1509915092
return BUFFER_E;
1510015093
}
15101-
ret = XSPRINTF((char*)buf,
15102-
"%4d%02d%02d%02d%02d%02dZ", year, mon, day,
15103-
hour, mini, sec);
15104-
#else
1510515094
ret = XSNPRINTF((char*)buf, len,
1510615095
"%4d%02d%02d%02d%02d%02dZ", year, mon, day,
1510715096
hour, mini, sec);
15108-
#endif
1510915097
}
1511015098

1511115099
return ret;

wolfcrypt/test/test.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49806,11 +49806,7 @@ static wc_test_ret_t pkcs7signed_run_vectors(
4980649806
#endif
4980749807

4980849808
for (j = 0, k = 2; j < (int)sizeof(digest); j++, k += 2) {
49809-
#if defined(WOLF_C89)
49810-
XSPRINTF((char*)&transId[k], "%02x", digest[j]);
49811-
#else
49812-
(void)XSNPRINTF((char*)&transId[k], 3, "%02x", digest[j]);
49813-
#endif
49809+
(void)XSNPRINTF((char*)&transId[k], 3, "%02x", digest[j]);
4981449810
}
4981549811
}
4981649812

wolfssl/test.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1099,10 +1099,11 @@ static WC_INLINE void ShowX509Ex(WOLFSSL_X509* x509, const char* hdr,
10991099
char serialMsg[80];
11001100

11011101
/* testsuite has multiple threads writing to stdout, get output
1102-
message ready to write once */
1103-
strLen = sprintf(serialMsg, " %s", words[3]);
1102+
* message ready to write once */
1103+
strLen = XSNPRINTF(serialMsg, sizeof(serialMsg), " %s", words[3]);
11041104
for (i = 0; i < sz; i++)
1105-
sprintf(serialMsg + strLen + (i*3), ":%02x ", serial[i]);
1105+
strLen = XSNPRINTF(serialMsg + strLen, sizeof(serialMsg) - strLen,
1106+
":%02x ", serial[i]);
11061107
printf("%s\n", serialMsg);
11071108
}
11081109

wolfssl/wolfcrypt/types.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -831,6 +831,8 @@ typedef struct w64wrapper {
831831
#elif defined(WOLF_C89)
832832
#include <stdio.h>
833833
#define XSPRINTF sprintf
834+
/* snprintf not available for C89, so remap using macro */
835+
#define XSNPRINTF(f, len, ...) sprintf(f, ...)
834836
#else
835837
#include <stdio.h>
836838
#define XSNPRINTF snprintf

0 commit comments

Comments
 (0)