summaryrefslogtreecommitdiffstats
path: root/cpukit
diff options
context:
space:
mode:
authorSebastian Huber <sebastian.huber@embedded-brains.de>2021-05-05 08:49:52 +0200
committerSebastian Huber <sebastian.huber@embedded-brains.de>2021-05-06 08:03:58 +0200
commit2c5199bb049efe8e29cd12461dc57bd6e30388e8 (patch)
tree31086387522ae9803a5a15bb33ee014ed96eeaf8 /cpukit
parent5580b93f367e057ef0184231e7619beea4ae3734 (diff)
downloadrtems-master.tar.bz2
Return NULL for zero size allocationsHEADmaster
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.
Diffstat (limited to 'cpukit')
-rw-r--r--cpukit/libcsupport/src/alignedalloc.c4
-rw-r--r--cpukit/libcsupport/src/calloc.c9
-rw-r--r--cpukit/libcsupport/src/malloc.c4
-rw-r--r--cpukit/libcsupport/src/malloc_deferred.c4
-rw-r--r--cpukit/libcsupport/src/posix_memalign.c4
-rw-r--r--cpukit/libcsupport/src/rtems_memalign.c4
-rw-r--r--cpukit/libcsupport/src/rtemscalloc.c9
7 files changed, 30 insertions, 8 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 ) ) {