[PATCH] score: Fix _Workspace_Initialize_for_one_area()

Sebastian Huber sebastian.huber at embedded-brains.de
Tue Dec 7 17:07:51 UTC 2021


In _Workspace_Initialize_for_one_area(), properly check  if there is enough
free memory available for the configured workspace size.

The bug was introduced by commit 3d0620b607ff6459fec9d30efc1e0589bbd010f9.
---
 cpukit/include/rtems/score/wkspaceinitone.h | 34 ++++++++++++---------
 1 file changed, 20 insertions(+), 14 deletions(-)

diff --git a/cpukit/include/rtems/score/wkspaceinitone.h b/cpukit/include/rtems/score/wkspaceinitone.h
index c68e1b5db1..ce26a1cf8f 100644
--- a/cpukit/include/rtems/score/wkspaceinitone.h
+++ b/cpukit/include/rtems/score/wkspaceinitone.h
@@ -59,26 +59,29 @@ extern "C" {
  */
 RTEMS_INLINE_ROUTINE void _Workspace_Initialize_for_one_area( void )
 {
-  uintptr_t page_size;
-  uintptr_t wkspace_size;
-  uintptr_t wkspace_size_with_overhead;
-  uintptr_t available_size;
+  uintptr_t                 page_size;
+  uintptr_t                 wkspace_size;
+  uintptr_t                 wkspace_size_with_overhead;
+  const Memory_Information *mem;
+  Memory_Area              *area;
+  uintptr_t                 free_size;
+  uintptr_t                 available_size;
 
   page_size = CPU_HEAP_ALIGNMENT;
   wkspace_size = rtems_configuration_get_work_space_size();
   wkspace_size_with_overhead = wkspace_size + _Heap_Area_overhead( page_size );
 
-  if ( wkspace_size < wkspace_size_with_overhead ) {
-    const Memory_Information *mem;
-    Memory_Area              *area;
-    uintptr_t                 free_size;
-    uintptr_t                 size;
+  mem = _Memory_Get();
+  _Assert( _Memory_Get_count( mem ) == 1 );
 
-    mem = _Memory_Get();
-    _Assert( _Memory_Get_count( mem ) == 1 );
+  area = _Memory_Get_area( mem, 0 );
+  free_size = _Memory_Get_free_size( area );
 
-    area = _Memory_Get_area( mem, 0 );
-    free_size = _Memory_Get_free_size( area );
+  if (
+    wkspace_size < wkspace_size_with_overhead &&
+    free_size >= wkspace_size_with_overhead
+  ) {
+    uintptr_t size;
 
     if ( rtems_configuration_get_unified_work_area() ) {
       size = free_size;
@@ -95,7 +98,10 @@ RTEMS_INLINE_ROUTINE void _Workspace_Initialize_for_one_area( void )
 
     _Memory_Consume( area, size );
   } else {
-    /* An unsigned integer overflow happened */
+    /*
+     * An unsigned integer overflow happened, or the available free memory is
+     * not enough.
+     */
     available_size = 0;
   }
 
-- 
2.26.2



More information about the devel mailing list