Skip to content

Commit fd3a9d0

Browse files
committed
Removed fprintf and snprintf
1 parent ad6d34a commit fd3a9d0

File tree

11 files changed

+232
-100
lines changed

11 files changed

+232
-100
lines changed

benchmark/wh_bench_utils.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
#else
2929
#include <stdio.h> /* use default printf */
3030
#define WH_BENCH_PRINTF printf
31-
#define WH_BENCH_SNPRINTF snprintf
31+
#define WH_BENCH_SNPRINTF WOLFHSM_SNPRINTF
3232
#endif
3333

3434
#endif /* WH_BENCH_UTILS_H_ */

examples/posix/wh_posix_client/wh_posix_client.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ static int wh_ClientTask(void* cf, const char* type, int test)
103103
}
104104

105105
for (counter = 0; counter < REPEAT_COUNT; counter++) {
106-
sprintf((char*)tx_req, "Request:%u", counter);
106+
WOLFHSM_SNPRINTF((char*)tx_req, sizeof(tx_req), "Request:%u", counter);
107107
tx_req_len = strlen((char*)tx_req);
108108
do {
109109
ret = wh_Client_EchoRequest(client, tx_req_len, tx_req);

examples/posix/wh_posix_server/wh_posix_server_cfg.c

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,17 @@
1818
#include "port/posix/posix_transport_shm.h"
1919
#include "port/posix/posix_transport_tcp.h"
2020

21+
/* Safe bounded string copy with guaranteed NUL-termination */
22+
#define wh_strncpyz(dst, src, n) do { \
23+
if ((n) > 0) { \
24+
size_t i; \
25+
for (i = 0; i < (n) - 1 && (src)[i] != '\0'; i++) { \
26+
(dst)[i] = (src)[i]; \
27+
} \
28+
(dst)[i] = '\0'; \
29+
} \
30+
} while (0)
31+
2132
posixTransportShmConfig shmConfig;
2233
posixTransportTcpConfig tcpConfig;
2334

@@ -339,7 +350,7 @@ static void parseNvmInitFile(const char* filePath)
339350
fclose(file);
340351
exit(EXIT_FAILURE);
341352
}
342-
snprintf(label, sizeof(label), "%s", token);
353+
wh_strncpyz(label, token, sizeof(label));
343354

344355
/* Parse the file path */
345356
token = strtok(NULL, " ");
@@ -414,7 +425,7 @@ static void processEntry(Entry* entry, int isKey, whNvmContext* nvmContext)
414425
meta.access = entry->access;
415426
meta.flags = entry->flags;
416427
meta.len = fileSize;
417-
snprintf((char*)meta.label, WH_NVM_LABEL_LEN, "%s", entry->label);
428+
wh_strncpyz((char*)meta.label, entry->label, WH_NVM_LABEL_LEN);
418429

