opencc/opencc-fixes-crash.patch
2025-07-17 14:09:39 +08:00

44 lines
1.3 KiB
Diff

commit cfe3ced00bbf764f68f10222b9d2a6367c2c3f82
Author: Peng Wu <pwu@redhat.com>
Date: Tue Mar 25 13:43:05 2025 +0800
Fix crash when read the config file
diff --git a/src/Config.cpp b/src/Config.cpp
index a8392b1..5c768c0 100644
--- a/src/Config.cpp
+++ b/src/Config.cpp
@@ -16,6 +16,7 @@
* limitations under the License.
*/
+#include <sys/stat.h>
#include <fstream>
#include <list>
#include <unordered_map>
@@ -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<char>(ifs),
(std::istreambuf_iterator<char>()));