scc

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

commit af54b65089b204d8032664acebce17df7efd04d3
parent 1acd5b871784b694808522b487b9dedea67068a2
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);