419430
int rc = wh_Nvm_AddObject(nvmContext, &meta, fileSize, buffer);
420431
if (rc != 0) {

test/wh_test_clientserver.c

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -914,8 +914,8 @@ int whTest_ClientServerSequential(whTestNvmBackendType nvmType)
914914

915915
/* Prepare echo test */
916916
send_len =
917-
snprintf(send_buffer, sizeof(send_buffer), "Request:%u", counter);
918-
snprintf(recv_buffer, sizeof(recv_buffer), "NOTHING RECEIVED");
917+
WOLFHSM_SNPRINTF(send_buffer, sizeof(send_buffer), "Request:%u", counter);
918+
wh_strncpyz(recv_buffer, "NOTHING RECEIVED", sizeof(recv_buffer));
919919
recv_len = 0;
920920

921921
WH_TEST_RETURN_ON_FAIL(
@@ -993,8 +993,8 @@ int whTest_ClientServerSequential(whTestNvmBackendType nvmType)
993993

994994
whNvmId lastAvailObjects = 0;
995995

996-
label_len = snprintf(label, sizeof(label), "Label:%d", id);
997-
len = snprintf(send_buffer, sizeof(send_buffer), "Data:%d Counter:%d",
996+
label_len = WOLFHSM_SNPRINTF(label, sizeof(label), "Label:%d", id);
997+
len = WOLFHSM_SNPRINTF(send_buffer, sizeof(send_buffer), "Data:%d Counter:%d",
998998
id, counter);
999999

10001000
#if defined(WOLFHSM_CFG_DEBUG_VERBOSE)
@@ -1146,8 +1146,8 @@ int whTest_ClientServerSequential(whTestNvmBackendType nvmType)
11461146

11471147
whNvmId lastAvailObjects = 0;
11481148

1149-
snprintf((char*)(meta.label), sizeof(meta.label), "Label:%d", meta.id);
1150-
len = snprintf(send_buffer, sizeof(send_buffer), "Data:%d Counter:%d",
1149+
WOLFHSM_SNPRINTF((char*)(meta.label), sizeof(meta.label), "Label:%d", meta.id);
1150+
len = WOLFHSM_SNPRINTF(send_buffer, sizeof(send_buffer), "Data:%d Counter:%d",
11511151
meta.id, counter);
11521152

11531153
#if defined(WOLFHSM_CFG_DEBUG_VERBOSE)
@@ -1351,8 +1351,8 @@ int whTest_ClientServerClientConfig(whClientConfig* clientCfg)
13511351

13521352
/* Prepare echo test */
13531353
send_len =
1354-
snprintf(send_buffer, sizeof(send_buffer), "Request:%u", counter);
1355-
snprintf(recv_buffer, sizeof(recv_buffer), "NOTHING RECEIVED");
1354+
WOLFHSM_SNPRINTF(send_buffer, sizeof(send_buffer), "Request:%u", counter);
1355+
wh_strncpyz(recv_buffer, "NOTHING RECEIVED", sizeof(recv_buffer));
13561356
recv_len = 0;
13571357

13581358
WH_TEST_RETURN_ON_FAIL(ret = wh_Client_Echo(client, send_len, send_buffer, &recv_len, recv_buffer));
@@ -1401,8 +1401,8 @@ int whTest_ClientServerClientConfig(whClientConfig* clientCfg)
14011401

14021402
whNvmId lastAvailObjects = 0;
14031403

1404-
label_len = snprintf(label, sizeof(label), "Label:%d", id);
1405-
len = snprintf(send_buffer, sizeof(send_buffer), "Data:%d Counter:%d",
1404+
label_len = WOLFHSM_SNPRINTF(label, sizeof(label), "Label:%d", id);
1405+
len = WOLFHSM_SNPRINTF(send_buffer, sizeof(send_buffer), "Data:%d Counter:%d",
14061406
id, counter);
14071407

14081408
lastAvailObjects = avail_objects;
@@ -1532,8 +1532,8 @@ int whTest_ClientServerClientConfig(whClientConfig* clientCfg)
15321532

15331533
whNvmId lastAvailObjects = 0;
15341534

1535-
snprintf((char*)(meta.label), sizeof(meta.label), "Label:%d", meta.id);
1536-
len = snprintf(send_buffer, sizeof(send_buffer), "Data:%d Counter:%d",
1535+
WOLFHSM_SNPRINTF((char*)(meta.label), sizeof(meta.label), "Label:%d", meta.id);
1536+
len = WOLFHSM_SNPRINTF(send_buffer, sizeof(send_buffer), "Data:%d Counter:%d",
15371537
meta.id, counter);
15381538

15391539
lastAvailObjects = avail_objects;

test/wh_test_comm.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ int whTest_CommMem(void)
131131
&rx_req_len, rx_req));
132132

133133
for (counter = 0; counter < REPEAT_COUNT; counter++) {
134-
snprintf((char*)tx_req, sizeof(tx_req), "Request:%u", counter);
134+
WOLFHSM_SNPRINTF((char*)tx_req, sizeof(tx_req), "Request:%u", counter);
135135
tx_req_len = strlen((char*)tx_req);
136136
tx_req_type = counter * 2;
137137
WH_TEST_RETURN_ON_FAIL(
@@ -159,7 +159,7 @@ int whTest_CommMem(void)
159159
WH_TEST_DEBUG_PRINT("Server RecvRequest:%d, flags %x, type:%x, seq:%d, len:%d, %s\n",
160160
ret, rx_req_flags, rx_req_type, rx_req_seq, rx_req_len, rx_req);
161161

162-
snprintf((char*)tx_resp, sizeof(tx_resp), "Response:%s", rx_req);
162+
WOLFHSM_SNPRINTF((char*)tx_resp, sizeof(tx_resp), "Response:%s", rx_req);
163163
tx_resp_len = strlen((char*)tx_resp);
164164
ret = wh_CommServer_SendResponse(server, rx_req_flags, rx_req_type,
165165
rx_req_seq, tx_resp_len, tx_resp);
@@ -218,7 +218,7 @@ static void* _whCommClientTask(void* cf)
218218
WH_TEST_ASSERT_MSG(0 == ret, "Client Init: ret=%d", ret);
219219

220220
for (counter = 0; counter < REPEAT_COUNT; counter++) {
221-
snprintf((char*)tx_req, sizeof(tx_req), "Request:%u", counter);
221+
WOLFHSM_SNPRINTF((char*)tx_req, sizeof(tx_req), "Request:%u", counter);
222222
tx_req_len = strlen((char*)tx_req);
223223
tx_req_type = counter * 2;
224224
do {
@@ -306,7 +306,7 @@ static void* _whCommServerTask(void* cf)
306306
}
307307

308308
do {
309-
snprintf((char*)tx_resp, sizeof(tx_resp), "Response:%s", rx_req);
309+
WOLFHSM_SNPRINTF((char*)tx_resp, sizeof(tx_resp), "Response:%s", rx_req);
310310
tx_resp_len = strlen((char*)tx_resp);
311311
ret = wh_CommServer_SendResponse(server, rx_req_flags, rx_req_type,
312312
rx_req_seq, tx_resp_len, tx_resp);
@@ -413,7 +413,7 @@ void wh_CommClientServer_ShMemThreadTest(void)
413413

414414
/* Make unique name for this test */
415415
char uniq_name[32] = {0};
416-
snprintf(uniq_name, sizeof(uniq_name),"/wh_test_comm_shm.%u",
416+
WOLFHSM_SNPRINTF(uniq_name, sizeof(uniq_name),"/wh_test_comm_shm.%u",
417417
(unsigned) getpid());
418418
tmcf->name = uniq_name;
419419

test/wh_test_common.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,18 @@
6666
#define WH_ERROR_PRINT2(fmt, ...) \
6767
WOLFHSM_CFG_PRINTF("ERROR [%s:%d]: " fmt, __func__, __LINE__, ##__VA_ARGS__)
6868
#endif
69+
70+
/* Safe bounded string copy with guaranteed NUL-termination */
71+
#define wh_strncpyz(dst, src, n) do { \
72+
if ((n) > 0) { \
73+
size_t i; \
74+
for (i = 0; i < (n) - 1 && (src)[i] != '\0'; i++) { \
75+
(dst)[i] = (src)[i]; \
76+
} \
77+
(dst)[i] = '\0'; \
78+
} \
79+
} while (0)
80+
6981
/*
7082
* Helper macro for test error propagation
7183
* Evaluates the "call" argument, and if not equal to zero, displays the

test/wh_test_server_img_mgr.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ static int whTest_ServerImgMgrServerCfgEcc256(whServerConfig* serverCfg)
315315
sigMeta.access = WH_NVM_ACCESS_ANY;
316316
sigMeta.flags = WH_NVM_FLAGS_NONE;
317317
sigMeta.len = sigLen;
318-
snprintf((char*)sigMeta.label, WH_NVM_LABEL_LEN, "TestSig");
318+
wh_strncpyz((char*)sigMeta.label, "TestSig", WH_NVM_LABEL_LEN);
319319

320320
ret = wh_Nvm_AddObject(serverCfg->nvm, &sigMeta, sigLen, signature);
321321
if (ret != WH_ERROR_OK) {
@@ -365,7 +365,7 @@ static int whTest_ServerImgMgrServerCfgEcc256(whServerConfig* serverCfg)
365365
keyMeta.access = WH_NVM_ACCESS_ANY;
366366
keyMeta.flags = WH_NVM_FLAGS_NONE;
367367
keyMeta.len = pubKeyDerLen;
368-
snprintf((char*)keyMeta.label, WH_NVM_LABEL_LEN, "TestKey");
368+
wh_strncpyz((char*)keyMeta.label, "TestKey", WH_NVM_LABEL_LEN);
369369

370370
ret = wh_Server_KeystoreCacheKey(server, &keyMeta, pubKeyDer);
371371
if (ret != WH_ERROR_OK) {
@@ -616,7 +616,7 @@ static int whTest_ServerImgMgrServerCfgAes128Cmac(whServerConfig* serverCfg)
616616
sigMeta.access = WH_NVM_ACCESS_ANY;
617617
sigMeta.flags = WH_NVM_FLAGS_NONE;
618618
sigMeta.len = cmac_size;
619-
snprintf((char*)sigMeta.label, WH_NVM_LABEL_LEN, "TestCmacSig");
619+
wh_strncpyz((char*)sigMeta.label, "TestCmacSig", WH_NVM_LABEL_LEN);
620620

621621
ret = wh_Nvm_AddObject(serverCfg->nvm, &sigMeta, cmac_size, computed_cmac);
622622
if (ret != WH_ERROR_OK) {
@@ -657,7 +657,7 @@ static int whTest_ServerImgMgrServerCfgAes128Cmac(whServerConfig* serverCfg)
657657
keyMeta.access = WH_NVM_ACCESS_ANY;
658658
keyMeta.flags = WH_NVM_FLAGS_NONE;
659659
keyMeta.len = sizeof(testAes128Key);
660-
snprintf((char*)keyMeta.label, WH_NVM_LABEL_LEN, "TestAes128Key");
660+
wh_strncpyz((char*)keyMeta.label, "TestAes128Key", WH_NVM_LABEL_LEN);
661661

662662
ret = wh_Server_KeystoreCacheKey(server, &keyMeta, (uint8_t*)testAes128Key);
663663
if (ret != WH_ERROR_OK) {
@@ -934,7 +934,7 @@ static int whTest_ServerImgMgrServerCfgRsa2048(whServerConfig* serverCfg)
934934
sigMeta.access = WH_NVM_ACCESS_ANY;
935935
sigMeta.flags = WH_NVM_FLAGS_NONE;
936936
sigMeta.len = sigLen;
937-
snprintf((char*)sigMeta.label, WH_NVM_LABEL_LEN, "TestRsaSig");
937+
wh_strncpyz((char*)sigMeta.label, "TestRsaSig", WH_NVM_LABEL_LEN);
938938

939939
ret = wh_Nvm_AddObject(serverCfg->nvm, &sigMeta, sigLen, signature);
940940
if (ret != WH_ERROR_OK) {
@@ -984,7 +984,7 @@ static int whTest_ServerImgMgrServerCfgRsa2048(whServerConfig* serverCfg)
984984
keyMeta.access = WH_NVM_ACCESS_ANY;
985985
keyMeta.flags = WH_NVM_FLAGS_NONE;
986986
keyMeta.len = pubKeyDerLen;
987-
snprintf((char*)keyMeta.label, WH_NVM_LABEL_LEN, "TestRsaKey");
987+
wh_strncpyz((char*)keyMeta.label, "TestRsaKey", WH_NVM_LABEL_LEN);
988988

989989
ret = wh_Server_KeystoreCacheKey(server, &keyMeta, pubKeyDer);
990990
if (ret != WH_ERROR_OK) {

0 commit comments

Comments
 (0)