diff options
author | dzwdz | 2022-08-12 15:26:57 +0200 |
---|---|---|
committer | dzwdz | 2022-08-12 15:26:57 +0200 |
commit | 1528e2153dfbd729916f1211a6f481a2171f855f (patch) | |
tree | 4a5e746215a2279cc4f5e5ab1be32b86b3c86ff8 /src/user | |
parent | 55ce6808cd99ecbb06bd9b0fac181ed2c3bf15ff (diff) |
user/libc: getcwd() bounds checking
Diffstat (limited to 'src/user')
-rw-r--r-- | src/user/lib/stdlib.c | 10 |
1 files changed, 7 insertions, 3 deletions
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; } |