summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOri Bernstein <ori@eigenstate.org>2020-12-18 07:16:29 -0800
committerOri Bernstein <ori@eigenstate.org>2020-12-18 07:16:29 -0800
commit2db3642b8d3b871c6f9d1f4e3204b2f44fc4b79f (patch)
tree70d9f6b0bf5ef59abcc9fe8adca25d94b7ae887b
parentf09788364452d6921d555587196f5453e1259257 (diff)
downloadplan9front-2db3642b8d3b871c6f9d1f4e3204b2f44fc4b79f.tar.xz
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.
-rw-r--r--sys/src/ape/lib/ap/gen/strndup.c12
1 files changed, 5 insertions, 7 deletions
diff --git a/sys/src/ape/lib/ap/gen/strndup.c b/sys/src/ape/lib/ap/gen/strndup.c
index b60e2da2b..ef9d447df 100644
--- a/sys/src/ape/lib/ap/gen/strndup.c
+++ b/sys/src/ape/lib/ap/gen/strndup.c
@@ -8,13 +8,11 @@ strndup(char *p, size_t max)
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;
}