From dd59db2be2c25352354c7659413f7aee22e5baa0 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Thu, 13 Mar 2025 09:16:23 +0100 Subject: [PATCH] fs/LookupFile: check for empty file name on Windows Fixes crash bug in the (synthetic) unit test. See code comment. --- src/fs/LookupFile.cxx | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/fs/LookupFile.cxx b/src/fs/LookupFile.cxx index ad0a0b0f9..08a2d94b8 100644 --- a/src/fs/LookupFile.cxx +++ b/src/fs/LookupFile.cxx @@ -43,12 +43,30 @@ LookupFile(Path pathname) } catch (const std::system_error &e) { if (!IsPathNotFound(e)) throw; + +#ifdef _WIN32 + if (idx == 0) + /* on Windows, the semantics are + different for empty strings: + GetFileAttributesExA() fails with + ERROR_PATH_NOT_FOUND, and the + IsPathNotFound() check above would + not rethrow the exception, but the + empty string would lead to an + integer overflow in the code below, + so we need to make this a special + case on Windows */ + throw; +#endif + } //find one dir up if (slash != nullptr) *slash = '/'; + assert(idx > 0); + slash = FindSlash(&buffer.front(), idx - 1); if (slash == nullptr) return {};