From b73c455670c467539a28611be8d30ac421be8441 Mon Sep 17 00:00:00 2001 From: Frank Busse Date: Thu, 26 Nov 2020 17:40:49 +0000 Subject: posix runtime: getcwd: check malloc and set errno * failing malloc was not handled before, now returns null/ENOMEM * when path is non-null and size is zero return null/EINVAL --- runtime/POSIX/fd.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/runtime/POSIX/fd.c b/runtime/POSIX/fd.c index cfd2622f..35d0d315 100644 --- a/runtime/POSIX/fd.c +++ b/runtime/POSIX/fd.c @@ -1351,9 +1351,15 @@ char *getcwd(char *buf, size_t size) { } if (!buf) { - if (!size) - size = 1024; + size = 1024; // typically PATH_MAX buf = malloc(size); + if (!buf) { + errno = ENOMEM; + return NULL; + } + } else if (!size) { + errno = EINVAL; + return NULL; } buf = __concretize_ptr(buf); -- cgit 1.4.1