From: Nick Mathewson Date: Wed, 8 Jan 2014 16:51:22 +0000 (-0500) Subject: Fix a pile of coverity warnings in the unit tests X-Git-Tag: release-2.1.4-alpha~49 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=867f401f9bdb5d0f1dae6d5d7b96719ca0ef7600;p=libevent Fix a pile of coverity warnings in the unit tests 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. --- diff --git a/test/regress.c b/test/regress.c index fb8a5359..e288d46b 100644 --- a/test/regress.c +++ b/test/regress.c @@ -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 diff --git a/test/regress_buffer.c b/test/regress_buffer.c index ab11de94..3be55748 100644 --- a/test/regress_buffer.c +++ b/test/regress_buffer.c @@ -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"); diff --git a/test/regress_bufferevent.c b/test/regress_bufferevent.c index a6a27752..6799f825 100644 --- a/test/regress_bufferevent.c +++ b/test/regress_bufferevent.c @@ -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) diff --git a/test/regress_http.c b/test/regress_http.c index 9404d8d6..66f9a959 100644 --- a/test/regress_http.c +++ b/test/regress_http.c @@ -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); diff --git a/test/regress_rpc.c b/test/regress_rpc.c index 0a5c7a3f..01a058cb 100644 --- a/test/regress_rpc.c +++ b/test/regress_rpc.c @@ -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");