Skip to content

Commit

Permalink
replace calls to sprintf() by snprintf() to avoid deprecation compile…
Browse files Browse the repository at this point in the history
…r warnings on MacOS
  • Loading branch information
DDvO committed Sep 24, 2024
1 parent d07bb59 commit 5b03269
Show file tree
Hide file tree
Showing 10 changed files with 22 additions and 21 deletions.
3 changes: 2 additions & 1 deletion apps/passwd.c
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,8 @@ static char *shacrypt(const char *passwd, const char *magic, const char *salt)
OPENSSL_strlcat(out_buf, ascii_dollar, sizeof(out_buf));
if (rounds_custom) {
char tmp_buf[80]; /* "rounds=999999999" */
sprintf(tmp_buf, "rounds=%u", rounds);

snprintf(tmp_buf, sizeof(tmp_buf), "rounds=%u", rounds);
#ifdef CHARSET_EBCDIC
/* In case we're really on a ASCII based platform and just pretend */
if (tmp_buf[0] != 0x72) /* ASCII 'r' */
Expand Down
12 changes: 6 additions & 6 deletions apps/speed.c
Original file line number Diff line number Diff line change
Expand Up @@ -2624,13 +2624,13 @@ int speed_main(int argc, char **argv)
if (doit[D_HMAC]) {
static const char hmac_key[] = "This is a key...";
int len = strlen(hmac_key);
size_t hmac_name_len = sizeof("hmac()") + strlen(evp_mac_mdname);
OSSL_PARAM params[3];

if (evp_mac_mdname == NULL)
goto end;
evp_hmac_name = app_malloc(sizeof("hmac()") + strlen(evp_mac_mdname),
"HMAC name");
sprintf(evp_hmac_name, "hmac(%s)", evp_mac_mdname);
evp_hmac_name = app_malloc(hmac_name_len, "HMAC name");
snprintf(evp_hmac_name, hmac_name_len, "hmac(%s)", evp_mac_mdname);
names[D_HMAC] = evp_hmac_name;

params[0] =
Expand Down Expand Up @@ -2894,6 +2894,7 @@ int speed_main(int argc, char **argv)
}

if (doit[D_EVP_CMAC]) {
size_t len = sizeof("cmac()") + strlen(evp_mac_ciphername);
OSSL_PARAM params[3];
EVP_CIPHER *cipher = NULL;

Expand All @@ -2906,9 +2907,8 @@ int speed_main(int argc, char **argv)
BIO_printf(bio_err, "\nRequested CMAC cipher with unsupported key length.\n");
goto end;
}
evp_cmac_name = app_malloc(sizeof("cmac()")
+ strlen(evp_mac_ciphername), "CMAC name");
sprintf(evp_cmac_name, "cmac(%s)", evp_mac_ciphername);
evp_cmac_name = app_malloc(len, "CMAC name");
snprintf(evp_cmac_name, len, "cmac(%s)", evp_mac_ciphername);
names[D_EVP_CMAC] = evp_cmac_name;

params[0] = OSSL_PARAM_construct_utf8_string(OSSL_ALG_PARAM_CIPHER,
Expand Down
6 changes: 3 additions & 3 deletions crypto/dso/dso_dlfcn.c
Original file line number Diff line number Diff line change
Expand Up @@ -265,11 +265,11 @@ static char *dlfcn_name_converter(DSO *dso, const char *filename)
}
if (transform) {
if ((DSO_flags(dso) & DSO_FLAG_NAME_TRANSLATION_EXT_ONLY) == 0)
sprintf(translated, "lib%s" DSO_EXTENSION, filename);
snprintf(translated, rsize, "lib%s" DSO_EXTENSION, filename);
else
sprintf(translated, "%s" DSO_EXTENSION, filename);
snprintf(translated, rsize, "%s" DSO_EXTENSION, filename);
} else
sprintf(translated, "%s", filename);
snprintf(translated, rsize, "%s", filename);
return translated;
}

Expand Down
2 changes: 1 addition & 1 deletion test/cmactest.c
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ static char *pt(unsigned char *md, unsigned int len)
static char buf[80];

for (i = 0; i < len; i++)
sprintf(&(buf[i * 2]), "%02x", md[i]);
snprintf(&(buf[i * 2]), sizeof(buf), "%02x", md[i]);
return buf;
}

