]> granicus.if.org Git - libevent/commitdiff
Fix a pile of coverity warnings in the unit tests
authorNick Mathewson <nickm@torproject.org>
Wed, 8 Jan 2014 16:51:22 +0000 (11:51 -0500)
committerNick Mathewson <nickm@torproject.org>
Wed, 8 Jan 2014 16:51:22 +0000 (11:51 -0500)
Most of these problems can only trigger when the tests fail, but it's
good hygiene to keep the code warning-free in any case.

test/regress.c
test/regress_buffer.c
test/regress_bufferevent.c
test/regress_http.c
test/regress_rpc.c

index fb8a5359bf3358b21164ec1a22429cf3904b9462..e288d46bda74b706bb321b7217f64816e8a174e0 100644 (file)
@@ -1729,11 +1729,15 @@ static void read_not_timeout_cb(evutil_socket_t fd, short what, void *arg)
 {
        struct read_not_timeout_param *rntp = arg;
        char c;
+       ev_ssize_t n;
        (void) fd; (void) what;
-       (void) read(fd, &c, 1);
+       n = read(fd, &c, 1);
+       tt_int_op(n, ==, 1);
        rntp->events |= what;
        ++rntp->count;
        if(2 == rntp->count) event_del(rntp->ev[0]);
+end:
+       ;
 }
 
 static void
@@ -1935,13 +1939,18 @@ re_add_read_cb(evutil_socket_t fd, short event, void *arg)
        char buf[256];
        struct event *ev_other = arg;
        readd_test_event_last_added = ev_other;
+       ev_ssize_t n_read;
+
+       n_read = read(fd, buf, sizeof(buf));
 
-       if (read(fd, buf, sizeof(buf)) < 0) {
+       if (n_read < 0) {
                tt_fail_perror("read");
+               event_base_loopbreak(event_get_base(ev_other));
+               return;
+       } else {
+               event_add(ev_other, NULL);
+               ++test_ok;
        }
-
-       event_add(ev_other, NULL);
-       ++test_ok;
 }
 
 static void
@@ -3144,8 +3153,13 @@ test_active_by_fd(void *arg)
        ev2 = event_new(base, data->pair[0], EV_WRITE, tabf_cb, &e2);
        ev3 = event_new(base, data->pair[1], EV_READ, tabf_cb, &e3);
        ev4 = event_new(base, data->pair[1], EV_READ, tabf_cb, &e4);
+       tt_assert(ev1);
+       tt_assert(ev2);
+       tt_assert(ev3);
+       tt_assert(ev4);
 #ifndef _WIN32
        evsig = event_new(base, SIGHUP, EV_SIGNAL, tabf_cb, &es);
+       tt_assert(evsig);
        event_add(evsig, &tenmin);
 #endif
 
index ab11de940cb2573506b44d302125304e152333cd..3be557481c7eee1e956780e917151cf85cd068a3 100644 (file)
@@ -888,7 +888,7 @@ test_evbuffer_file_segment_add_cleanup_cb(void* ptr)
        char *tmpfilename = NULL;
        int fd = -1;
        struct evbuffer *evb = NULL;
-       struct evbuffer_file_segment *seg = NULL;
+       struct evbuffer_file_segment *seg = NULL, *segptr;
        char const* arg = "token";
 
        fd = regress_make_tmpfile("file_segment_test_file", 22, &tmpfilename);
@@ -897,7 +897,7 @@ test_evbuffer_file_segment_add_cleanup_cb(void* ptr)
        evb = evbuffer_new();
        tt_assert(evb);
 
