[rtems commit] Return NULL for zero size allocations

Sebastian Huber sebh at rtems.org
Thu May 6 13:14:56 UTC 2021


Module:    rtems
Branch:    master
Commit:    2c5199bb049efe8e29cd12461dc57bd6e30388e8
Changeset: http://git.rtems.org/rtems/commit/?id=2c5199bb049efe8e29cd12461dc57bd6e30388e8

Author:    Sebastian Huber <sebastian.huber at embedded-brains.de>
Date:      Wed May  5 08:49:52 2021 +0200

Return NULL for zero size allocations

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 b552fc2..9c9ea83 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 693aa21..d5cefb3 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 795254f..3e55a94 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 aab7640..b319d12 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 316ed73..4e89413 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 aa938ac..aa67c74 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 836f1da..7e05a14 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 c9dcb63..a337641 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(



More information about the vc mailing list