[PATCH] Avoid a stack buffer when not needed
authorPino Toscano <toscano.pino@tiscali.it>
Thu, 10 Feb 2022 07:26:11 +0000 (08:26 +0100)
committerPino Toscano <pino@debian.org>
Sat, 3 Sep 2022 06:15:09 +0000 (07:15 +0100)
Allocate a PATH_MAX-sized buffer on stack only in case we are not using
realpath(X, null), i.e. on platforms with older POSIX versions, macOS,
or Android.

This fixes the build on platforms that do not have PATH_MAX (e.g.
GNU/Hurd), and it provides a minor optimization on realpath(X, null)
platforms.

Change-Id: Icd92a1b15ec18c5eef8113408e9610dfac774101
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Gbp-Pq: Name upstream_Avoid-a-stack-buffer-when-not-needed.patch

src/corelib/io/qfilesystemengine_unix.cpp

index efb42fa71523d09f1ee6e6f2aa7301b3cbd1e03b..966a9470e709f5cb42ae4b3b172da24024c5988b 100644 (file)
@@ -686,7 +686,9 @@ QFileSystemEntry QFileSystemEngine::canonicalName(const QFileSystemEntry &entry,
     Q_UNUSED(data);
     return QFileSystemEntry(slowCanonicalized(absoluteName(entry).filePath()));
 #else
+# if defined(Q_OS_DARWIN) || defined(Q_OS_ANDROID) || _POSIX_VERSION < 200801L
     char stack_result[PATH_MAX+1];
+# endif
     char *resolved_name = nullptr;
 # if defined(Q_OS_DARWIN) || defined(Q_OS_ANDROID)
     // On some Android and macOS versions, realpath() will return a path even if
@@ -714,8 +716,10 @@ QFileSystemEntry QFileSystemEngine::canonicalName(const QFileSystemEntry &entry,
         data.knownFlagsMask |= QFileSystemMetaData::ExistsAttribute;
         data.entryFlags |= QFileSystemMetaData::ExistsAttribute;
         QString canonicalPath = QDir::cleanPath(QFile::decodeName(resolved_name));
+# if defined(Q_OS_DARWIN) || defined(Q_OS_ANDROID) || _POSIX_VERSION < 200801L
         if (resolved_name != stack_result)
             free(resolved_name);
+# endif
         return QFileSystemEntry(canonicalPath);
     } else if (errno == ENOENT || errno == ENOTDIR) { // file doesn't exist
         data.knownFlagsMask |= QFileSystemMetaData::ExistsAttribute;