From 2c5199bb049efe8e29cd12461dc57bd6e30388e8 Mon Sep 17 00:00:00 2001 From: Sebastian Huber Date: Wed, 5 May 2021 08:49:52 +0200 Subject: 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 +++++---- 7 files changed, 30 insertions(+), 8 deletions(-) (limited to 'cpukit') 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 ) ) { -- cgit v1.2.3