From 1bc253968d78b75e1f80e504e797d41f2d875864 Mon Sep 17 00:00:00 2001 From: "Chris Davies (MSTIC)" Date: Tue, 11 Dec 2018 17:37:21 +0000 Subject: [PATCH] This isn't finished, but I think represents a better way of handling warnings and errors that doesn't treat warnings as errors --- YaraSharp/Compiler.cpp | 29 ++++++++++++++--------------- YaraSharp/Compiler.h | 3 ++- YaraSharp/YaraSharp.cpp | 8 ++++++++ 3 files changed, 24 insertions(+), 16 deletions(-) diff --git a/YaraSharp/Compiler.cpp b/YaraSharp/Compiler.cpp index 009d7ec..57ed47c 100644 --- a/YaraSharp/Compiler.cpp +++ b/YaraSharp/Compiler.cpp @@ -10,7 +10,11 @@ namespace YaraSharp ErrorUtility::ThrowOnError(yr_compiler_create(&TestCompiler)); Compiler = TestCompiler; - Errors = gcnew List(); + // Set up and initialize the error dictionary for errors and warnings + // Which are populated by the callback handler + Errors = gcnew Dictionary^>(); + Errors->Add(YARA_ERROR_LEVEL_ERROR, gcnew List()); + Errors->Add(YARA_ERROR_LEVEL_WARNING, gcnew List()); SetCompilerExternals(ExternalVariables); SetCompilerCallback(); @@ -52,7 +56,11 @@ namespace YaraSharp } List^ CCompiler::GetErrors() { - return this->Errors; + return this->Errors[YARA_ERROR_LEVEL_ERROR]; + } + List^ CCompiler::GetWarnings() + { + return this->Errors[YARA_ERROR_LEVEL_WARNING]; } // Set externals @@ -99,20 +107,11 @@ namespace YaraSharp UNREFERENCED_PARAMETER(UserData); String^ errorLevel; - - if (ErrorLevel == YARA_ERROR_LEVEL_ERROR) - { - errorLevel = "ERROR"; - } - else if (ErrorLevel == YARA_ERROR_LEVEL_WARNING) - { - errorLevel = "WARNING"; - } - - auto msg = String::Format("{0}: {1} on line {2} in file: {3}", - errorLevel, marshal_as(Message), LineNumber, + + auto msg = String::Format("{0} on line {1} in file: {2}", + marshal_as(Message), LineNumber, Filename ? marshal_as(Filename) : "[none]"); - Errors->Add(msg); + Errors[ErrorLevel]->Add(msg); } } \ No newline at end of file diff --git a/YaraSharp/Compiler.h b/YaraSharp/Compiler.h index 1b41407..664255f 100644 --- a/YaraSharp/Compiler.h +++ b/YaraSharp/Compiler.h @@ -8,7 +8,7 @@ namespace YaraSharp public ref class CCompiler sealed { initonly YR_COMPILER* Compiler; - initonly List^ Errors; + initonly Dictionary^>^ Errors; public: CCompiler(Dictionary^ ExternalVariables); @@ -16,6 +16,7 @@ namespace YaraSharp CRules^ GetRules(); List^ GetErrors(); + List^ GetWarnings(); int AddFile(String^ FilePath); void AddFiles(List^ FilePathList); diff --git a/YaraSharp/YaraSharp.cpp b/YaraSharp/YaraSharp.cpp index 4f4ae3c..5babdaa 100644 --- a/YaraSharp/YaraSharp.cpp +++ b/YaraSharp/YaraSharp.cpp @@ -19,6 +19,10 @@ namespace YaraSharp CompilationErrors->Add(FilePathList[i], TestCompiler->GetErrors()); FilePathList->Remove(FilePathList[i--]); } + else if (TestCompiler->GetWarnings()->Count > 0) + { + CompilationErrors->Add(FilePathList[i], TestCompiler->GetWarnings()); + } delete TestCompiler; } @@ -41,6 +45,10 @@ namespace YaraSharp delete TestCompiler; break; } + else if (TestCompiler->GetWarnings()->Count > 0) + { + CompilationErrors->Add(FilePath, TestCompiler->GetWarnings()); + } } }