From f8a57ef690ce4af7aecbf81371e4da5daa279d4f Mon Sep 17 00:00:00 2001 From: Themaister Date: Sun, 23 Sep 2012 10:53:19 +0200 Subject: [PATCH] Fix crash in Windows when UDP CMD was disabled. --- command.c | 62 ++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 41 insertions(+), 21 deletions(-) diff --git a/command.c b/command.c index 651181f655..26a4059c8a 100644 --- a/command.c +++ b/command.c @@ -60,20 +60,14 @@ static bool socket_nonblock(int fd) #endif } -rarch_cmd_t *rarch_cmd_new(bool stdin_enable, bool network_enable, uint16_t port) -{ - rarch_cmd_t *handle = (rarch_cmd_t*)calloc(1, sizeof(*handle)); - if (!handle) - return NULL; - #ifdef HAVE_NETWORK_CMD - if (network_enable && !netplay_init_network()) - return NULL; +static bool cmd_init_network(rarch_cmd_t *handle, uint16_t port) +{ + if (!netplay_init_network()) + return false; RARCH_LOG("Bringing up command interface on port %hu.\n", (unsigned short)port); - handle->net_fd = -1; - struct addrinfo hints, *res = NULL; memset(&hints, 0, sizeof(hints)); #if defined(_WIN32) || defined(HAVE_SOCKET_LEGACY) @@ -104,31 +98,57 @@ rarch_cmd_t *rarch_cmd_new(bool stdin_enable, bool network_enable, uint16_t port RARCH_ERR("Failed to bind socket.\n"); goto error; } + + freeaddrinfo(res); + return true; + +error: + if (res) + freeaddrinfo(res); + return false; +} +#endif + +#ifdef HAVE_STDIN_CMD +static bool cmd_init_stdin(rarch_cmd_t *handle) +{ +#ifndef _WIN32 + if (stdin_enable && !socket_nonblock(STDIN_FILENO)) + return false; +#endif + + handle->stdin_enable = true; + return true; +} +#endif + +rarch_cmd_t *rarch_cmd_new(bool stdin_enable, bool network_enable, uint16_t port) +{ + rarch_cmd_t *handle = (rarch_cmd_t*)calloc(1, sizeof(*handle)); + if (!handle) + return NULL; + + handle->net_fd = -1; + handle->stdin_enable = stdin_enable; + +#ifdef HAVE_NETWORK_CMD + if (network_enable && !cmd_init_network(handle, port)) + goto error; #else (void)network_enable; (void)port; #endif #ifdef HAVE_STDIN_CMD -#ifndef _WIN32 - if (stdin_enable && !socket_nonblock(STDIN_FILENO)) + if (stdin_enable && !cmd_init_stdin(handle)) goto error; -#endif - handle->stdin_enable = stdin_enable; #else (void)stdin_enable; #endif -#ifdef HAVE_NETWORK_CMD - freeaddrinfo(res); -#endif return handle; error: -#ifdef HAVE_NETWORK_CMD - if (res) - freeaddrinfo(res); -#endif rarch_cmd_free(handle); return NULL; }