Skip to content

Commit 8eb3585

Browse files
authored
Move MSB check after final size calculation (#463)
We use the MSB of the size member of a BlockLink_t to track whether not a block is allocated. Consequently, the size must not be so large that the MSB is set. The check to see if the MSB in the size is set needs to be done after the final size (metadata + alignment) is calculated. Signed-off-by: Gaurav Aggarwal <[email protected]>
1 parent 82be779 commit 8eb3585

File tree

3 files changed

+78
-79
lines changed

3 files changed

+78
-79
lines changed

portable/MemMang/heap_2.c

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -160,36 +160,36 @@ void * pvPortMalloc( size_t xWantedSize )
160160
xHeapHasBeenInitialised = pdTRUE;
161161
}
162162

163-
/* Check the requested block size is not so large that the top bit is
164-
* set. The top bit of the block size member of the BlockLink_t structure
165-
* is used to determine who owns the block - the application or the
166-
* kernel, so it must be free. */
167-
if( heapBLOCK_SIZE_IS_VALID( xWantedSize ) )
163+
/* The wanted size must be increased so it can contain a BlockLink_t
164+
* structure in addition to the requested amount of bytes. */
165+
if( ( xWantedSize > 0 ) &&
166+
( ( xWantedSize + heapSTRUCT_SIZE ) > xWantedSize ) ) /* Overflow check. */
168167
{
169-
/* The wanted size must be increased so it can contain a BlockLink_t
170-
* structure in addition to the requested amount of bytes. */
171-
if( ( xWantedSize > 0 ) &&
172-
( ( xWantedSize + heapSTRUCT_SIZE ) > xWantedSize ) ) /* Overflow check */
173-
{
174-
xWantedSize += heapSTRUCT_SIZE;
168+
xWantedSize += heapSTRUCT_SIZE;
175169

176-
/* Byte alignment required. Check for overflow. */
177-
if( ( xWantedSize + ( portBYTE_ALIGNMENT - ( xWantedSize & portBYTE_ALIGNMENT_MASK ) ) )
178-
> xWantedSize )
179-
{
180-
xWantedSize += ( portBYTE_ALIGNMENT - ( xWantedSize & portBYTE_ALIGNMENT_MASK ) );
181-
configASSERT( ( xWantedSize & portBYTE_ALIGNMENT_MASK ) == 0 );
182-
}
183-
else
184-
{
185-
xWantedSize = 0;
186-
}
170+
/* Byte alignment required. Check for overflow. */
171+
if( ( xWantedSize + ( portBYTE_ALIGNMENT - ( xWantedSize & portBYTE_ALIGNMENT_MASK ) ) )
172+
> xWantedSize )
173+
{
174+
xWantedSize += ( portBYTE_ALIGNMENT - ( xWantedSize & portBYTE_ALIGNMENT_MASK ) );
175+
configASSERT( ( xWantedSize & portBYTE_ALIGNMENT_MASK ) == 0 );
187176
}
188177
else
189178
{
190179
xWantedSize = 0;
191180
}
181+
}
182+
else
183+
{
184+
xWantedSize = 0;
185+
}
192186

187+
/* Check the block size we are trying to allocate is not so large that the
188+
* top bit is set. The top bit of the block size member of the BlockLink_t
189+
* structure is used to determine who owns the block - the application or
190+
* the kernel, so it must be free. */
191+
if( heapBLOCK_SIZE_IS_VALID( xWantedSize ) != 0 )
192+
{
193193
if( ( xWantedSize > 0 ) && ( xWantedSize <= xFreeBytesRemaining ) )
194194
{
195195
/* Blocks are stored in byte order - traverse the list from the start
@@ -274,10 +274,10 @@ void vPortFree( void * pv )
274274
* byte alignment warnings. */
275275
pxLink = ( void * ) puc;
276276

277-
configASSERT( heapBLOCK_IS_ALLOCATED( pxLink ) );
277+
configASSERT( heapBLOCK_IS_ALLOCATED( pxLink ) != 0 );
278278
configASSERT( pxLink->pxNextFreeBlock == NULL );
279279

280-
if( heapBLOCK_IS_ALLOCATED( pxLink ) )
280+
if( heapBLOCK_IS_ALLOCATED( pxLink ) != 0 )
281281
{
282282
if( pxLink->pxNextFreeBlock == NULL )
283283
{

portable/MemMang/heap_4.c

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -146,44 +146,43 @@ void * pvPortMalloc( size_t xWantedSize )
146146
mtCOVERAGE_TEST_MARKER();
147147
}
148148

149-
/* Check the requested block size is not so large that the top bit is
150-
* set. The top bit of the block size member of the BlockLink_t structure
151-
* is used to determine who owns the block - the application or the
152-
* kernel, so it must be free. */
153-
if( heapBLOCK_SIZE_IS_VALID( xWantedSize ) )
149+
/* The wanted size must be increased so it can contain a BlockLink_t
150+
* structure in addition to the requested amount of bytes. */
151+
if( ( xWantedSize > 0 ) &&
152+
( ( xWantedSize + xHeapStructSize ) > xWantedSize ) ) /* Overflow check. */
154153
{
155-
/* The wanted size must be increased so it can contain a BlockLink_t
156-
* structure in addition to the requested amount of bytes. */
157-
if( ( xWantedSize > 0 ) &&
158-
( ( xWantedSize + xHeapStructSize ) > xWantedSize ) ) /* Overflow check */
159-
{
160-
xWantedSize += xHeapStructSize;
154+
xWantedSize += xHeapStructSize;
161155

162-
/* Ensure that blocks are always aligned. */
163-
if( ( xWantedSize & portBYTE_ALIGNMENT_MASK ) != 0x00 )
156+
/* Ensure that blocks are always aligned. */
157+
if( ( xWantedSize & portBYTE_ALIGNMENT_MASK ) != 0x00 )
158+
{
159+
/* Byte alignment required. Check for overflow. */
160+
if( ( xWantedSize + ( portBYTE_ALIGNMENT - ( xWantedSize & portBYTE_ALIGNMENT_MASK ) ) ) > xWantedSize )
164161
{
165-
/* Byte alignment required. Check for overflow. */
166-
if( ( xWantedSize + ( portBYTE_ALIGNMENT - ( xWantedSize & portBYTE_ALIGNMENT_MASK ) ) )
167-
> xWantedSize )
168-
{
169-
xWantedSize += ( portBYTE_ALIGNMENT - ( xWantedSize & portBYTE_ALIGNMENT_MASK ) );
170-
configASSERT( ( xWantedSize & portBYTE_ALIGNMENT_MASK ) == 0 );
171-
}
172-
else
173-
{
174-
xWantedSize = 0;
175-
}
162+
xWantedSize += ( portBYTE_ALIGNMENT - ( xWantedSize & portBYTE_ALIGNMENT_MASK ) );
163+
configASSERT( ( xWantedSize & portBYTE_ALIGNMENT_MASK ) == 0 );
176164
}
177165
else
178166
{
179-
mtCOVERAGE_TEST_MARKER();
167+
xWantedSize = 0;
180168
}
181169
}
182170
else
183171
{
184-
xWantedSize = 0;
172+
mtCOVERAGE_TEST_MARKER();
185173
}
174+
}
175+
else
176+
{
177+
xWantedSize = 0;
178+
}
186179

180+
/* Check the block size we are trying to allocate is not so large that the
181+
* top bit is set. The top bit of the block size member of the BlockLink_t
182+
* structure is used to determine who owns the block - the application or
183+
* the kernel, so it must be free. */
184+
if( heapBLOCK_SIZE_IS_VALID( xWantedSize ) != 0 )
185+
{
187186
if( ( xWantedSize > 0 ) && ( xWantedSize <= xFreeBytesRemaining ) )
188187
{
189188
/* Traverse the list from the start (lowest address) block until
@@ -302,10 +301,10 @@ void vPortFree( void * pv )
302301
/* This casting is to keep the compiler from issuing warnings. */
303302
pxLink = ( void * ) puc;
304303

305-
configASSERT( heapBLOCK_IS_ALLOCATED( pxLink ) );
304+
configASSERT( heapBLOCK_IS_ALLOCATED( pxLink ) != 0 );
306305
configASSERT( pxLink->pxNextFreeBlock == NULL );
307306

308-
if( heapBLOCK_IS_ALLOCATED( pxLink ) )
307+
if( heapBLOCK_IS_ALLOCATED( pxLink ) != 0 )
309308
{
310309
if( pxLink->pxNextFreeBlock == NULL )
311310
{

portable/MemMang/heap_5.c

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -157,43 +157,43 @@ void * pvPortMalloc( size_t xWantedSize )
157157

158158
vTaskSuspendAll();
159159
{
160-
/* Check the requested block size is not so large that the top bit is
161-
* set. The top bit of the block size member of the BlockLink_t structure
162-
* is used to determine who owns the block - the application or the
163-
* kernel, so it must be free. */
164-
if( heapBLOCK_SIZE_IS_VALID( xWantedSize ) )
160+
/* The wanted size is increased so it can contain a BlockLink_t
161+
* structure in addition to the requested amount of bytes. */
162+
if( ( xWantedSize > 0 ) &&
163+
( ( xWantedSize + xHeapStructSize ) > xWantedSize ) ) /* Overflow check. */
165164
{
166-
/* The wanted size is increased so it can contain a BlockLink_t
167-
* structure in addition to the requested amount of bytes. */
168-
if( ( xWantedSize > 0 ) &&
169-
( ( xWantedSize + xHeapStructSize ) > xWantedSize ) ) /* Overflow check */
170-
{
171-
xWantedSize += xHeapStructSize;
165+
xWantedSize += xHeapStructSize;
172166

173-
/* Ensure that blocks are always aligned */
174-
if( ( xWantedSize & portBYTE_ALIGNMENT_MASK ) != 0x00 )
167+
/* Ensure that blocks are always aligned */
168+
if( ( xWantedSize & portBYTE_ALIGNMENT_MASK ) != 0x00 )
169+
{
170+
/* Byte alignment required. Check for overflow */
171+
if( ( xWantedSize + ( portBYTE_ALIGNMENT - ( xWantedSize & portBYTE_ALIGNMENT_MASK ) ) ) >
172+
xWantedSize )
175173
{
176-
/* Byte alignment required. Check for overflow */
177-
if( ( xWantedSize + ( portBYTE_ALIGNMENT - ( xWantedSize & portBYTE_ALIGNMENT_MASK ) ) ) >
178-
xWantedSize )
179-
{
180-
xWantedSize += ( portBYTE_ALIGNMENT - ( xWantedSize & portBYTE_ALIGNMENT_MASK ) );
181-
}
182-
else
183-
{
184-
xWantedSize = 0;
185-
}
174+
xWantedSize += ( portBYTE_ALIGNMENT - ( xWantedSize & portBYTE_ALIGNMENT_MASK ) );
186175
}
187176
else
188177
{
189-
mtCOVERAGE_TEST_MARKER();
178+
xWantedSize = 0;
190179
}
191180
}
192181
else
193182
{
194-
xWantedSize = 0;
183+
mtCOVERAGE_TEST_MARKER();
195184
}
185+
}
186+
else
187+
{
188+
xWantedSize = 0;
189+
}
196190

191+
/* Check the block size we are trying to allocate is not so large that the
192+
* top bit is set. The top bit of the block size member of the BlockLink_t
193+
* structure is used to determine who owns the block - the application or
194+
* the kernel, so it must be free. */
195+
if( heapBLOCK_SIZE_IS_VALID( xWantedSize ) != 0 )
196+
{
197197
if( ( xWantedSize > 0 ) && ( xWantedSize <= xFreeBytesRemaining ) )
198198
{
199199
/* Traverse the list from the start (lowest address) block until
@@ -310,10 +310,10 @@ void vPortFree( void * pv )
310310
/* This casting is to keep the compiler from issuing warnings. */
311311
pxLink = ( void * ) puc;
312312

313-
configASSERT( heapBLOCK_IS_ALLOCATED( pxLink ) );
313+
configASSERT( heapBLOCK_IS_ALLOCATED( pxLink ) != 0 );
314314
configASSERT( pxLink->pxNextFreeBlock == NULL );
315315

316-
if( heapBLOCK_IS_ALLOCATED( pxLink ) )
316+
if( heapBLOCK_IS_ALLOCATED( pxLink ) != 0 )
317317
{
318318
if( pxLink->pxNextFreeBlock == NULL )
319319
{

0 commit comments

Comments
 (0)