Enforce stricter world names using a blacklist

Blacklisted characters are: / \
master
Matthew I 2012-09-02 16:51:17 -04:00 committed by Perttu Ahola
parent a0da6bcf43
commit 5dd1d354f8
4 changed files with 35 additions and 1 deletions

View File

@ -39,6 +39,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "tile.h" // getTexturePath #include "tile.h" // getTexturePath
#include "filesys.h" #include "filesys.h"
#include "util/string.h" #include "util/string.h"
#include "subgame.h"
struct CreateWorldDestMainMenu : public CreateWorldDest struct CreateWorldDestMainMenu : public CreateWorldDest
{ {
@ -47,6 +48,9 @@ struct CreateWorldDestMainMenu : public CreateWorldDest
{} {}
void accepted(std::wstring name, std::string gameid) void accepted(std::wstring name, std::string 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); m_menu->createNewWorld(name, gameid);
} }
GUIMainMenu *m_menu; GUIMainMenu *m_menu;
@ -929,3 +933,7 @@ int GUIMainMenu::getTab()
return TAB_SINGLEPLAYER; // Default return TAB_SINGLEPLAYER; // Default
} }
void GUIMainMenu::displayMessageMenu(std::wstring msg)
{
(new GUIMessageMenu(env, parent, -1, menumgr, msg))->drop();
}

View File

@ -92,6 +92,7 @@ public:
void createNewWorld(std::wstring name, std::string gameid); void createNewWorld(std::wstring name, std::string gameid);
void deleteWorld(const std::vector<std::string> &paths); void deleteWorld(const std::vector<std::string> &paths);
int getTab(); int getTab();
void displayMessageMenu(std::wstring msg);
private: private:
MainMenuData *m_data; MainMenuData *m_data;

View File

@ -24,6 +24,8 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include <set> #include <set>
#include <vector> #include <vector>
#define WORLDNAME_BLACKLISTED_CHARS "/\\"
struct SubgameSpec struct SubgameSpec
{ {
std::string id; // "" = game does not exist std::string id; // "" = game does not exist

View File

@ -242,6 +242,29 @@ inline bool string_allowed(const std::string &s, const std::string &allowed_char
return true; 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 Forcefully wraps string into rows using \n
(no word wrap, used for showing paths in gui) (no word wrap, used for showing paths in gui)