Skip to content

Commit 44b18de

Browse files
committed
fixes for cppcheck-2.13.0 --force:
* fix null pointer derefs in wc_InitRsaKey_Id() and wc_InitRsaKey_Label() (nullPointerRedundantCheck). * fix use of wrong printf variant in rsip_vprintf() (wrongPrintfScanfArgNum). * fix wrong printf format in bench_xmss_sign_verify() (invalidPrintfArgType_sint). * add missing WOLFSSL_XFREE_NO_NULLNESS_CHECK variants of XFREE() (WOLFSSL_LINUXKM, FREESCALE_MQX, FREESCALE_KSDK_MQX). * suppress false-positive uninitvar on "limit" in CheckTLS13AEADSendLimit(). * suppress true-but-benign-positive autoVariables in DoClientHello(). * in wolfcrypt/src/ecc.c, refactor ECC_KEY_MAX_BITS() as a local function to resolve true-but-benign-positive identicalInnerCondition. * refactor flow in wc_ecc_sign_hash_ex() to resolve true-but-benign-positive identicalInnerCondition.
1 parent 457188f commit 44b18de

7 files changed

Lines changed: 53 additions & 25 deletions

File tree

linuxkm/linuxkm_wc_port.h

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -636,11 +636,19 @@
636636
#ifdef WOLFSSL_TRACK_MEMORY
637637
#include <wolfssl/wolfcrypt/memory.h>
638638
#define XMALLOC(s, h, t) ({(void)(h); (void)(t); wolfSSL_Malloc(s);})
639-
#define XFREE(p, h, t) ({void* _xp; (void)(h); _xp = (p); if(_xp) wolfSSL_Free(_xp);})
639+
#ifdef WOLFSSL_XFREE_NO_NULLNESS_CHECK
640+
#define XFREE(p, h, t) ({(void)(h); (void)(t); wolfSSL_Free(p);})
641+
#else
642+
#define XFREE(p, h, t) ({void* _xp; (void)(h); _xp = (p); if(_xp) wolfSSL_Free(_xp);})
643+
#endif
640644
#define XREALLOC(p, n, h, t) ({(void)(h); (void)(t); wolfSSL_Realloc(p, n);})
641645
#else
642646
#define XMALLOC(s, h, t) ({(void)(h); (void)(t); malloc(s);})
643-
#define XFREE(p, h, t) ({void* _xp; (void)(h); _xp = (p); if(_xp) free(_xp);})
647+
#ifdef WOLFSSL_XFREE_NO_NULLNESS_CHECK
648+
#define XFREE(p, h, t) ({(void)(h); (void)(t); free(p);})
649+
#else
650+
#define XFREE(p, h, t) ({void* _xp; (void)(h); (void)(t); _xp = (p); if(_xp) free(_xp);})
651+
#endif
644652
#define XREALLOC(p, n, h, t) ({(void)(h); (void)(t); realloc(p, n);})
645653
#endif
646654

src/internal.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23944,8 +23944,11 @@ static int CheckTLS13AEADSendLimit(WOLFSSL* ssl)
2394423944
ssl->keys.sequence_number_lo);
2394523945
}
2394623946

23947-
if (w64GTE(seq, limit))
23947+
if (w64GTE(seq, limit)) { /* cppcheck-suppress uninitvar
23948+
* (false positive from cppcheck-2.13.0)
23949+
*/
2394823950
return Tls13UpdateKeys(ssl); /* Need to generate new keys */
23951+
}
2394923952

