From 5dd1d354f86692e4c08cc78f3d9743557103449e Mon Sep 17 00:00:00 2001 From: Matthew I Date: Sun, 2 Sep 2012 16:51:17 -0400 Subject: [PATCH] Enforce stricter world names using a blacklist Blacklisted characters are: / \ --- src/guiMainMenu.cpp | 10 +++++++++- src/guiMainMenu.h | 1 + src/subgame.h | 2 ++ src/util/string.h | 23 +++++++++++++++++++++++ 4 files changed, 35 insertions(+), 1 deletion(-) diff --git a/src/guiMainMenu.cpp b/src/guiMainMenu.cpp index cdf1bc7d5..4ceecbb5f 100644 --- a/src/guiMainMenu.cpp +++ b/src/guiMainMenu.cpp @@ -39,6 +39,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "tile.h" // getTexturePath #include "filesys.h" #include "util/string.h" +#include "subgame.h" struct CreateWorldDestMainMenu : public CreateWorldDest { @@ -47,7 +48,10 @@ struct CreateWorldDestMainMenu : public CreateWorldDest {} void accepted(std::wstring name, std::string gameid) { - m_menu->createNewWorld(name, gameid); + if(!string_allowed_blacklist(wide_to_narrow(name), WORLDNAME_BLACKLISTED_CHARS)) + m_menu->displayMessageMenu(wgettext("Cannot create world: Name contains invalid characters")); + else + m_menu->createNewWorld(name, gameid); } GUIMainMenu *m_menu; }; @@ -929,3 +933,7 @@ int GUIMainMenu::getTab() return TAB_SINGLEPLAYER; // Default } +void GUIMainMenu::displayMessageMenu(std::wstring msg) +{ + (new GUIMessageMenu(env, parent, -1, menumgr, msg))->drop(); +} \ No newline at end of file diff --git a/src/guiMainMenu.h b/src/guiMainMenu.h index fa3d83c45..715deb47d 100644 --- a/src/guiMainMenu.h +++ b/src/guiMainMenu.h @@ -92,6 +92,7 @@ public: void createNewWorld(std::wstring name, std::string gameid); void deleteWorld(const std::vector &paths); int getTab(); + void displayMessageMenu(std::wstring msg); private: MainMenuData *m_data; diff --git a/src/subgame.h b/src/subgame.h index e3a299cbe..bffa86e28 100644 --- a/src/subgame.h +++ b/src/subgame.h @@ -24,6 +24,8 @@ with this program; if not, write to the Free Software Foundation, Inc., #include #include +#define WORLDNAME_BLACKLISTED_CHARS "/\\" + struct SubgameSpec { std::string id; // "" = game does not exist diff --git a/src/util/string.h b/src/util/string.h index 97b07f2ff..71b11de3d 100644 --- a/src/util/string.h +++ b/src/util/string.h @@ -242,6 +242,29 @@ inline bool string_allowed(const std::string &s, const std::string &allowed_char return true; } +/* + Checks if a string contains no blacklisted characters (opposite + function of string_allowed()) +*/ +inline bool string_allowed_blacklist(const std::string & s, const std::string & blacklisted_chars) +{ + for(unsigned int i = 0; i < s.length(); i++) + { + bool invalid = false; + for(unsigned int j = 0; j < blacklisted_chars.length(); j++) + { + if(s[i] == blacklisted_chars[j]) + { + invalid = true; + break; + } + } + if(invalid) + return false; + } + return true; +} + /* Forcefully wraps string into rows using \n (no word wrap, used for showing paths in gui) -- 2.25.1