From 637d17a1b975ad33334c64c5ce8d0a19898e5c14 Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Tue, 5 Apr 2011 16:32:39 -0400 Subject: [PATCH] Check for allocation failures in apply_socktype_protocol_hack --- evutil.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/evutil.c b/evutil.c index 926ebc47..0f376e09 100644 --- a/evutil.c +++ b/evutil.c @@ -1124,7 +1124,7 @@ apply_numeric_port_hack(int port, struct evutil_addrinfo **ai) } } -static void +static int apply_socktype_protocol_hack(struct evutil_addrinfo *ai) { struct evutil_addrinfo *ai_new; @@ -1133,6 +1133,8 @@ apply_socktype_protocol_hack(struct evutil_addrinfo *ai) if (ai->ai_socktype || ai->ai_protocol) continue; ai_new = mm_malloc(sizeof(*ai_new)); + if (!ai_new) + return -1; memcpy(ai_new, ai, sizeof(*ai_new)); ai->ai_socktype = SOCK_STREAM; ai->ai_protocol = IPPROTO_TCP; @@ -1142,6 +1144,7 @@ apply_socktype_protocol_hack(struct evutil_addrinfo *ai) ai_new->ai_next = ai->ai_next; ai->ai_next = ai_new; } + return 0; } #endif @@ -1232,7 +1235,11 @@ evutil_getaddrinfo(const char *nodename, const char *servname, apply_numeric_port_hack(portnum, res); if (need_socktype_protocol_hack()) { - apply_socktype_protocol_hack(*res); + if (apply_socktype_protocol_hack(*res) < 0) { + evutil_freeaddrinfo(*res); + *res = NULL; + return EVUTIL_EAI_MEMORY; + } } return err; #else