From 1528e2153dfbd729916f1211a6f481a2171f855f Mon Sep 17 00:00:00 2001 From: dzwdz Date: Fri, 12 Aug 2022 15:26:57 +0200 Subject: user/libc: getcwd() bounds checking --- src/user/lib/stdlib.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) (limited to 'src/user') diff --git a/src/user/lib/stdlib.c b/src/user/lib/stdlib.c index a5d2d9b..43405d7 100644 --- a/src/user/lib/stdlib.c +++ b/src/user/lib/stdlib.c @@ -104,10 +104,14 @@ int chdir(const char *path) { return 0; } -char *getcwd(char *buf, size_t size) { +char *getcwd(char *buf, size_t capacity) { const char *realcwd = getrealcwd(); - // TODO bounds checking - memcpy(buf, realcwd, strlen(realcwd) + 1); + size_t len = strlen(realcwd) + 1; + if (capacity < len) { + errno = capacity == 0 ? EINVAL : ERANGE; + return NULL; + } + memcpy(buf, realcwd, len); return buf; } -- cgit v1.2.3