From 39cc845df3b8edf6389d59f75c528e529698c407 Mon Sep 17 00:00:00 2001 From: Polshakov Dmitry Date: Fri, 18 Feb 2022 15:32:05 +0300 Subject: [PATCH] Fix crash on typing "#include " (#446) * fix: scan include name until end of line * fix: correctly check file existance Co-authored-by: Dmitry Polshakov --- .../source/pattern_language/preprocessor.cpp | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/lib/libimhex/source/pattern_language/preprocessor.cpp b/lib/libimhex/source/pattern_language/preprocessor.cpp index 81dee9841..b603ca2a0 100644 --- a/lib/libimhex/source/pattern_language/preprocessor.cpp +++ b/lib/libimhex/source/pattern_language/preprocessor.cpp @@ -50,7 +50,7 @@ namespace hex::pl { offset += 1; std::string includeFile; - while (code[offset] != endChar) { + while (code[offset] != endChar && code[offset] != '\n') { includeFile += code[offset]; offset += 1; @@ -63,20 +63,24 @@ namespace hex::pl { if (includeFile[0] != '/') { for (const auto &dir : hex::getPath(ImHexPath::PatternsInclude)) { - fs::path tempPath = dir / includePath; - if (fs::exists(tempPath)) { + fs::path tempPath = dir / includePath; + if (fs::is_regular_file(tempPath)) { includePath = tempPath; break; } } } + if (!fs::is_regular_file(includePath)) { + if (includePath.parent_path().filename().string() == "std") + throwPreprocessorError(hex::format("{0}: No such file.\n\nThis file might be part of the standard library.\nYou can install the standard library though\nthe Content Store found under Help -> Content Store.", includeFile.c_str()), lineNumber); + else + throwPreprocessorError(hex::format("{0}: No such file", includeFile.c_str()), lineNumber); + } + File file(includePath, File::Mode::Read); if (!file.isValid()) { - if (includePath.parent_path().filename().string() == "std") - throwPreprocessorError(hex::format("{0}: No such file or directory.\n\nThis file might be part of the standard library.\nYou can install the standard library though\nthe Content Store found under Help -> Content Store.", includeFile.c_str()), lineNumber); - else - throwPreprocessorError(hex::format("{0}: No such file or directory", includeFile.c_str()), lineNumber); + throwPreprocessorError(hex::format("{0}: Failed to open file", includeFile.c_str()), lineNumber); } Preprocessor preprocessor;