From 6a580bb3320d6cbef4551d0c9e86f70665612620 Mon Sep 17 00:00:00 2001 From: Ilari Liusvaara Date: Tue, 15 Dec 2015 04:17:11 +0200 Subject: [PATCH] Add regex sanity checks on startup and add --sanity-check --- include/core/loadlib.hpp | 3 +++ src/core/loadlib.cpp | 3 +++ src/platform/wxwidgets/main.cpp | 38 +++++++++++++++++++++++++++++++++ 3 files changed, 44 insertions(+) diff --git a/include/core/loadlib.hpp b/include/core/loadlib.hpp index d7fe9ab2..a43a4677 100644 --- a/include/core/loadlib.hpp +++ b/include/core/loadlib.hpp @@ -7,5 +7,8 @@ void handle_post_loadlibrary(); void autoload_libraries(void(*on_error)(const std::string& libname, const std::string& err, bool system) = NULL); void with_loaded_library(const loadlib::module& l); bool with_unloaded_library(loadlib::module& l); +std::string loadlib_debug_get_user_library_dir(); +std::string loadlib_debug_get_system_library_dir(); + #endif diff --git a/src/core/loadlib.cpp b/src/core/loadlib.cpp index 3591bde7..15e5f4c0 100644 --- a/src/core/loadlib.cpp +++ b/src/core/loadlib.cpp @@ -208,3 +208,6 @@ void autoload_libraries(void(*on_error)(const std::string& libname, const std::s } handle_post_loadlibrary(); } + +std::string loadlib_debug_get_user_library_dir() { return get_user_library_dir(); } +std::string loadlib_debug_get_system_library_dir() { return get_system_library_dir(); } diff --git a/src/platform/wxwidgets/main.cpp b/src/platform/wxwidgets/main.cpp index 5c52a8c7..b8dc8111 100644 --- a/src/platform/wxwidgets/main.cpp +++ b/src/platform/wxwidgets/main.cpp @@ -16,6 +16,7 @@ #include "core/messages.hpp" #include "core/misc.hpp" #include "core/instance.hpp" +#include "core/misc.hpp" #include "core/moviefile-common.hpp" #include "core/moviedata.hpp" #include "core/random.hpp" @@ -25,6 +26,7 @@ #include "interface/romtype.hpp" #include "library/crandom.hpp" #include "library/directory.hpp" +#include "library/running-executable.hpp" #include "library/string.hpp" #include "library/threads.hpp" #include "library/utf8.hpp" @@ -441,6 +443,21 @@ void lsnes_app::OnInitCmdLine(wxCmdLineParser& parser) parser.SetSwitchChars(wxT("")); } +static bool regex_sanity_check() +{ + bool regex_sane = true; + try { + //Simple sanity checks. + regex_sane &= regex_match("foo.*baz", "foobarbaz", REGEX_MATCH_REGEX); + regex_sane &= regex_match(".*foo.*baz.*", "foobarbaz", REGEX_MATCH_REGEX); + regex_sane &= regex_match("foo*baz", "FOOBARBAZ", REGEX_MATCH_IWILDCARDS); + regex_sane &= regex_match("foo.*baz", "FOOBARBAZ", REGEX_MATCH_IREGEX); + } catch(...) { + regex_sane = false; + } + return regex_sane; +} + bool lsnes_app::OnCmdLineParsed(wxCmdLineParser& parser) { for(size_t i = 0; i< parser.GetParamCount(); i++) @@ -457,6 +474,7 @@ bool lsnes_app::OnCmdLineParsed(wxCmdLineParser& parser) std::cout << "--lua=: Load specified Lua script on startup" << std::endl; std::cout << "--library=: Load specified library on startup" << std::endl; std::cout << "--set==: Set setting to value " << std::endl; + std::cout << "--sanity-check: Perfrom some simple sanity checks" << std::endl; std::cout << ": Load specified ROM on startup" << std::endl; exit_immediately = true; return true; @@ -475,6 +493,21 @@ bool lsnes_app::OnCmdLineParsed(wxCmdLineParser& parser) c_lua.push_back(r[1]); if(r = regex("--library=(.+)", i)) c_library.push_back(r[1]); + if(i == "--sanity-check") { + if(regex_sanity_check()) { + std::cout << "Regex library passes basic sanity checks." << std::endl; + } else { + std::cout << "Regex library FAILS basic sanity checks." << std::endl; + } + std::cout << "Executable: '" << running_executable() << "'" << std::endl; + std::cout << "Configuration directory: '" << get_config_path() + << "'" << std::endl; + std::cout << "System autoload directory: '" << loadlib_debug_get_system_library_dir() + << "'" << std::endl; + std::cout << "User autoload directory: '" << loadlib_debug_get_user_library_dir() + << "'" << std::endl; + exit_immediately = true; + } } return true; } @@ -514,6 +547,11 @@ bool lsnes_app::OnInit() return false; } + if(!regex_sanity_check()) { + wxMessageBox(towxstring("Regex sanity check FAILED.\n\nExpect problems."), + _T("lsnes: Error"), wxICON_EXCLAMATION | wxOK, NULL); + } + reached_main(); set_random_seed(); bring_app_foreground();