2395023953
return 0;
2395123954
}
@@ -35828,7 +35831,8 @@ static int DoSessionTicket(WOLFSSL* ssl, const byte* input, word32* inOutIdx,
3582835831
#endif
3582935832

3583035833
#ifdef OPENSSL_EXTRA
35831-
ssl->clSuites = clSuites;
35834+
ssl->clSuites = clSuites; /* cppcheck-suppress autoVariables
35835+
*/
3583235836
/* Give user last chance to provide a cert for cipher selection */
3583335837
if (ret == 0 && ssl->ctx->certSetupCb != NULL)
3583435838
ret = CertSetupCbWrapper(ssl);

wolfcrypt/benchmark/benchmark.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9641,7 +9641,7 @@ static void bench_xmss_sign_verify(const char * params)
96419641

96429642
ret = wc_XmssKey_GetPubLen(&key, &pkSz);
96439643
if (pkSz != XMSS_SHA256_PUBLEN) {
9644-
fprintf(stderr, "error: xmss pub len: got %d, expected %d\n", pkSz,
9644+
fprintf(stderr, "error: xmss pub len: got %u, expected %d\n", pkSz,
96459645
XMSS_SHA256_PUBLEN);
96469646
goto exit_xmss_sign_verify;
96479647
}

wolfcrypt/src/ecc.c

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -251,17 +251,20 @@ ECC Curve Sizes:
251251
#else
252252
#define MAX_ECC_BITS_USE MAX_ECC_BITS_NEEDED
253253
#endif
254-
#if !defined(WOLFSSL_CUSTOM_CURVES) && (ECC_MIN_KEY_SZ > 160) && \
255-
(!defined(HAVE_ECC_KOBLITZ) || (ECC_MIN_KEY_SZ > 224))
256-
#define ECC_KEY_MAX_BITS(key) \
257-
((((key) == NULL) || ((key)->dp == NULL)) ? MAX_ECC_BITS_USE : \
258-
((unsigned)((key)->dp->size * 8)))
259-
#else
260-
/* Add one bit for cases when order is a bit greater than prime. */
261-
#define ECC_KEY_MAX_BITS(key) \
262-
((((key) == NULL) || ((key)->dp == NULL)) ? MAX_ECC_BITS_USE : \
263-
((unsigned)((key)->dp->size * 8 + 1)))
264-
#endif
254+
255+
static WC_MAYBE_UNUSED WC_INLINE word32 ECC_KEY_MAX_BITS(const ecc_key *key) {
256+
if (((key) == NULL) || ((key)->dp == NULL))
257+
return MAX_ECC_BITS_USE;
258+
else {
259+
#if !defined(WOLFSSL_CUSTOM_CURVES) && (ECC_MIN_KEY_SZ > 160) && \
260+
(!defined(HAVE_ECC_KOBLITZ) || (ECC_MIN_KEY_SZ > 224))
261+
return (word32)((key)->dp->size * 8);
262+
#else
263+
/* Add one bit for cases when order is a bit greater than prime. */
264+
return (word32)((key)->dp->size * 8 + 1);
265+
#endif
266+
}
267+
}
265268

266269
/* forward declarations */
267270
static int wc_ecc_new_point_ex(ecc_point** point, void* heap);
@@ -7263,10 +7266,10 @@ int wc_ecc_sign_hash_ex(const byte* in, word32 inlen, WC_RNG* rng,
72637266
pubkey = (ecc_key*)XMALLOC(sizeof(ecc_key), key->heap, DYNAMIC_TYPE_ECC);
72647267
if (pubkey == NULL)
72657268
err = MEMORY_E;
7269+
else
72667270
#endif
7267-
7271+
{
72687272
/* don't use async for key, since we don't support async return here */
7269-
if (err == MP_OKAY) {
72707273
err = wc_ecc_init_ex(pubkey, key->heap, INVALID_DEVID);
72717274
if (err == MP_OKAY) {
72727275
err = ecc_sign_hash_sw(key, pubkey, rng, curve, e, r, s);

wolfcrypt/src/rsa.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -267,8 +267,10 @@ int wc_InitRsaKey_Id(RsaKey* key, unsigned char* id, int len, void* heap,
267267
ret = BUFFER_E;
268268

269269
#if defined(HAVE_PKCS11)
270-
XMEMSET(key, 0, sizeof(RsaKey));
271-
key->isPkcs11 = 1;
270+
if (ret == 0) {
271+
XMEMSET(key, 0, sizeof(RsaKey));
272+
key->isPkcs11 = 1;
273+
}
272274
#endif
273275

274276
if (ret == 0)
@@ -302,8 +304,10 @@ int wc_InitRsaKey_Label(RsaKey* key, const char* label, void* heap, int devId)
302304
}
303305

304306
#if defined(HAVE_PKCS11)
305-
XMEMSET(key, 0, sizeof(RsaKey));
306-
key->isPkcs11 = 1;
307+
if (ret == 0) {
308+
XMEMSET(key, 0, sizeof(RsaKey));
309+
key->isPkcs11 = 1;
310+
}
307311
#endif
308312

309313
if (ret == 0)

wolfcrypt/test/test.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ const byte const_byte_array[] = "A+Gd\0\0\0";
212212
int ret;
213213
char tmpBuf[80];
214214

215-
ret = XSNPRINTF(tmpBuf, sizeof(tmpBuf), format, args);
215+
ret = vsnprintf(tmpBuf, sizeof(tmpBuf), format, args);
216216
printf(tmpBuf);
217217

218218
return ret;

wolfssl/wolfcrypt/settings.h

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1193,7 +1193,12 @@ extern void uITRON4_free(void *p) ;
11931193
#if !defined(XMALLOC_OVERRIDE) && !defined(XMALLOC_USER)
11941194
#define XMALLOC_OVERRIDE
11951195
#define XMALLOC(s, h, t) ((void)(h), (void)(t), (void *)_mem_alloc_system((s)))
1196-
#define XFREE(p, h, t) {void* xp = (p); (void)(h); (void)(t); if ((xp)) _mem_free((xp));}
1196+
#ifdef WOLFSSL_XFREE_NO_NULLNESS_CHECK
1197+
#define XFREE(p, h, t) {(void)(h); (void)(t); _mem_free(p);}
1198+
#else
1199+
#define XFREE(p, h, t) {void* xp = (p); (void)(h); (void)(t); if ((xp)) _mem_free((xp));}
1200+
#endif
1201+
11971202
/* Note: MQX has no realloc, using fastmath above */
11981203
#endif
11991204
#ifdef USE_FAST_MATH
@@ -1224,7 +1229,11 @@ extern void uITRON4_free(void *p) ;
12241229
#endif
12251230

12261231
#define XMALLOC(s, h, t) ((void)(h), (void)(t), (void *)_mem_alloc_system((s)))
1227-
#define XFREE(p, h, t) {void* xp = (p); (void)(h); (void)(t); if ((xp)) _mem_free((xp));}
1232+
#ifdef WOLFSSL_XFREE_NO_NULLNESS_CHECK
1233+
#define XFREE(p, h, t) {(void)(h); (void)(t); _mem_free(p);}
1234+
#else
1235+
#define XFREE(p, h, t) {void* xp = (p); (void)(h); (void)(t); if ((xp)) _mem_free((xp));}
1236+
#endif
12281237
#define XREALLOC(p, n, h, t) _mem_realloc((p), (n)) /* since MQX 4.1.2 */
12291238

12301239
#define MQX_FILE_PTR FILE *

0 commit comments

Comments
 (0)