Skip to content

Commit eceae93

Browse files
committed
Fix pointer cleanup on dynamic allocation failure
When using a custom alloc/free implementation, allocation may fail, in which case the cleanup section needs to zeroize and free those pointers there were successfully allocated, and skip those for which allocation failed. This skipping of failed allocations is not implemented correctly and would lead to a null pointer dereference. This commit fixes this. Signed-off-by: Hanno Becker <beckphan@amazon.co.uk>
1 parent dab806c commit eceae93

1 file changed

Lines changed: 9 additions & 6 deletions

File tree

mlkem/src/common.h

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -187,12 +187,15 @@
187187
/* Custom allocation */
188188

189189
#define MLK_ALLOC(v, T, N) MLK_CUSTOM_ALLOC(v, T, N)
190-
#define MLK_FREE(v, T, N) \
191-
do \
192-
{ \
193-
mlk_zeroize(v, sizeof(T) * (N)); \
194-
MLK_CUSTOM_FREE(v, T, N); \
195-
v = NULL; \
190+
#define MLK_FREE(v, T, N) \
191+
do \
192+
{ \
193+
if (v != NULL) \
194+
{ \
195+
mlk_zeroize(v, sizeof(T) * (N)); \
196+
MLK_CUSTOM_FREE(v, T, N); \
197+
v = NULL; \
198+
} \
196199
} while (0)
197200

198201
#endif /* MLK_CONFIG_CUSTOM_ALLOC_FREE */

0 commit comments

Comments
 (0)