git: 9front

Download patch

ref: 9c85501815ad91034892f5d409f5a34ab2410143
parent: ee9ba3db384d4b3c06299af2e118e97e208ca852
author: Ori Bernstein <ori@eigenstate.org>
date: Fri Dec 18 02:16:29 EST 2020

strndup: don't assume buffer is terminated

Using strlen in strndup will walk past the first
n bytes up to the terminator, which may not be
present. This is not what we want.

While we're here, do some cleanups.

--- a/sys/src/ape/lib/ap/gen/strndup.c
+++ b/sys/src/ape/lib/ap/gen/strndup.c
@@ -8,13 +8,11 @@
 	int n;
 	char *np;
 
-	n = strlen(p)+1;
-	if(n > max)
-		n = max+1;
-	np = malloc(n);
+	n = strnlen(p, max);
+	np = malloc(n+1);
 	if(!np)
-		return nil;
-	memmove(np, p, n);
-	np[n-1] = 0;
+		return NULL;
+	memcpy(np, p, n);
+	np[n] = 0;
 	return np;
 }
--