From 773bfb0d4d3afc32c675aee9670d3f4f0205b8af Mon Sep 17 00:00:00 2001 From: bloodstalker Date: Sun, 5 Feb 2017 23:18:02 +0330 Subject: in case mutator gets a bad header, now it throws an exception instead of just a seg fault. --- mutator-lvl0.cpp | 52 +++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 49 insertions(+), 3 deletions(-) (limited to 'mutator-lvl0.cpp') diff --git a/mutator-lvl0.cpp b/mutator-lvl0.cpp index a8c0f52..fffa2e7 100644 --- a/mutator-lvl0.cpp +++ b/mutator-lvl0.cpp @@ -25,6 +25,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.* #include "mutator_aux.h" /*standard headers*/ #include +#include #include #include #include @@ -75,6 +76,26 @@ std::vector MacroUndefSourceLocation; std::vector MacroNameString; std::vector IncludeFileArr; +/**********************************************************************************************************************/ +struct MutExHeaderNotFound : public std::exception +{ +public: + MutExHeaderNotFound(std::string FileName) : FName(FileName) {} + + const char* what () const throw() + { + const char* OutString = "Header not Found"; + return OutString; + } + + std::string getFileName() const + { + return FName; + } + +private: + std::string FName; +}; /**********************************************************************************************************************/ /*@DEVI-struct for nullstmt*/ struct NullStmtInfo @@ -5473,18 +5494,37 @@ class PPInclusion : public PPCallbacks public: explicit PPInclusion (SourceManager *SM) : SM(*SM) {} + /*@DEVI-if we dont throw an exception for a bad header inclusion, we wil crash later on since we have InclusionDirective PPCallback.*/ + virtual bool FileNotFound(StringRef FileName, SmallVectorImpl &RecoveryPath) + { + throw MutExHeaderNotFound(FileName.str()); + } + virtual void InclusionDirective (SourceLocation HashLoc, const Token &IncludeTok, StringRef FileName, \ bool IsAngled, CharSourceRange FileNameRange, const FileEntry* File, \ StringRef SearchPath, StringRef RelativePath, const clang::Module* Imported) { - /*@DEVI-not including the correct directory for headers inclusions will make the code crash. the below consition does not prevent that.*/ - if (File->isValid()) +#if defined(__linux__) + std::ifstream HeaderABS(SearchPath.str() + "/" + FileName.str()); + std::ifstream HeaderRel(RelativePath.str() + "/" + FileName.str()); +#elif defined(__MACH__) && defined(__APPLE__) + std::ifstream HeaderABS(SearchPath.str() + "/" + FileName.str()); + std::ifstream HeaderRel(RelativePath.str() + "/" + FileName.str()); +#elif defined(__CYGWIN__) || defined(_WIN32) || defined(_WIN64) + std::ifstream HeaderABS(SearchPath.str() + "\\" + FileName.str()); + std::ifstream HeaderRel(RelativePath.str() + "\\" + FileName.str()); +#else + std::ifstream HeaderABS(SearchPath.str() + "/" + FileName.str()); + std::ifstream HeaderRel(RelativePath.str() + "/" + FileName.str()); +#endif + + if (File->isValid() && (HeaderABS.good() || HeaderRel.good())) { #if 0 assert(HashLoc.isValid() && "The SourceLocation for InclusionDirective is invalid."); #endif -#if 0 +#if 1 if (IsAngled) { size_t singleQPos = FileName.find("\'", 0); @@ -7386,6 +7426,12 @@ int main(int argc, const char **argv) JSONDocOUT.CloseReport(); + try {} + catch (MutExHeaderNotFound &E1) + { + std::cerr << E1.what() << std::endl; + } + return RunResult; } /*last line intentionally left blank.*/ -- cgit v1.2.3