From b98d32d0c90864ff60bc689f3bcf738af58ca79c Mon Sep 17 00:00:00 2001 From: Azat Khuzhin Date: Wed, 14 Nov 2018 00:20:20 +0300 Subject: [PATCH] http: improve error path for bufferevent_{setfd,enable,disable}() We have calls to the next functions but do not check return values, though they can be invalid and it is better to show this somehow. Also do bufferevent_setfd() first and only after it bufferevent_enable()/bufferevent_disable() since: a) it is more natural b) it will avoid extra operations c) it will not fail first bufferevent_enable() (this is the case for buffbufferevent_async at least) In this case we could add more information for issues like #709 --- http.c | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/http.c b/http.c index 27d3bd9e..b49bc04c 100644 --- a/http.c +++ b/http.c @@ -1339,7 +1339,8 @@ evhttp_connection_reset_(struct evhttp_connection *evcon) evutil_closesocket(evcon->fd); evcon->fd = -1; } - bufferevent_setfd(evcon->bufev, -1); + err = bufferevent_setfd(evcon->bufev, -1); + EVUTIL_ASSERT(!err && "setfd"); /* we need to clean up any buffered data */ tmp = bufferevent_get_output(evcon->bufev); @@ -1358,7 +1359,6 @@ static void evhttp_connection_start_detectclose(struct evhttp_connection *evcon) { evcon->flags |= EVHTTP_CON_CLOSEDETECT; - bufferevent_enable(evcon->bufev, EV_READ); } @@ -1366,7 +1366,6 @@ static void evhttp_connection_stop_detectclose(struct evhttp_connection *evcon) { evcon->flags &= ~EVHTTP_CON_CLOSEDETECT; - bufferevent_disable(evcon->bufev, EV_READ); } @@ -2558,9 +2557,11 @@ evhttp_connection_connect_(struct evhttp_connection *evcon) return (-1); } - bufferevent_setfd(evcon->bufev, evcon->fd); + if (bufferevent_setfd(evcon->bufev, evcon->fd)) + return (-1); } else { - bufferevent_setfd(evcon->bufev, -1); + if (bufferevent_setfd(evcon->bufev, -1)) + return (-1); } /* Set up a callback for successful connection setup */ @@ -2576,7 +2577,8 @@ evhttp_connection_connect_(struct evhttp_connection *evcon) bufferevent_set_timeouts(evcon->bufev, &evcon->timeout, &evcon->timeout); } /* make sure that we get a write callback */ - bufferevent_enable(evcon->bufev, EV_WRITE); + if (bufferevent_enable(evcon->bufev, EV_WRITE)) + return (-1); evcon->state = EVCON_CONNECTING; @@ -4241,12 +4243,19 @@ evhttp_get_request_connection( evcon->fd = fd; - bufferevent_enable(evcon->bufev, EV_READ); - bufferevent_disable(evcon->bufev, EV_WRITE); - bufferevent_setfd(evcon->bufev, fd); + if (bufferevent_setfd(evcon->bufev, fd)) + goto err; + if (bufferevent_enable(evcon->bufev, EV_READ)) + goto err; + if (bufferevent_disable(evcon->bufev, EV_WRITE)) + goto err; bufferevent_socket_set_conn_address_(evcon->bufev, sa, salen); return (evcon); + +err: + evhttp_connection_free(evcon); + return (NULL); } static int -- 2.40.0