-       seg = evbuffer_file_segment_new(fd, 0, -1, 0);
+       segptr = seg = evbuffer_file_segment_new(fd, 0, -1, 0);
        tt_assert(seg);
 
        evbuffer_file_segment_add_cleanup_cb(
@@ -906,29 +906,27 @@ test_evbuffer_file_segment_add_cleanup_cb(void* ptr)
        tt_assert(fd != -1);
 
        tt_assert(evbuffer_add_file_segment(evb, seg, 0, -1)!=-1);
-       
+
        evbuffer_validate(evb);
 
        tt_int_op(file_segment_cleanup_cb_called_count, ==, 0);
        evbuffer_file_segment_free(seg);
+       seg = NULL; /* Prevent double-free. */
 
        tt_int_op(file_segment_cleanup_cb_called_count, ==, 0);
        evbuffer_free(evb);
-       
+       evb = NULL; /* pevent double-free */
+
        tt_int_op(file_segment_cleanup_cb_called_count, ==, 1);
-       tt_assert(file_segment_cleanup_cb_called_with == seg);
+       tt_assert(file_segment_cleanup_cb_called_with == segptr);
        tt_assert(file_segment_cleanup_cb_called_with_flags == 0);
        tt_assert(file_segment_cleanup_cb_called_with_arg == (void*)arg);
-       
-       seg = NULL;
-       evb = NULL;
 
 end:
-       
-       if(evb) 
-         evbuffer_free(evb);
-       if(seg)
-         evbuffer_file_segment_free(seg);
+       if (evb)
+               evbuffer_free(evb);
+       if (seg)
+               evbuffer_file_segment_free(seg);
        if (tmpfilename) {
                unlink(tmpfilename);
                free(tmpfilename);
@@ -1810,6 +1808,7 @@ test_evbuffer_prepend(void *ptr)
        evbuffer_prepend(buf1, "It is no longer true to say ", 28);
        evbuffer_validate(buf1);
        n = evbuffer_remove(buf1, tmp, sizeof(tmp)-1);
+       tt_int_op(n, >=, 0);
        tmp[n]='\0';
        tt_str_op(tmp,==,"It is no longer true to say it has 29 characters");
 
index a6a27752e42b6e7ccfc4fec1f29868dfd12d0100..6799f82543a0da49a23b084fb8285f6cabb1e1ec 100644 (file)
@@ -486,6 +486,7 @@ reader_eventcb(struct bufferevent *bev, short what, void *ctx)
                char buf[512];
                size_t n;
                n = bufferevent_read(bev, buf, sizeof(buf)-1);
+               tt_int_op(n, >=, 0);
                buf[n] = '\0';
                tt_str_op(buf, ==, TEST_STR);
                if (++n_strings_read == 2)
index 9404d8d66f62c287f3d82847042256ee17a76bab..66f9a95912a786e5d9ad2e10dac4b3352259885c 100644 (file)
@@ -599,6 +599,7 @@ http_bad_request_test(void *arg)
 
        /* NULL request test */
        fd = http_connect("127.0.0.1", port);
+       tt_int_op(fd, >=, 0);
 
        /* Stupid thing to send a request */
        bev = bufferevent_socket_new(data->base, fd, 0);
@@ -712,6 +713,7 @@ http_delete_test(void *arg)
        http = http_setup(&port, data->base, 0);
 
        fd = http_connect("127.0.0.1", port);
+       tt_int_op(fd, >=, 0);
 
        /* Stupid thing to send a request */
        bev = bufferevent_socket_new(data->base, fd, 0);
@@ -853,6 +855,7 @@ http_allowed_methods_test(void *arg)
        http = http_setup(&port, data->base, 0);
 
        fd1 = http_connect("127.0.0.1", port);
+       tt_int_op(fd1, >=, 0);
 
        /* GET is out; PATCH is in. */
        evhttp_set_allowed_methods(http, EVHTTP_REQ_PATCH);
@@ -874,6 +877,7 @@ http_allowed_methods_test(void *arg)
        event_base_dispatch(data->base);
 
        fd2 = http_connect("127.0.0.1", port);
+       tt_int_op(fd2, >=, 0);
 
        bev2 = bufferevent_socket_new(data->base, fd2, 0);
        bufferevent_enable(bev2, EV_READ|EV_WRITE);
@@ -891,6 +895,7 @@ http_allowed_methods_test(void *arg)
        event_base_dispatch(data->base);
 
        fd3 = http_connect("127.0.0.1", port);
+       tt_int_op(fd3, >=, 0);
 
        bev3 = bufferevent_socket_new(data->base, fd3, 0);
        bufferevent_enable(bev3, EV_READ|EV_WRITE);
@@ -1855,6 +1860,7 @@ http_failure_test(void *arg)
        http = http_setup(&port, data->base, 0);
 
        fd = http_connect("127.0.0.1", port);
+       tt_int_op(fd, >=, 0);
 
        /* Stupid thing to send a request */
        bev = bufferevent_socket_new(data->base, fd, 0);
@@ -1948,6 +1954,7 @@ http_close_detection_(struct basic_test_data *data, int with_delay)
 
        evcon = evhttp_connection_base_new(data->base, NULL,
            "127.0.0.1", port);
+       tt_assert(evcon);
        evhttp_connection_set_timeout_tv(evcon, &sec_tenth);
 
 
@@ -2617,9 +2624,11 @@ http_base_test(void *ptr)
 
        test_ok = 0;
        base = event_base_new();
+       tt_assert(base);
        http = http_setup(&port, base, 0);
 
        fd = http_connect("127.0.0.1", port);
+       tt_int_op(fd, >=, 0);
 
        /* Stupid thing to send a request */
        bev = bufferevent_socket_new(base, fd, 0);
@@ -2703,6 +2712,7 @@ http_incomplete_test_(struct basic_test_data *data, int use_timeout)
        evhttp_set_timeout(http, 1);
 
        fd = http_connect("127.0.0.1", port);
+       tt_int_op(fd, >=, 0);
 
        /* Stupid thing to send a request */
        bev = bufferevent_socket_new(data->base, fd, 0);
@@ -2765,13 +2775,15 @@ http_chunked_readcb(struct bufferevent *bev, void *arg)
 static void
 http_chunked_errorcb(struct bufferevent *bev, short what, void *arg)
 {
+       struct evhttp_request *req = NULL;
+
        if (!test_ok)
                goto out;
 
        test_ok = -1;
 
        if ((what & BEV_EVENT_EOF) != 0) {
-               struct evhttp_request *req = evhttp_request_new(NULL, NULL);
+               req = evhttp_request_new(NULL, NULL);
                const char *header;
                enum message_read_status done;
 
@@ -2849,11 +2861,12 @@ http_chunked_errorcb(struct bufferevent *bev, short what, void *arg)
                free((void *)header);
 
                test_ok = 2;
-
-               evhttp_request_free(req);
        }
 
 out:
+       if (req)
+               evhttp_request_free(req);
+
        event_base_loopexit(arg, NULL);
 }
 
@@ -3400,10 +3413,15 @@ http_multi_line_header_test(void *arg)
 
        http = http_setup(&port, data->base, 0);
 
+       tt_ptr_op(http, !=, NULL);
+
        fd = http_connect("127.0.0.1", port);
 
+       tt_int_op(fd, !=, -1);
+
        /* Stupid thing to send a request */
        bev = bufferevent_socket_new(data->base, fd, 0);
+       tt_ptr_op(bev, !=, NULL);
        bufferevent_setcb(bev, http_readcb, http_writecb,
            http_errorcb, data->base);
 
index 0a5c7a3f39023b0426b43f62bac465a4797479f4..01a058cbb2b86facf4196f035bc7257eaac3a4cc 100644 (file)
@@ -463,12 +463,14 @@ rpc_basic_client(void)
            != NULL);
 
        pool = rpc_pool_with_connection(port);
+       tt_assert(pool);
 
        assert(evrpc_add_hook(pool, EVRPC_OUTPUT, rpc_hook_add_meta, NULL));
        assert(evrpc_add_hook(pool, EVRPC_INPUT, rpc_hook_remove_header, (void*)"output"));
 
        /* set up the basic message */
        msg = msg_new();
+       tt_assert(msg);
        EVTAG_ASSIGN(msg, from_name, "niels");
        EVTAG_ASSIGN(msg, to_name, "tester");
 
@@ -539,9 +541,11 @@ rpc_basic_queued_client(void)
        rpc_setup(&http, &port, &base);
 
        pool = rpc_pool_with_connection(port);
+       tt_assert(pool);
 
        /* set up the basic message */
        msg = msg_new();
+       tt_assert(msg);
        EVTAG_ASSIGN(msg, from_name, "niels");
        EVTAG_ASSIGN(msg, to_name, "tester");
 
@@ -640,12 +644,13 @@ rpc_basic_client_with_pause(void)
        assert(evrpc_add_hook(base, EVRPC_OUTPUT, rpc_hook_pause, base));
 
        pool = rpc_pool_with_connection(port);
-
+       tt_assert(pool);
        assert(evrpc_add_hook(pool, EVRPC_INPUT, rpc_hook_pause, pool));
        assert(evrpc_add_hook(pool, EVRPC_OUTPUT, rpc_hook_pause, pool));
 
        /* set up the basic message */
        msg = msg_new();
+       tt_assert(msg);
        EVTAG_ASSIGN(msg, from_name, "niels");
        EVTAG_ASSIGN(msg, to_name, "tester");
 
@@ -688,12 +693,14 @@ rpc_client_timeout(void)
        rpc_setup(&http, &port, &base);
 
        pool = rpc_pool_with_connection(port);
+       tt_assert(pool);
 
        /* set the timeout to 1 second. */
        evrpc_pool_set_timeout(pool, 1);
 
        /* set up the basic message */
        msg = msg_new();
+       tt_assert(msg);
        EVTAG_ASSIGN(msg, from_name, "niels");
        EVTAG_ASSIGN(msg, to_name, "tester");