[PATCH] Return NULL for zero size allocations

Sebastian Huber sebastian.huber at embedded-brains.de
Wed May 5 06:52:54 UTC 2021


In POSIX, zero size memory allocations are implementation-defined
behaviour.  The implementation has two options:

https://pubs.opengroup.org/onlinepubs/9699919799/functions/malloc.html

https://pubs.opengroup.org/onlinepubs/9699919799/functions/posix_memalign.html

Linux and FreeBSD return a unique pointer for zero size memory
allocations.   Return NULL on RTEMS to more likely catch the use of a
zero size memory area by erroneous applications.

Update #4390.
---
 cpukit/libcsupport/src/alignedalloc.c    |  4 ++
 cpukit/libcsupport/src/calloc.c          |  9 +--
 cpukit/libcsupport/src/malloc.c          |  4 ++
 cpukit/libcsupport/src/malloc_deferred.c |  4 ++
 cpukit/libcsupport/src/posix_memalign.c  |  4 ++
 cpukit/libcsupport/src/rtems_memalign.c  |  4 ++
 cpukit/libcsupport/src/rtemscalloc.c     |  9 +--
 testsuites/libtests/malloctest/init.c    | 81 ++++++++----------------
 8 files changed, 56 insertions(+), 63 deletions(-)

diff --git a/cpukit/libcsupport/src/alignedalloc.c b/cpukit/libcsupport/src/alignedalloc.c
index b552fc2a0f..9c9ea83bd8 100644
--- a/cpukit/libcsupport/src/alignedalloc.c
+++ b/cpukit/libcsupport/src/alignedalloc.c
@@ -35,6 +35,10 @@
 
 void *aligned_alloc( size_t alignment, size_t size )
 {
+  if ( size == 0 ) {
+    return NULL;
+  }
+
   return rtems_heap_allocate_aligned_with_boundary( size, alignment, 0 );
 }
 
