Skip to content

Commit df4c0d8

Browse files
committed
fix RNG leak
1 parent 21c77cd commit df4c0d8

1 file changed

Lines changed: 9 additions & 3 deletions

File tree

test/wh_test_crypto.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ static int whTest_CryptoRng(whClientContext* ctx, int devId, WC_RNG* rng)
142142
if (ret != 0) {
143143
WH_ERROR_PRINT("Failed to wc_InitRng_ex %d\n", ret);
144144
} else {
145+
int freeRet;
145146
ret = wc_RNG_GenerateBlock(rng, lil, sizeof(lil));
146147
if (ret != 0) {
147148
WH_ERROR_PRINT("Failed to wc_RNG_GenerateBlock %d\n", ret);
@@ -162,9 +163,14 @@ static int whTest_CryptoRng(whClientContext* ctx, int devId, WC_RNG* rng)
162163
ret = -1;
163164
}
164165
}
165-
ret = wc_FreeRng(rng);
166-
if (ret != 0) {
167-
WH_ERROR_PRINT("Failed to wc_FreeRng %d\n", ret);
166+
}
167+
/* Always free the RNG if InitRng succeeded, regardless of which (if
168+
* any) GenerateBlock call failed. */
169+
freeRet = wc_FreeRng(rng);
170+
if (freeRet != 0) {
171+
WH_ERROR_PRINT("Failed to wc_FreeRng %d\n", freeRet);
172+
if (ret == 0) {
173+
ret = freeRet;
168174
}
169175
}
170176
}

0 commit comments

Comments
 (0)