Add player name length checks 1572/head
authorsapier <Sapier at GMX dot net>
Fri, 22 Aug 2014 12:03:04 +0000 (14:03 +0200)
committersapier <Sapier at GMX dot net>
Fri, 22 Aug 2014 19:18:43 +0000 (21:18 +0200)
src/main.cpp
src/server.cpp

index bb2ac5faf47fe2cb57feccd7f3551ac8e80f45fd..a44a2e6112d333213bca4d45e14d002731f9d96d 100644 (file)
@@ -79,6 +79,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "httpfetch.h"
 #include "guiEngine.h"
 #include "mapsector.h"
+#include "player.h"
 
 #include "database-sqlite3.h"
 #ifdef USE_LEVELDB
@@ -1843,6 +1844,13 @@ int main(int argc, char *argv[])
                                break;
                        }
 
+                       if (current_playername.length() > PLAYERNAME_SIZE-1) {
+                               error_message = wgettext("Player name to long.");
+                               playername = current_playername.substr(0,PLAYERNAME_SIZE-1);
+                               g_settings->set("name", playername);
+                               continue;
+                       }
+
                        /*
                                Run game
                        */
index 9bd8e70f3a157e52df11fdf379236fb430286f39..93d00f3698a7af0998307f6ea9bd489597a0678a 100644 (file)
@@ -1448,14 +1448,21 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id)
                /*
                        Set up player
                */
-
-               // Get player name
                char playername[PLAYERNAME_SIZE];
-               for(u32 i=0; i<PLAYERNAME_SIZE-1; i++)
-               {
-                       playername[i] = data[3+i];
+               unsigned int playername_length = 0;
+               for (; playername_length < PLAYERNAME_SIZE; playername_length++ ) {
+                       playername[playername_length] = data[3+playername_length];
+                       if (data[3+playername_length] == 0)
+                               break;
+               }
+
+               if (playername_length == PLAYERNAME_SIZE) {
+                       actionstream<<"Server: Player with name exceeding max length "
+                                       <<"tried to connect from "<<addr_s<<std::endl;
+                       DenyAccess(peer_id, L"Name to long");
+                       return;
                }
-               playername[PLAYERNAME_SIZE-1] = 0;
+
 
                if(playername[0]=='\0')
                {