Skip to content

Commit 01808be

Browse files
Merge pull request wolfSSL#8474 from philljj/coverity_feb_2025
coverity: fix test_dtls warnings.
2 parents 619a41f + 6f1c31a commit 01808be

File tree

1 file changed

+24
-12
lines changed

1 file changed

+24
-12
lines changed

tests/api/test_dtls.c

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ int test_dtls12_basic_connection_id(void)
4141
unsigned char client_cid[] = { 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 };
4242
unsigned char server_cid[] = { 0, 1, 2, 3, 4, 5 };
4343
unsigned char readBuf[40];
44+
void * cid = NULL;
4445
const char* params[] = {
4546
#ifndef NO_RSA
4647
#ifndef NO_SHA256
@@ -88,6 +89,10 @@ int test_dtls12_basic_connection_id(void)
8889
client_cid, sizeof(client_cid))
8990
#define SERVER_CID() mymemmem(test_ctx.c_buff, test_ctx.c_len, \
9091
server_cid, sizeof(server_cid))
92+
#define RESET_CID(cid) if ((cid) != NULL) { \
93+
((char*)(cid))[0] = -1; \
94+
}
95+
9196

9297
printf("\n");
9398
for (i = 0; i < XELEM_CNT(params) && EXPECT_SUCCESS(); i++) {
@@ -227,15 +232,15 @@ int test_dtls12_basic_connection_id(void)
227232
wolfSSL_SetLoggingPrefix("client");
228233
ExpectIntEQ(wolfSSL_write(ssl_c, params[i],
229234
(int)XSTRLEN(params[i])), XSTRLEN(params[i]));
230-
ExpectNotNull(CLIENT_CID());
231-
/* Use Expect so we don't access CLIENT_CID() if it is NULL */
232-
ExpectTrue(((char*)CLIENT_CID())[0] = -1);
235+
/* Reset client cid. */
236+
ExpectNotNull(cid = CLIENT_CID());
237+
RESET_CID(cid);
233238
wolfSSL_SetLoggingPrefix("server");
234239
ExpectIntEQ(wolfSSL_write(ssl_s, params[i],
235240
(int)XSTRLEN(params[i])), XSTRLEN(params[i]));
236-
ExpectNotNull(SERVER_CID());
237-
/* Use Expect so we don't access SERVER_CID() if it is NULL */
238-
ExpectTrue(((char*)SERVER_CID())[0] = -1);
241+
/* Reset server cid. */
242+
ExpectNotNull(cid = SERVER_CID());
243+
RESET_CID(cid);
239244
/* Try to read the data but it shouldn't be there */
240245
wolfSSL_SetLoggingPrefix("client");
241246
ExpectIntEQ(wolfSSL_read(ssl_c, readBuf, sizeof(readBuf)), -1);
@@ -394,6 +399,7 @@ int test_dtls12_basic_connection_id(void)
394399

395400
#undef CLIENT_CID
396401
#undef SERVER_CID
402+
#undef RESET_CID
397403
#endif
398404
return EXPECT_RESULT();
399405
}
@@ -406,6 +412,7 @@ int test_dtls13_basic_connection_id(void)
406412
unsigned char client_cid[] = { 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 };
407413
unsigned char server_cid[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
408414
unsigned char readBuf[50];
415+
void * cid = NULL;
409416
const char* params[] = {
410417
#ifndef NO_SHA256
411418
#ifdef WOLFSSL_AES_128
@@ -432,6 +439,10 @@ int test_dtls13_basic_connection_id(void)
432439
client_cid, sizeof(client_cid))
433440
#define SERVER_CID() mymemmem(test_ctx.c_buff, test_ctx.c_len, \
434441
server_cid, sizeof(server_cid))
442+
#define RESET_CID(cid) if ((cid) != NULL) { \
443+
((char*)(cid))[0] = -1; \
444+
}
445+
435446

436447
printf("\n");
437448
for (i = 0; i < XELEM_CNT(params) && EXPECT_SUCCESS(); i++) {
@@ -512,14 +523,14 @@ int test_dtls13_basic_connection_id(void)
512523
/* Write some data but with wrong CID */
513524
ExpectIntEQ(wolfSSL_write(ssl_c, params[i], (int)XSTRLEN(params[i])),
514525
XSTRLEN(params[i]));
515-
ExpectNotNull(CLIENT_CID());
516-
/* Use Expect so we don't access CLIENT_CID() if it is NULL */
517-
ExpectTrue(((char*)CLIENT_CID())[0] = -1);
526+
/* Reset client cid. */
527+
ExpectNotNull(cid = CLIENT_CID());
528+
RESET_CID(cid);
518529
ExpectIntEQ(wolfSSL_write(ssl_s, params[i], (int)XSTRLEN(params[i])),
519530
XSTRLEN(params[i]));
520-
ExpectNotNull(SERVER_CID());
521-
/* Use Expect so we don't access SERVER_CID() if it is NULL */
522-
ExpectTrue(((char*)SERVER_CID())[0] = -1);
531+
/* Reset server cid. */
532+
ExpectNotNull(cid = SERVER_CID());
533+
RESET_CID(cid);
523534
/* Try to read the data but it shouldn't be there */
524535
ExpectIntEQ(wolfSSL_read(ssl_c, readBuf, sizeof(readBuf)), -1);
525536
ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
@@ -547,6 +558,7 @@ int test_dtls13_basic_connection_id(void)
547558

548559
#undef CLIENT_CID
549560
#undef SERVER_CID
561+
#undef RESET_CID
550562

551563
#endif
552564
return EXPECT_RESULT();

0 commit comments

Comments
 (0)