]> granicus.if.org Git - libevent/commitdiff
test: fix memory leaks for https (add BEV_OPT_CLOSE_ON_FREE)
authorAzat Khuzhin <azat@libevent.org>
Thu, 25 Jun 2020 18:56:31 +0000 (21:56 +0300)
committerAzat Khuzhin <azat@libevent.org>
Thu, 25 Jun 2020 19:57:53 +0000 (22:57 +0300)
- http/https_filter_basic
- http/https_filter_chunk_out

test/regress_http.c

index 4353ca5f14fbcb79367f6df81b1f9ea31c92843d..282d6f7c16fdf2806d866ecc3d918b882d7b4e6d 100644 (file)
@@ -496,9 +496,9 @@ http_chunked_cb(struct evhttp_request *req, void *arg)
 }
 
 static struct bufferevent *
-create_bev(struct event_base *base, evutil_socket_t fd, int ssl_mask)
+create_bev(struct event_base *base, evutil_socket_t fd, int ssl_mask, int flags_)
 {
-       int flags = BEV_OPT_DEFER_CALLBACKS;
+       int flags = BEV_OPT_DEFER_CALLBACKS | flags_;
        struct bufferevent *bev = NULL;
 
        if (!ssl_mask) {
@@ -561,7 +561,7 @@ http_basic_test_impl(void *arg, int ssl, const char *request_line)
        fd = http_connect("127.0.0.1", port);
 
        /* Stupid thing to send a request */
-       bev = create_bev(data->base, fd, ssl);
+       bev = create_bev(data->base, fd, ssl, BEV_OPT_CLOSE_ON_FREE);
        bufferevent_setcb(bev, http_readcb, http_half_writecb,
            http_errorcb, data->base);
        out = bufferevent_get_output(bev);
@@ -577,12 +577,11 @@ http_basic_test_impl(void *arg, int ssl, const char *request_line)
 
        /* connect to the second port */
        bufferevent_free(bev);
-       evutil_closesocket(fd);
 
        fd = http_connect("127.0.0.1", port2);
 
        /* Stupid thing to send a request */
-       bev = create_bev(data->base, fd, ssl);
+       bev = create_bev(data->base, fd, ssl, BEV_OPT_CLOSE_ON_FREE);
        bufferevent_setcb(bev, http_readcb, http_writecb,
            http_errorcb, data->base);
        out = bufferevent_get_output(bev);
@@ -599,12 +598,11 @@ http_basic_test_impl(void *arg, int ssl, const char *request_line)
 
        /* Connect to the second port again. This time, send an absolute uri. */
        bufferevent_free(bev);
-       evutil_closesocket(fd);
 
        fd = http_connect("127.0.0.1", port2);
 
        /* Stupid thing to send a request */
-       bev = create_bev(data->base, fd, ssl);
+       bev = create_bev(data->base, fd, ssl, BEV_OPT_CLOSE_ON_FREE);
        bufferevent_setcb(bev, http_readcb, http_writecb,
            http_errorcb, data->base);
 
@@ -3398,7 +3396,7 @@ http_incomplete_test_(struct basic_test_data *data, int use_timeout, int ssl)
        tt_assert(fd != EVUTIL_INVALID_SOCKET);
 
        /* Stupid thing to send a request */
-       bev = create_bev(data->base, fd, ssl);
+       bev = create_bev(data->base, fd, ssl, 0);
        bufferevent_setcb(bev,
            http_incomplete_readcb, http_incomplete_writecb,
            http_incomplete_errorcb, use_timeout ? NULL : &fd);
@@ -3598,7 +3596,7 @@ static void
 http_chunk_out_test_impl(void *arg, int ssl)
 {
        struct basic_test_data *data = arg;
-       struct bufferevent *bev;
+       struct bufferevent *bev = NULL;
        evutil_socket_t fd;
        const char *http_request;
        ev_uint16_t port = 0;
@@ -3615,7 +3613,7 @@ http_chunk_out_test_impl(void *arg, int ssl)
        tt_assert(fd != EVUTIL_INVALID_SOCKET);
 
        /* Stupid thing to send a request */
-       bev = create_bev(data->base, fd, ssl);
+       bev = create_bev(data->base, fd, ssl, BEV_OPT_CLOSE_ON_FREE);
        bufferevent_setcb(bev,
            http_chunked_readcb, http_chunked_writecb,
            http_chunked_errorcb, data->base);
@@ -3633,6 +3631,7 @@ http_chunk_out_test_impl(void *arg, int ssl)
        event_base_dispatch(data->base);
 
        bufferevent_free(bev);
+       bev = NULL;
 
        evutil_gettimeofday(&tv_end, NULL);
        evutil_timersub(&tv_end, &tv_start, &tv_end);
@@ -3642,7 +3641,7 @@ http_chunk_out_test_impl(void *arg, int ssl)
        tt_int_op(test_ok, ==, 2);
 
        /* now try again with the regular connection object */
-       bev = create_bev(data->base, -1, ssl);
+       bev = create_bev(data->base, -1, ssl, BEV_OPT_CLOSE_ON_FREE);
        evcon = evhttp_connection_base_bufferevent_new(
                data->base, NULL, bev, "127.0.0.1", port);
        tt_assert(evcon);
@@ -3650,14 +3649,13 @@ http_chunk_out_test_impl(void *arg, int ssl)
        /* make two requests to check the keepalive behavior */
        for (i = 0; i < 2; i++) {
                test_ok = 0;
-               req = evhttp_request_new(http_chunked_request_done,data->base);
+               req = evhttp_request_new(http_chunked_request_done, data->base);
 
                /* Add the information that we care about */
                evhttp_add_header(evhttp_request_get_output_headers(req), "Host", "somehost");
 
                /* We give ownership of the request to the connection */
-               if (evhttp_make_request(evcon, req,
-                       EVHTTP_REQ_GET, "/chunked") == -1) {
+               if (evhttp_make_request(evcon, req, EVHTTP_REQ_GET, "/chunked") == -1) {
                        tt_abort_msg("Couldn't make request");
                }
 
@@ -3688,7 +3686,7 @@ http_stream_out_test_impl(void *arg, int ssl)
        test_ok = 0;
        exit_base = data->base;
 
-       bev = create_bev(data->base, -1, ssl);
+       bev = create_bev(data->base, -1, ssl, 0);
        evcon = evhttp_connection_base_bufferevent_new(
                data->base, NULL, bev, "127.0.0.1", port);
        tt_assert(evcon);
@@ -3888,7 +3886,7 @@ http_connection_fail_test_impl(void *arg, int ssl)
        /* auto detect a port */
        evhttp_free(http);
 
-       bev = create_bev(data->base, -1, ssl);
+       bev = create_bev(data->base, -1, ssl, 0);
        /* Pick an unroutable address. This administratively scoped multicast
         * address should do when working with TCP. */
        evcon = evhttp_connection_base_bufferevent_new(
@@ -3960,7 +3958,7 @@ http_simple_test_impl(void *arg, int ssl, int dirty, const char *uri)
        exit_base = data->base;
        test_ok = 0;
 
-       bev = create_bev(data->base, -1, ssl);
+       bev = create_bev(data->base, -1, ssl, 0);
 #ifdef EVENT__HAVE_OPENSSL
        bufferevent_openssl_set_allow_dirty_shutdown(bev, dirty);
 #endif
@@ -4007,7 +4005,7 @@ http_connection_retry_test_basic(void *arg, const char *addr, struct evdns_base
        /* auto detect a port */
        evhttp_free(http);
 
-       bev = create_bev(data->base, -1, ssl);
+       bev = create_bev(data->base, -1, ssl, 0);
        evcon = evhttp_connection_base_bufferevent_new(data->base, dns_base, bev, addr, hs.port);
        tt_assert(evcon);
        if (dns_base)
@@ -4795,7 +4793,7 @@ http_write_during_read_test_impl(void *arg, int ssl)
 
        fd = http_connect("127.0.0.1", port);
        tt_assert(fd != EVUTIL_INVALID_SOCKET);
-       bev = create_bev(data->base, fd, 0);
+       bev = create_bev(data->base, fd, 0, 0);
        bufferevent_setcb(bev, NULL, NULL, NULL, data->base);
        bufferevent_disable(bev, EV_READ);
 
@@ -4867,7 +4865,7 @@ static void http_run_bev_request(struct event_base *base, int port,
        tt_assert(fd != EVUTIL_INVALID_SOCKET);
 
        /* Stupid thing to send a request */
-       bev = create_bev(base, fd, 0);
+       bev = create_bev(base, fd, 0, 0);
        bufferevent_setcb(bev, http_readcb, http_writecb,
            http_errorcb, base);
        out = bufferevent_get_output(bev);
@@ -5293,7 +5291,7 @@ http_timeout_read_server_test(void *arg)
        evhttp_set_read_timeout_tv(http, &tv);
 
        fd = http_connect("127.0.0.1", port);
-       bev = create_bev(data->base, fd, 0);
+       bev = create_bev(data->base, fd, 0, 0);
        bufferevent_setcb(bev, http_readcb, http_writecb, http_errorcb, data->base);
        out = bufferevent_get_output(bev);