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/calloc.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'cpukit/libcsupport/src/calloc.c') 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 ) ) { -- cgit v1.2.3