summaryrefslogtreecommitdiffstats
path: root/cpukit/libcsupport/src/rtemscalloc.c
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/libcsupport/src/rtemscalloc.c
parentlibc: Reimplement posix_memlign() (diff)
downloadrtems-2c5199bb049efe8e29cd12461dc57bd6e30388e8.tar.bz2
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.
Diffstat (limited to 'cpukit/libcsupport/src/rtemscalloc.c')
-rw-r--r--cpukit/libcsupport/src/rtemscalloc.c9
1 files changed, 5 insertions, 4 deletions
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 ) ) {