From 8f4ddcef033333a41f3a0c8b043dc056829b0cfb Mon Sep 17 00:00:00 2001 From: Andynium Date: Sun, 1 Mar 2026 10:06:26 +0100 Subject: [PATCH] utils: make fallback realpath safe --- utils/utils.c | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/utils/utils.c b/utils/utils.c index c64718531..2ad921eee 100644 --- a/utils/utils.c +++ b/utils/utils.c @@ -27,6 +27,7 @@ #include #include #include +#include #include "utils/messages.h" #include "utils/dirent.h" @@ -472,14 +473,20 @@ int uname(struct utsname *buf) { char *realpath(const char *path, char *resolved_path) { - char *ret; - if (resolved_path == NULL) { - ret=strdup(path); - } else { - ret = resolved_path; - strcpy(resolved_path, path); + if (path == NULL) { + errno = EINVAL; + return NULL; } - return ret; + + /* Without a system realpath(), we cannot safely copy into an unknown-sized + * caller buffer. Provide only the allocating form. + */ + if (resolved_path != NULL) { + errno = ENAMETOOLONG; + return NULL; + } + + return strdup(path); } #endif