summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDarkvater <darkvater@openttd.org>2006-04-22 09:46:31 +0000
committerDarkvater <darkvater@openttd.org>2006-04-22 09:46:31 +0000
commited96dcd86896a251e46932873804186aa5db7551 (patch)
treed17778b0172b265e34c0b0bf267266b0bf46fe32
parent084acbf23cf3512de1e9f22e6118d3668a0c8843 (diff)
downloadopenttd-ed96dcd86896a251e46932873804186aa5db7551.tar.xz
(svn r4521) - Fix: be consistent about the size of the player-name in MP. This hopefully fixes a crash on lesser OS's (eg Win98). Use ttd_strlcpy() to just copy over strings and properly terminate them because different implementations of snprintf() behave differently. Courtesy of TrueLight
-rw-r--r--network.c10
-rw-r--r--network.h2
-rw-r--r--network_server.c10
-rw-r--r--network_server.h2
4 files changed, 12 insertions, 12 deletions
diff --git a/network.c b/network.c
index 95063afcc..b640a7268 100644
--- a/network.c
+++ b/network.c
@@ -92,11 +92,11 @@ NetworkClientState *NetworkFindClientStateFromIndex(uint16 client_index)
// if the user did not send it yet, Client #<no> is used.
void NetworkGetClientName(char *client_name, size_t size, const NetworkClientState *cs)
{
- NetworkClientInfo *ci = DEREF_CLIENT_INFO(cs);
- if (ci->client_name[0] == '\0')
- snprintf(client_name, size, "Client #%d", cs->index);
+ const NetworkClientInfo *ci = DEREF_CLIENT_INFO(cs);
+ if (*ci->client_name == '\0')
+ snprintf(client_name, size, "Client #%4d", cs->index);
else
- snprintf(client_name, size, "%s", ci->client_name);
+ ttd_strlcpy(client_name, ci->client_name, size);
}
byte NetworkSpectatorCount(void)
@@ -545,7 +545,7 @@ void NetworkCloseClient(NetworkClientState *cs)
// We did not receive a leave message from this client...
NetworkErrorCode errorno = NETWORK_ERROR_CONNECTION_LOST;
char str[100];
- char client_name[NETWORK_NAME_LENGTH];
+ char client_name[NETWORK_CLIENT_NAME_LENGTH];
NetworkClientState *new_cs;
NetworkGetClientName(client_name, sizeof(client_name), cs);
diff --git a/network.h b/network.h
index 867d2bb3f..0165b62c6 100644
--- a/network.h
+++ b/network.h
@@ -145,7 +145,7 @@ VARDEF NetworkGameInfo _network_game_info;
VARDEF NetworkPlayerInfo _network_player_info[MAX_PLAYERS];
VARDEF NetworkClientInfo _network_client_info[MAX_CLIENT_INFO];
-VARDEF char _network_player_name[NETWORK_NAME_LENGTH];
+VARDEF char _network_player_name[NETWORK_CLIENT_NAME_LENGTH];
VARDEF char _network_default_ip[NETWORK_HOSTNAME_LENGTH];
VARDEF uint16 _network_own_client_index;
diff --git a/network_server.c b/network_server.c
index fc4334d35..e89973f12 100644
--- a/network_server.c
+++ b/network_server.c
@@ -567,7 +567,7 @@ DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_COMPANY_INFO)
DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_JOIN)
{
- char name[NETWORK_NAME_LENGTH];
+ char name[NETWORK_CLIENT_NAME_LENGTH];
char unique_id[NETWORK_NAME_LENGTH];
NetworkClientInfo *ci;
byte playas;
@@ -612,7 +612,7 @@ DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_JOIN)
}
// We need a valid name.. make it Player
- if (name[0] == '\0') snprintf(name, sizeof(name), "Player");
+ if (*name == '\0') ttd_strlcpy(name, "Player", sizeof(name));
if (!NetworkFindName(name)) { // Change name if duplicate
// We could not create a name for this player
@@ -622,8 +622,8 @@ DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_JOIN)
ci = DEREF_CLIENT_INFO(cs);
- snprintf(ci->client_name, sizeof(ci->client_name), "%s", name);
- snprintf(ci->unique_id, sizeof(ci->unique_id), "%s", unique_id);
+ ttd_strlcpy(ci->client_name, name, sizeof(ci->client_name));
+ ttd_strlcpy(ci->unique_id, unique_id, sizeof(ci->unique_id));
ci->client_playas = playas;
ci->client_lang = client_lang;
@@ -1389,7 +1389,7 @@ bool NetworkFindName(char new_name[NETWORK_CLIENT_NAME_LENGTH])
byte number = 0;
char original_name[NETWORK_CLIENT_NAME_LENGTH];
- // We use NETWORK_NAME_LENGTH in here, because new_name is really a pointer
+ // We use NETWORK_CLIENT_NAME_LENGTH in here, because new_name is really a pointer
ttd_strlcpy(original_name, new_name, NETWORK_CLIENT_NAME_LENGTH);
while (!found_name) {
diff --git a/network_server.h b/network_server.h
index 8d09c4c36..89b05aff4 100644
--- a/network_server.h
+++ b/network_server.h
@@ -12,7 +12,7 @@ DEF_SERVER_SEND_COMMAND(PACKET_SERVER_SHUTDOWN);
DEF_SERVER_SEND_COMMAND(PACKET_SERVER_NEWGAME);
DEF_SERVER_SEND_COMMAND_PARAM(PACKET_SERVER_RCON)(NetworkClientState *cs, uint16 color, const char *command);
-bool NetworkFindName(char new_name[NETWORK_NAME_LENGTH]);
+bool NetworkFindName(char new_name[NETWORK_CLIENT_NAME_LENGTH]);
void NetworkServer_HandleChat(NetworkAction action, DestType desttype, int dest, const char *msg, uint16 from_index);
bool NetworkServer_ReadPackets(NetworkClientState *cs);