@@ -118,7 +118,9 @@ int test_wc_LmsKey_sign_verify(void)
118118 int i ;
119119 int numSigs = 5 ;
120120
121+ /* Zero so cleanup is safe if an early alloc failure skips init. */
121122 XMEMSET (& key , 0 , sizeof (key ));
123+ XMEMSET (& rng , 0 , sizeof (rng ));
122124
123125 ExpectIntEQ (wc_InitRng (& rng ), 0 );
124126
@@ -174,8 +176,10 @@ int test_wc_LmsKey_reload_cache(void)
174176 /* Sign 33 times to advance q past the 32-entry cache window. */
175177 int preSigs = 33 ;
176178
179+ /* Zero so cleanup is safe if an early alloc failure skips init. */
177180 XMEMSET (& key , 0 , sizeof (key ));
178181 XMEMSET (& vkey , 0 , sizeof (vkey ));
182+ XMEMSET (& rng , 0 , sizeof (rng ));
179183
180184 ExpectIntEQ (wc_InitRng (& rng ), 0 );
181185
@@ -861,8 +865,9 @@ static int rfc9802_gen_chain(void* caKey, int caKeyType, int caSigType,
861865 ExpectNotNull (caDer = (byte * )XMALLOC (derCap , NULL , DYNAMIC_TYPE_TMP_BUFFER ));
862866 ExpectNotNull (leafDer = (byte * )XMALLOC (derCap , NULL ,
863867 DYNAMIC_TYPE_TMP_BUFFER ));
864- ExpectIntEQ (wc_ecc_init (& leafKey ), 0 );
865- leafKeyInit = 1 ;
868+ if (wc_ecc_init (& leafKey ) == 0 ) /* only flag for free if init succeeded */
869+ leafKeyInit = 1 ;
870+ ExpectIntEQ (leafKeyInit , 1 );
866871 ExpectIntEQ (wc_ecc_make_key (rng , 32 , & leafKey ), 0 );
867872
868873 /* Self-signed CA root. */
@@ -972,6 +977,10 @@ int test_rfc9802_lms_x509_gen(void)
972977 LmsKey key ;
973978 WC_RNG rng ;
974979
980+ /* Zero so cleanup is safe if an early alloc failure skips init. */
981+ XMEMSET (& key , 0 , sizeof (key ));
982+ XMEMSET (& rng , 0 , sizeof (rng ));
983+
975984 ExpectIntEQ (wc_InitRng (& rng ), 0 );
976985
977986 /* Single-level LMS (L1-H5-W8). */
@@ -1177,6 +1186,10 @@ int test_rfc9802_xmss_x509_gen(void)
11771186 XmssKey key ;
11781187 WC_RNG rng ;
11791188
1189+ /* Zero so cleanup is safe if an early alloc failure skips init. */
1190+ XMEMSET (& key , 0 , sizeof (key ));
1191+ XMEMSET (& rng , 0 , sizeof (rng ));
1192+
11801193 ExpectIntEQ (wc_InitRng (& rng ), 0 );
11811194
11821195 /* Single-tree XMSS. */
0 commit comments