From 22bb757726cb82f5046c3dc56c358c1947314650 Mon Sep 17 00:00:00 2001 From: yangfl Date: Wed, 19 Mar 2025 10:36:39 +0800 Subject: [PATCH] Remove PATH_MAX from realpath POSIX.1-2008 ensures realpath() give a dynamically allocated buffer if NULL is passed, which avoids using PATH_MAX, which may be too large to fit in stack, or even undefined on some systems. --- common/filesystem.cpp | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/common/filesystem.cpp b/common/filesystem.cpp index 5287a0c..766ee5c 100644 --- a/common/filesystem.cpp +++ b/common/filesystem.cpp @@ -100,10 +100,14 @@ bool changeDirectory(const UString & dir) } UString getAbsPath(const UString & path) { - char abs[PATH_MAX] = {}; + char * abs = realpath(path.toLocal8Bit(), nullptr); // Last is a non-standard extension for non-existent files - if (realpath(path.toLocal8Bit(), abs) || abs[0] != '\0') - return UString(abs); - return path; + UString new_path; + if (abs) + new_path = UString(abs); + else + new_path = path; + free(abs); + return new_path; } #endif