ensure getUser() returns a valid player name

This commit is contained in:
darkrose 2015-12-17 17:15:59 +10:00
parent 03fc74e997
commit 3f8324d7e0
2 changed files with 23 additions and 10 deletions

View File

@ -33,6 +33,7 @@
#include "config.h" #include "config.h"
#include "debug.h" #include "debug.h"
#include "filesys.h" #include "filesys.h"
#include "player.h"
#ifdef __APPLE__ #ifdef __APPLE__
#include "CoreFoundation/CoreFoundation.h" #include "CoreFoundation/CoreFoundation.h"
@ -374,19 +375,20 @@ void initializePaths(char* argv0)
std::string getUser() std::string getUser()
{ {
std::string user("someone");
#ifdef _WIN32 #ifdef _WIN32
char buff[1024]; char buff[1024];
int size = 1024; int size = 1024;
if (GetUserName(buff,LPDWORD(&size))) if (GetUserName(buff,LPDWORD(&size)))
return std::string(buff); user = std::string(buff);
#else #else
char* u = getenv("USER"); char* u = getenv("USER");
if (u) if (u)
return std::string(u); user = std::string(u);
#endif #endif
return std::string("someone"); return string_allowify(user,PLAYERNAME_ALLOWED_CHARS);
} }
} //namespace porting } //namespace porting

View File

@ -1413,23 +1413,34 @@ private:
*/ */
inline bool string_allowed(const std::string &s, const std::string &allowed_chars) inline bool string_allowed(const std::string &s, const std::string &allowed_chars)
{ {
for(u32 i=0; i<s.size(); i++) for (u32 i=0; i<s.size(); i++) {
{
bool confirmed = false; bool confirmed = false;
for(u32 j=0; j<allowed_chars.size(); j++) for (u32 j=0; j<allowed_chars.size(); j++) {
{ if (s[i] == allowed_chars[j]) {
if(s[i] == allowed_chars[j])
{
confirmed = true; confirmed = true;
break; break;
} }
} }
if(confirmed == false) if (confirmed == false)
return false; return false;
} }
return true; return true;
} }
inline std::string string_allowify(const std::string &s, const std::string &allowed_chars)
{
std::string result;
for (u32 i=0; i<s.size(); i++) {
for (u32 j=0; j<allowed_chars.size(); j++) {
if (s[i] == allowed_chars[j]) {
result += s[i];
break;
}
}
}
return result;
}
/* /*
Some helper stuff Some helper stuff
*/ */