Expand Down
2 changes: 1 addition & 1 deletion test/drbgtest.c
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ static int test_rand_reseed_on_fork(EVP_RAND_CTX *primary,

presult[0].pindex = presult[1].pindex = i;

sprintf(presult[0].name, "child %d", i);
snprintf(presult[0].name, sizeof(presult[0].name), "child %d", i);
strcpy(presult[1].name, presult[0].name);

/* collect the random output of the children */
Expand Down
4 changes: 2 additions & 2 deletions test/enginetest.c
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,9 @@ static int test_engines(void)

TEST_info("About to beef up the engine-type list");
for (loop = 0; loop < NUMTOADD; loop++) {
sprintf(buf, "id%d", loop);
snprintf(buf, sizeof(buf), "id%d", loop);
eid[loop] = OPENSSL_strdup(buf);
sprintf(buf, "Fake engine type %d", loop);
snprintf(buf, sizeof(buf), "Fake engine type %d", loop);
ename[loop] = OPENSSL_strdup(buf);
if (!TEST_ptr(block[loop] = ENGINE_new())
|| !TEST_true(ENGINE_set_id(block[loop], eid[loop]))
Expand Down
2 changes: 1 addition & 1 deletion test/hmactest.c
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ static char *pt(unsigned char *md, unsigned int len)
if (md == NULL)
return NULL;
for (i = 0; i < len; i++)
sprintf(&(buf[i * 2]), "%02x", md[i]);
snprintf(&(buf[i * 2]), sizeof(buf), "%02x", md[i]);
return buf;
}
# endif
Expand Down
4 changes: 2 additions & 2 deletions test/p_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,11 @@ static int p_get_params(void *provctx, OSSL_PARAM params[])
const char *versionp = *(void **)counter_request[0].data;
const char *namep = *(void **)counter_request[1].data;

sprintf(buf, "Hello OpenSSL %.20s, greetings from %s!",
snprintf(buf, sizeof(buf), "Hello OpenSSL %.20s, greetings from %s!",
versionp, namep);
}
} else {
sprintf(buf, "Howdy stranger...");
snprintf(buf, sizeof(buf), "Howdy stranger...");
}

p->return_size = buf_l = strlen(buf) + 1;
Expand Down
6 changes: 3 additions & 3 deletions test/pkcs12_format_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ static int test_single_key(PKCS12_ENC *enc)
char fname[80];
PKCS12_BUILDER *pb;

sprintf(fname, "1key_ciph-%s_iter-%d.p12", OBJ_nid2sn(enc->nid), enc->iter);
snprintf(fname, sizeof(fname), "1key_ciph-%s_iter-%d.p12", OBJ_nid2sn(enc->nid), enc->iter);

pb = new_pkcs12_builder(fname);

Expand Down Expand Up @@ -464,7 +464,7 @@ static int test_single_cert_mac(PKCS12_ENC *mac)
char fname[80];
PKCS12_BUILDER *pb;

sprintf(fname, "1cert_mac-%s_iter-%d.p12", OBJ_nid2sn(mac->nid), mac->iter);
snprintf(fname, sizeof(fname), "1cert_mac-%s_iter-%d.p12", OBJ_nid2sn(mac->nid), mac->iter);

pb = new_pkcs12_builder(fname);

Expand Down Expand Up @@ -624,7 +624,7 @@ static int test_single_secret(PKCS12_ENC *enc)
char fname[80];
PKCS12_BUILDER *pb;

sprintf(fname, "1secret_ciph-%s_iter-%d.p12", OBJ_nid2sn(enc->nid), enc->iter);
snprintf(fname, sizeof(fname), "1secret_ciph-%s_iter-%d.p12", OBJ_nid2sn(enc->nid), enc->iter);
pb = new_pkcs12_builder(fname);
custom_nid = get_custom_oid();

Expand Down
2 changes: 1 addition & 1 deletion test/sslapitest.c
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ static int compare_hex_encoded_buffer(const char *hex_encoded,
return 1;

for (i = j = 0; i < raw_length && j + 1 < hex_length; i++, j += 2) {
sprintf(hexed, "%02x", raw[i]);
snprintf(hexed, sizeof(hexed), "%02x", raw[i]);
if (!TEST_int_eq(hexed[0], hex_encoded[j])
|| !TEST_int_eq(hexed[1], hex_encoded[j + 1]))
return 1;
Expand Down

0 comments on commit 5b03269

Please sign in to comment.