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.
This commit is contained in:
Sebastian Huber
2021-05-06 08:03:58 +02:00
parent 5580b93f36
commit 2c5199bb04
8 changed files with 57 additions and 64 deletions
+4
View File
@@ -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 );
}
+6 -5
View File
@@ -35,14 +35,15 @@ void *calloc(
size_t length;
if ( nelem == 0 ) {
length = 0;
} else if ( elsize > SIZE_MAX / nelem ) {
errno = ENOMEM;
return NULL;
} else {
length = nelem * elsize;
}
if ( elsize > SIZE_MAX / nelem ) {
errno = ENOMEM;
return NULL;
}
length = nelem * elsize;
cptr = malloc( length );
RTEMS_OBFUSCATE_VARIABLE( cptr );
if ( RTEMS_PREDICT_FALSE( cptr == NULL ) ) {
+4
View File
@@ -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;
+4
View File
@@ -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
+4
View File
@@ -37,6 +37,10 @@ int posix_memalign(
*memptr = NULL;
if ( size == 0 ) {
return 0;
}
if ( alignment < sizeof( void * ) ) {
return EINVAL;
}
+4
View File
@@ -40,6 +40,10 @@ int rtems_memalign(
*pointer = NULL;
if ( size == 0 ) {
return 0;
}
/*
* Perform the aligned allocation requested
*/
+5 -4
View File
@@ -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 ) ) {
+26 -55
View File
@@ -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(