Follow @Openwall on Twitter for new release announcements and other news
[<prev] [next>] [thread-next>] [day] [month] [year] [list]
Date: Thu, 21 Jan 2021 09:02:40 -0500
From: "Alex Xu (Hello71)" <alex_y_xu@...oo.ca>
To: musl@...ts.openwall.com
Cc: "Alex Xu (Hello71)" <alex_y_xu@...oo.ca>
Subject: [PATCH] don't set errno in free

busybox echo fails if free sets errno, which madvise does on old
kernels.
---
 src/malloc/mallocng/free.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)

diff --git a/src/malloc/mallocng/free.c b/src/malloc/mallocng/free.c
index 40745f97..82836815 100644
--- a/src/malloc/mallocng/free.c
+++ b/src/malloc/mallocng/free.c
@@ -119,7 +119,13 @@ void free(void *p)
 	if (((uintptr_t)(start-1) ^ (uintptr_t)end) >= 2*PGSZ && g->last_idx) {
 		unsigned char *base = start + (-(uintptr_t)start & (PGSZ-1));
 		size_t len = (end-base) & -PGSZ;
-		if (len) madvise(base, len, MADV_FREE);
+		if (len) {
+			// madvise(..., MADV_FREE) returns -EINVAL on old kernels
+			// POSIX.1-202x requires free() to not modify errno on success
+			int e = errno;
+			madvise(base, len, MADV_FREE);
+			errno = e;
+		}
 	}
 
 	// atomic free without locking if this is neither first or last slot
@@ -139,5 +145,9 @@ void free(void *p)
 	wrlock();
 	struct mapinfo mi = nontrivial_free(g, idx);
 	unlock();
-	if (mi.len) munmap(mi.base, mi.len);
+	// POSIX.1-202x requires free() to not modify errno on success
+	// munmap should succeed but no harm checking it again
+	if (mi.len)
+		if (munmap(mi.base, mi.len))
+			a_crash();
 }
-- 
2.30.0

Powered by blists - more mailing lists

Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.