From 98f0d4d45b18d5e8f302ff7e055aa14fc58ff0e0 Mon Sep 17 00:00:00 2001 From: Gregor Richards Date: Sat, 3 Dec 2016 09:46:20 -0500 Subject: [PATCH 1/2] Better error messages when the Netplay handshake fails --- network/netplay/netplay_common.c | 50 ++++++++++++++++++++------------ 1 file changed, 31 insertions(+), 19 deletions(-) diff --git a/network/netplay/netplay_common.c b/network/netplay/netplay_common.c index 16779d54d7..d10a7072b8 100644 --- a/network/netplay/netplay_common.c +++ b/network/netplay/netplay_common.c @@ -170,7 +170,7 @@ bool netplay_handshake(netplay_t *netplay) bool is_server = netplay->is_server; int compression = 0; - msg[0] = '\0'; + msg[0] = msg[sizeof(msg)-1] = '\0'; mem_info.id = RETRO_MEMORY_SAVE_RAM; @@ -190,22 +190,21 @@ bool netplay_handshake(netplay_t *netplay) if (!socket_receive_all_blocking(netplay->fd, header, sizeof(header))) { - RARCH_ERR("%s\n", - msg_hash_to_str(MSG_FAILED_TO_RECEIVE_HEADER_FROM_CLIENT)); - return false; + strncpy(msg, msg_hash_to_str(MSG_FAILED_TO_RECEIVE_HEADER_FROM_CLIENT), sizeof(msg)-1); + goto error; } if (*content_crc_ptr != ntohl(header[0])) { - RARCH_ERR("%s\n", msg_hash_to_str(MSG_CONTENT_CRC32S_DIFFER)); - return false; + strncpy(msg, msg_hash_to_str(MSG_CONTENT_CRC32S_DIFFER), sizeof(msg)-1); + goto error; } if (netplay_impl_magic() != ntohl(header[1])) { - RARCH_ERR("Implementations differ, make sure you're using exact same " - "libretro implementations and RetroArch version.\n"); - return false; + strncpy(msg, "Implementations differ. Make sure you're using exact same " + "libretro implementations and RetroArch version.", sizeof(msg)-1); + goto error; } /* Some cores only report the correct sram size late, so we can't actually @@ -223,13 +222,17 @@ bool netplay_handshake(netplay_t *netplay) netplay_endian_mismatch(local_pmagic, remote_pmagic)) { RARCH_ERR("Endianness mismatch with an endian-sensitive core.\n"); - return false; + strncpy(msg, "This core does not support inter-architecture netplay " + "between these systems.", sizeof(msg)-1); + goto error; } if ((netplay->quirks & NETPLAY_QUIRK_PLATFORM_DEPENDENT) && (local_pmagic != remote_pmagic)) { RARCH_ERR("Platform mismatch with a platform-sensitive core.\n"); - return false; + strncpy(msg, "This core does not support inter-architecture netplay.", + sizeof(msg)-1); + goto error; } /* Clear any existing compression */ @@ -267,21 +270,22 @@ bool netplay_handshake(netplay_t *netplay) { if (!netplay_send_nickname(netplay, netplay->fd)) { - RARCH_ERR("%s\n", - msg_hash_to_str(MSG_FAILED_TO_SEND_NICKNAME_TO_HOST)); - return false; + strncpy(msg, msg_hash_to_str(MSG_FAILED_TO_SEND_NICKNAME_TO_HOST), + sizeof(msg)-1); + goto error; } } if (!netplay_get_nickname(netplay, netplay->fd)) { if (is_server) - RARCH_ERR("%s\n", - msg_hash_to_str(MSG_FAILED_TO_GET_NICKNAME_FROM_CLIENT)); + strncpy(msg, msg_hash_to_str(MSG_FAILED_TO_GET_NICKNAME_FROM_CLIENT), + sizeof(msg)-1); else - RARCH_ERR("%s\n", - msg_hash_to_str(MSG_FAILED_TO_RECEIVE_NICKNAME_FROM_HOST)); - return false; + strncpy(msg, + msg_hash_to_str(MSG_FAILED_TO_RECEIVE_NICKNAME_FROM_HOST), + sizeof(msg)-1); + goto error; } if (is_server) @@ -378,6 +382,14 @@ bool netplay_handshake(netplay_t *netplay) } return true; + +error: + if (msg[0]) + { + RARCH_ERR("%s\n", msg); + runloop_msg_queue_push(msg, 1, 180, false); + } + return false; } bool netplay_is_server(netplay_t* netplay) From f725f380616bfa832de4a97ce6ae21d61de0ac3f Mon Sep 17 00:00:00 2001 From: Gregor Richards Date: Sat, 3 Dec 2016 09:58:16 -0500 Subject: [PATCH 2/2] Use strlcpy --- network/netplay/netplay_common.c | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/network/netplay/netplay_common.c b/network/netplay/netplay_common.c index d10a7072b8..73e1632886 100644 --- a/network/netplay/netplay_common.c +++ b/network/netplay/netplay_common.c @@ -19,6 +19,7 @@ #include "netplay_private.h" #include +#include #include @@ -170,7 +171,7 @@ bool netplay_handshake(netplay_t *netplay) bool is_server = netplay->is_server; int compression = 0; - msg[0] = msg[sizeof(msg)-1] = '\0'; + msg[0] = '\0'; mem_info.id = RETRO_MEMORY_SAVE_RAM; @@ -190,20 +191,20 @@ bool netplay_handshake(netplay_t *netplay) if (!socket_receive_all_blocking(netplay->fd, header, sizeof(header))) { - strncpy(msg, msg_hash_to_str(MSG_FAILED_TO_RECEIVE_HEADER_FROM_CLIENT), sizeof(msg)-1); + strlcpy(msg, msg_hash_to_str(MSG_FAILED_TO_RECEIVE_HEADER_FROM_CLIENT), sizeof(msg)); goto error; } if (*content_crc_ptr != ntohl(header[0])) { - strncpy(msg, msg_hash_to_str(MSG_CONTENT_CRC32S_DIFFER), sizeof(msg)-1); + strlcpy(msg, msg_hash_to_str(MSG_CONTENT_CRC32S_DIFFER), sizeof(msg)); goto error; } if (netplay_impl_magic() != ntohl(header[1])) { - strncpy(msg, "Implementations differ. Make sure you're using exact same " - "libretro implementations and RetroArch version.", sizeof(msg)-1); + strlcpy(msg, "Implementations differ. Make sure you're using exact same " + "libretro implementations and RetroArch version.", sizeof(msg)); goto error; } @@ -222,16 +223,16 @@ bool netplay_handshake(netplay_t *netplay) netplay_endian_mismatch(local_pmagic, remote_pmagic)) { RARCH_ERR("Endianness mismatch with an endian-sensitive core.\n"); - strncpy(msg, "This core does not support inter-architecture netplay " - "between these systems.", sizeof(msg)-1); + strlcpy(msg, "This core does not support inter-architecture netplay " + "between these systems.", sizeof(msg)); goto error; } if ((netplay->quirks & NETPLAY_QUIRK_PLATFORM_DEPENDENT) && (local_pmagic != remote_pmagic)) { RARCH_ERR("Platform mismatch with a platform-sensitive core.\n"); - strncpy(msg, "This core does not support inter-architecture netplay.", - sizeof(msg)-1); + strlcpy(msg, "This core does not support inter-architecture netplay.", + sizeof(msg)); goto error; } @@ -270,8 +271,8 @@ bool netplay_handshake(netplay_t *netplay) { if (!netplay_send_nickname(netplay, netplay->fd)) { - strncpy(msg, msg_hash_to_str(MSG_FAILED_TO_SEND_NICKNAME_TO_HOST), - sizeof(msg)-1); + strlcpy(msg, msg_hash_to_str(MSG_FAILED_TO_SEND_NICKNAME_TO_HOST), + sizeof(msg)); goto error; } } @@ -279,12 +280,12 @@ bool netplay_handshake(netplay_t *netplay) if (!netplay_get_nickname(netplay, netplay->fd)) { if (is_server) - strncpy(msg, msg_hash_to_str(MSG_FAILED_TO_GET_NICKNAME_FROM_CLIENT), - sizeof(msg)-1); + strlcpy(msg, msg_hash_to_str(MSG_FAILED_TO_GET_NICKNAME_FROM_CLIENT), + sizeof(msg)); else - strncpy(msg, + strlcpy(msg, msg_hash_to_str(MSG_FAILED_TO_RECEIVE_NICKNAME_FROM_HOST), - sizeof(msg)-1); + sizeof(msg)); goto error; }