fixed bug #33545: With MEM_USE_POOLS==1, mem_malloc can return an unaligned pointer.

This commit is contained in:
goldsimon 2011-06-26 17:31:10 +00:00 committed by goldsimon
parent 49e16fcbe9
commit 22ee104a04
2 changed files with 13 additions and 5 deletions

View File

@ -17,6 +17,13 @@ HISTORY
++ Bugfixes: ++ Bugfixes:
2011-06-26: Simon Goldschmidt
* mem.c: fixed bug #33545: With MEM_USE_POOLS==1, mem_malloc can return an
unaligned pointer.
2011-06-26: Simon Goldschmidt
* mem.c: fixed bug #33544 "warning in mem.c in lwip 1.4.0 with NO_SYS=1"
2011-05-25: Simon Goldschmidt 2011-05-25: Simon Goldschmidt
* tcp.c: fixed bug #33398 (pointless conversion when checking TCP port range) * tcp.c: fixed bug #33398 (pointless conversion when checking TCP port range)

View File

@ -78,9 +78,10 @@
void * void *
mem_malloc(mem_size_t size) mem_malloc(mem_size_t size)
{ {
void *ret;
struct memp_malloc_helper *element; struct memp_malloc_helper *element;
memp_t poolnr; memp_t poolnr;
mem_size_t required_size = size + sizeof(struct memp_malloc_helper); mem_size_t required_size = size + LWIP_MEM_ALIGN_SIZE(sizeof(struct memp_malloc_helper));
for (poolnr = MEMP_POOL_FIRST; poolnr <= MEMP_POOL_LAST; poolnr = (memp_t)(poolnr + 1)) { for (poolnr = MEMP_POOL_FIRST; poolnr <= MEMP_POOL_LAST; poolnr = (memp_t)(poolnr + 1)) {
#if MEM_USE_POOLS_TRY_BIGGER_POOL #if MEM_USE_POOLS_TRY_BIGGER_POOL
@ -113,9 +114,9 @@ again:
/* save the pool number this element came from */ /* save the pool number this element came from */
element->poolnr = poolnr; element->poolnr = poolnr;
/* and return a pointer to the memory directly after the struct memp_malloc_helper */ /* and return a pointer to the memory directly after the struct memp_malloc_helper */
element++; ret = (u8_t*)element + LWIP_MEM_ALIGN_SIZE(sizeof(struct memp_malloc_helper));
return element; return ret;
} }
/** /**
@ -128,13 +129,13 @@ again:
void void
mem_free(void *rmem) mem_free(void *rmem)
{ {
struct memp_malloc_helper *hmem = (struct memp_malloc_helper*)rmem; struct memp_malloc_helper *hmem;
LWIP_ASSERT("rmem != NULL", (rmem != NULL)); LWIP_ASSERT("rmem != NULL", (rmem != NULL));
LWIP_ASSERT("rmem == MEM_ALIGN(rmem)", (rmem == LWIP_MEM_ALIGN(rmem))); LWIP_ASSERT("rmem == MEM_ALIGN(rmem)", (rmem == LWIP_MEM_ALIGN(rmem)));
/* get the original struct memp_malloc_helper */ /* get the original struct memp_malloc_helper */
hmem--; hmem = (struct memp_malloc_helper*)(void*)((u8_t*)rmem - LWIP_MEM_ALIGN_SIZE(sizeof(struct memp_malloc_helper)));
LWIP_ASSERT("hmem != NULL", (hmem != NULL)); LWIP_ASSERT("hmem != NULL", (hmem != NULL));
LWIP_ASSERT("hmem == MEM_ALIGN(hmem)", (hmem == LWIP_MEM_ALIGN(hmem))); LWIP_ASSERT("hmem == MEM_ALIGN(hmem)", (hmem == LWIP_MEM_ALIGN(hmem)));