Skip to content

Commit d239948

Browse files
committed
Generic Memory Pools
1. Added some extra parameter checking to wc_LoadStaticMemory_ex(). 2. Added some extra parameter checking to wc_StaticBufferSz_ex(). 3. Rename some parameters and add some logging prints. 4. Some static functions have some parameter checking and they are only calling in one spot, remove it.
1 parent 0b5c83f commit d239948

1 file changed

Lines changed: 17 additions & 18 deletions

File tree

wolfcrypt/src/memory.c

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -573,10 +573,6 @@ static int wc_partition_static_memory(byte* buffer, word32 sz, int flag,
573573

574574
WOLFSSL_ENTER("wc_partition_static_memory");
575575

576-
if (buffer == NULL) {
577-
return BAD_FUNC_ARG;
578-
}
579-
580576
/* align pt */
581577
while ((wc_ptr_t)pt % WOLFSSL_STATIC_ALIGN && pt < (buffer + sz)) {
582578
*pt = 0x00;
@@ -593,7 +589,7 @@ static int wc_partition_static_memory(byte* buffer, word32 sz, int flag,
593589
/* creating only IO buffers from memory passed in, max TLS is 16k */
594590
if (flag & WOLFMEM_IO_POOL || flag & WOLFMEM_IO_POOL_FIXED) {
595591
if ((ret = wc_create_memory_buckets(pt, ava,
596-
WOLFMEM_IO_SZ, 1, &(heap->io))) < 0) {
592+
WOLFMEM_IO_SZ, 1, &(heap->io))) < 0) {
597593
WOLFSSL_LEAVE("wc_partition_static_memory", ret);
598594
return ret;
599595
}
@@ -635,10 +631,6 @@ static int wc_partition_static_memory(byte* buffer, word32 sz, int flag,
635631
static int wc_init_memory_heap(WOLFSSL_HEAP* heap, unsigned int listSz,
636632
const unsigned int* sizeList, const unsigned int* distList)
637633
{
638-
if (heap == NULL || listSz > WOLFMEM_MAX_BUCKETS) {
639-
return BAD_FUNC_ARG;
640-
}
641-
642634
XMEMSET(heap, 0, sizeof(WOLFSSL_HEAP));
643635

644636
XMEMCPY(heap->sizeList, sizeList, listSz * sizeof(sizeList[0]));
@@ -653,20 +645,24 @@ static int wc_init_memory_heap(WOLFSSL_HEAP* heap, unsigned int listSz,
653645
}
654646

655647
int wc_LoadStaticMemory_ex(WOLFSSL_HEAP_HINT** pHint,
656-
unsigned int listSz, const unsigned int* memSzList,
657-
const unsigned int* memDistList, unsigned char* buf,
648+
unsigned int listSz, const unsigned int* sizeList,
649+
const unsigned int* distList, unsigned char* buf,
658650
unsigned int sz, int flag, int maxSz)
659651
{
660652
WOLFSSL_HEAP* heap = NULL;
661653
WOLFSSL_HEAP_HINT* hint = NULL;
662654
word32 idx = 0;
663655
int ret;
664656

665-
if (pHint == NULL || buf == NULL || listSz > WOLFMEM_MAX_BUCKETS) {
657+
WOLFSSL_ENTER("wc_LoadStaticMemory_ex");
658+
659+
if (pHint == NULL || buf == NULL || listSz > WOLFMEM_MAX_BUCKETS
660+
|| sizeList == NULL || distList == NULL) {
666661
return BAD_FUNC_ARG;
667662
}
668663

669664
if ((sizeof(WOLFSSL_HEAP) + sizeof(WOLFSSL_HEAP_HINT)) > sz - idx) {
665+
WOLFSSL_MSG("Not enough memory for partition tracking");
670666
return BUFFER_E; /* not enough memory for structures */
671667
}
672668

@@ -677,7 +673,7 @@ int wc_LoadStaticMemory_ex(WOLFSSL_HEAP_HINT** pHint,
677673
hint = (WOLFSSL_HEAP_HINT*)(buf + idx);
678674
idx += sizeof(WOLFSSL_HEAP_HINT);
679675

680-
ret = wc_init_memory_heap(heap, listSz, memSzList, memDistList);
676+
ret = wc_init_memory_heap(heap, listSz, sizeList, distList);
681677
if (ret != 0) {
682678
return ret;
683679
}
@@ -700,7 +696,7 @@ int wc_LoadStaticMemory_ex(WOLFSSL_HEAP_HINT** pHint,
700696
ret = wc_partition_static_memory(buf + idx, sz - idx, flag, heap);
701697
if (ret != 1) {
702698
WOLFSSL_MSG("Error partitioning memory");
703-
return -1;
699+
return MEMORY_E;
704700
}
705701

706702
/* determine what max applies too */
@@ -714,8 +710,6 @@ int wc_LoadStaticMemory_ex(WOLFSSL_HEAP_HINT** pHint,
714710
heap->flag |= flag;
715711
*pHint = hint;
716712

717-
(void)maxSz;
718-
719713
return 0;
720714
}
721715

@@ -724,10 +718,14 @@ int wc_LoadStaticMemory(WOLFSSL_HEAP_HINT** pHint,
724718
{
725719
word32 sizeList[WOLFMEM_DEF_BUCKETS] = { WOLFMEM_BUCKETS };
726720
word32 distList[WOLFMEM_DEF_BUCKETS] = { WOLFMEM_DIST };
721+
int ret = 0;
727722

728-
return wc_LoadStaticMemory_ex(pHint,
723+
WOLFSSL_ENTER("wc_LoadStaticMemory");
724+
ret = wc_LoadStaticMemory_ex(pHint,
729725
WOLFMEM_DEF_BUCKETS, sizeList, distList,
730726
buf, sz, flag, maxSz);
727+
WOLFSSL_LEAVE("wc_LoadStaticMemory", ret);
728+
return ret;
731729
}
732730

733731

@@ -754,7 +752,8 @@ int wolfSSL_StaticBufferSz_ex(unsigned int listSz,
754752

755753
WOLFSSL_ENTER("wolfSSL_StaticBufferSz_ex");
756754

757-
if (buffer == NULL || listSz > WOLFMEM_MAX_BUCKETS) {
755+
if (buffer == NULL || listSz > WOLFMEM_MAX_BUCKETS
756+
|| sizeList == NULL || distList == NULL) {
758757
return BAD_FUNC_ARG;
759758
}
760759

0 commit comments

Comments
 (0)