shithub: scc

Download patch

ref: af54b65089b204d8032664acebce17df7efd04d3
parent: 1acd5b871784b694808522b487b9dedea67068a2
author: Roberto E. Vargas Caballero <[email protected]>
date: Tue Mar 29 14:00:12 EDT 2022

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.

--- 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 @@
 	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);