From 230298f05170bedef562b942bb17f654dadad4a1 Mon Sep 17 00:00:00 2001 From: Peng Wu Date: Tue, 25 Mar 2025 13:43:05 +0800 Subject: [PATCH] Fix crash when read the config file --- src/Config.cpp | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/Config.cpp b/src/Config.cpp index a8392b1e9..5c768c07c 100644 --- a/src/Config.cpp +++ b/src/Config.cpp @@ -16,6 +16,7 @@ * limitations under the License. */ +#include #include #include #include @@ -216,6 +217,16 @@ std::string GetParentDirectory(const std::string& path) { return path.substr(0, pos + 1); } +bool isRegularFile(const std::string& path) { + struct stat info; + + if (stat(path.c_str(), &info) != 0) + return false; + + // Check if it's a regular file + return (info.st_mode & S_IFMT) == S_IFREG; +} + } // namespace Config::Config() : internal(new ConfigInternal()) {} @@ -241,6 +252,8 @@ ConverterPtr Config::NewFromFile(const std::string& fileName, impl->paths.push_back(PACKAGE_DATA_DIRECTORY); } std::string prefixedFileName = impl->FindConfigFile(fileName); + if (!isRegularFile(prefixedFileName)) + throw FileNotFound(prefixedFileName); std::ifstream ifs(UTF8Util::GetPlatformString(prefixedFileName)); std::string content(std::istreambuf_iterator(ifs), (std::istreambuf_iterator()));