scc

simple c99 compiler
git clone git://git.simple-cc.org/scc
Log | Files | Refs | Submodules | README | LICENSE

commit e2a1ec076c6543aacae71a150c198c705db08a68
parent ddef1973a8abfceb690a1654fca690316d1cadb5
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Tue, 29 Mar 2022 18:00:12 +0200

libc/realloc: Set errno for invalid parameters

Realloc() was checking some error conditions and returning NULL
in that case, but it meant that errno was not set in that case
which can lead to confusing outputs if perror() or strerror()
are called after it.

Diffstat:
Msrc/libc/stdlib/realloc.c | 10+++++++++-
1 file changed, 9 insertions(+), 1 deletion(-)

diff --git a/src/libc/stdlib/realloc.c b/src/libc/stdlib/realloc.c @@ -1,3 +1,4 @@ +#include <errno.h> #include <stdint.h> #include <stdlib.h> #include <string.h> @@ -11,8 +12,15 @@ realloc(void *ptr, size_t nbytes) Header *oh, *prev, *next, *new; size_t nunits, avail, onbytes, n; - if (nbytes == 0 || nbytes > SIZE_MAX - sizeof(Header)-1) + if (nbytes == 0) { + errno = EINVAL; return NULL; + } + + if (nbytes > SIZE_MAX - sizeof(Header)-1) { + errno = ENOMEM; + return NULL; + } if (!ptr) return malloc(nbytes);