diff --git a/cpukit/libcsupport/src/calloc.c b/cpukit/libcsupport/src/calloc.c
index 693aa21453..d5cefb382a 100644
--- a/cpukit/libcsupport/src/calloc.c
+++ b/cpukit/libcsupport/src/calloc.c
@@ -35,14 +35,15 @@ void *calloc(
   size_t  length;
 
   if ( nelem == 0 ) {
-    length = 0;
-  } else if ( elsize > SIZE_MAX / nelem ) {
+    return NULL;
+  }
+
+  if ( elsize > SIZE_MAX / nelem ) {
     errno = ENOMEM;
     return NULL;
-  } else {
-    length = nelem * elsize;
   }
 
+  length = nelem * elsize;
   cptr = malloc( length );
   RTEMS_OBFUSCATE_VARIABLE( cptr );
   if ( RTEMS_PREDICT_FALSE( cptr == NULL ) ) {
diff --git a/cpukit/libcsupport/src/malloc.c b/cpukit/libcsupport/src/malloc.c
index 795254fbab..3e55a94c83 100644
--- a/cpukit/libcsupport/src/malloc.c
+++ b/cpukit/libcsupport/src/malloc.c
@@ -30,6 +30,10 @@ void *malloc(
 {
   void        *return_this;
 
+  if ( size == 0 ) {
+    return NULL;
+  }
+
   return_this = rtems_heap_allocate_aligned_with_boundary( size, 0, 0 );
   if ( !return_this ) {
     errno = ENOMEM;
diff --git a/cpukit/libcsupport/src/malloc_deferred.c b/cpukit/libcsupport/src/malloc_deferred.c
index aab76406c7..b319d1213e 100644
--- a/cpukit/libcsupport/src/malloc_deferred.c
+++ b/cpukit/libcsupport/src/malloc_deferred.c
@@ -106,6 +106,10 @@ void *rtems_heap_allocate_aligned_with_boundary(
 
 void *rtems_malloc( size_t size )
 {
+  if ( size == 0 ) {
+    return NULL;
+  }
+
   return rtems_heap_allocate_aligned_with_boundary( size, 0, 0 );
 }
 #endif
diff --git a/cpukit/libcsupport/src/posix_memalign.c b/cpukit/libcsupport/src/posix_memalign.c
index 316ed7315c..4e89413c24 100644
--- a/cpukit/libcsupport/src/posix_memalign.c
+++ b/cpukit/libcsupport/src/posix_memalign.c
@@ -37,6 +37,10 @@ int posix_memalign(
 
   *memptr = NULL;
 
+  if ( size == 0 ) {
+    return 0;
+  }
+
   if ( alignment < sizeof( void * ) ) {
     return EINVAL;
   }
diff --git a/cpukit/libcsupport/src/rtems_memalign.c b/cpukit/libcsupport/src/rtems_memalign.c
index aa938ac66f..aa67c74a29 100644
--- a/cpukit/libcsupport/src/rtems_memalign.c
+++ b/cpukit/libcsupport/src/rtems_memalign.c
@@ -40,6 +40,10 @@ int rtems_memalign(
 
   *pointer = NULL;
 
+  if ( size == 0 ) {
+    return 0;
+  }
+
   /*
    *  Perform the aligned allocation requested
    */
diff --git a/cpukit/libcsupport/src/rtemscalloc.c b/cpukit/libcsupport/src/rtemscalloc.c
index 836f1da64d..7e05a14bb1 100644
--- a/cpukit/libcsupport/src/rtemscalloc.c
+++ b/cpukit/libcsupport/src/rtemscalloc.c
@@ -47,13 +47,14 @@ void *rtems_calloc( size_t nelem, size_t elsize )
   void   *p;
 
   if ( nelem == 0 ) {
-    length = 0;
-  } else if ( elsize > SIZE_MAX / nelem ) {
     return NULL;
-  } else {
-    length = nelem * elsize;
   }
 
+  if ( elsize > SIZE_MAX / nelem ) {
+    return NULL;
+  }
+
+  length = nelem * elsize;
   p = rtems_malloc( length );
   RTEMS_OBFUSCATE_VARIABLE( p );
   if ( RTEMS_PREDICT_FALSE( p == NULL ) ) {
diff --git a/testsuites/libtests/malloctest/init.c b/testsuites/libtests/malloctest/init.c
index c9dcb63b2c..a33764177d 100644
--- a/testsuites/libtests/malloctest/init.c
+++ b/testsuites/libtests/malloctest/init.c
@@ -1153,10 +1153,7 @@ static void test_rtems_malloc(void)
   void *p;
 
   p = rtems_malloc(0);
-  rtems_test_assert(p != NULL);
-
-  RTEMS_OBFUSCATE_VARIABLE(p);
-  free(p);
+  rtems_test_assert(p == NULL);
 
   errno = 0;
   p = rtems_malloc(SIZE_MAX / 2);
@@ -1176,22 +1173,13 @@ static void test_rtems_calloc(void)
   int *i;
 
   p = rtems_calloc(0, 0);
-  rtems_test_assert(p != NULL);
-
-  RTEMS_OBFUSCATE_VARIABLE(p);
-  free(p);
+  rtems_test_assert(p == NULL);
 
   p = rtems_calloc(0, 1);
-  rtems_test_assert(p != NULL);
-
-  RTEMS_OBFUSCATE_VARIABLE(p);
-  free(p);
+  rtems_test_assert(p == NULL);
 
   p = rtems_calloc(1, 0);
-  rtems_test_assert(p != NULL);
-
-  RTEMS_OBFUSCATE_VARIABLE(p);
-  free(p);
+  rtems_test_assert(p == NULL);
 
   errno = 0;
   p = rtems_calloc(1, SIZE_MAX / 2);
@@ -1323,73 +1311,56 @@ static void test_alloc_zero_size(void)
   int eno;
 
   size = 0;
+  errno = -1;
 
   RTEMS_OBFUSCATE_VARIABLE( size );
   p = malloc( size );
-  rtems_test_assert( p != NULL );
-
-  RTEMS_OBFUSCATE_VARIABLE( p );
-  free( p );
+  rtems_test_assert( p == NULL );
+  rtems_test_assert( errno == -1 );
 
   RTEMS_OBFUSCATE_VARIABLE( size );
-  p = calloc( 1, size );
-  rtems_test_assert( p != NULL );
-
-  RTEMS_OBFUSCATE_VARIABLE( p );
-  free( p );
+  p = calloc( size, 1 );
+  rtems_test_assert( p == NULL );
+  rtems_test_assert( errno == -1 );
 
   RTEMS_OBFUSCATE_VARIABLE( size );
   p = rtems_malloc( size );
-  rtems_test_assert( p != NULL );
-
-  RTEMS_OBFUSCATE_VARIABLE( p );
-  free( p );
+  rtems_test_assert( p == NULL );
+  rtems_test_assert( errno == -1 );
 
   RTEMS_OBFUSCATE_VARIABLE( size );
   p = rtems_calloc( 1, size );
-  rtems_test_assert( p != NULL );
-
-  RTEMS_OBFUSCATE_VARIABLE( p );
-  free( p );
+  rtems_test_assert( p == NULL );
+  rtems_test_assert( errno == -1 );
 
   RTEMS_OBFUSCATE_VARIABLE( size );
-  p = NULL;
+  p = (void *)(uintptr_t) 1;
   eno = posix_memalign( &p, 32, size );
   rtems_test_assert( eno == 0 );
-  rtems_test_assert( p != NULL );
-
-  RTEMS_OBFUSCATE_VARIABLE( p );
-  free( p );
+  rtems_test_assert( p == NULL );
+  rtems_test_assert( errno == -1 );
 
   RTEMS_OBFUSCATE_VARIABLE( size );
-  p = NULL;
+  p = (void *)(uintptr_t) 1;
   eno = rtems_memalign( &p, 32, size );
   rtems_test_assert( eno == 0 );
-  rtems_test_assert( p != NULL );
-
-  RTEMS_OBFUSCATE_VARIABLE( p );
-  free( p );
+  rtems_test_assert( p == NULL );
+  rtems_test_assert( errno == -1 );
 
   RTEMS_OBFUSCATE_VARIABLE( size );
   p = aligned_alloc( 32, size );
-  rtems_test_assert( p != NULL );
-
-  RTEMS_OBFUSCATE_VARIABLE( p );
-  free( p );
+  rtems_test_assert( p == NULL );
+  rtems_test_assert( errno == -1 );
 
   RTEMS_OBFUSCATE_VARIABLE( size );
   p = realloc( NULL, size );
-  rtems_test_assert( p != NULL );
-
-  RTEMS_OBFUSCATE_VARIABLE( p );
-  free( p );
+  rtems_test_assert( p == NULL );
+  rtems_test_assert( errno == -1 );
 
   RTEMS_OBFUSCATE_VARIABLE( size );
   p = reallocarray( NULL, 1, size );
-  rtems_test_assert( p != NULL );
-
-  RTEMS_OBFUSCATE_VARIABLE( p );
-  free( p );
+  rtems_test_assert( p == NULL );
+  rtems_test_assert( errno == -1 );
 }
 
 rtems_task Init(
-- 
2.26.2



More information about the devel mailing list