Skip to content

Commit ae3f31b

Browse files
Eugene OstroukhovMyles Borins
Eugene Ostroukhov
authored and
Myles Borins
committed
test: fix issues reported by Coverity
Wrapped the timer into class to ensure it is cleaned up properly. PR-URL: #8870 Reviewed-By: Ali Ijaz Sheikh <[email protected]> Reviewed-By: Ben Noordhuis <[email protected]>
1 parent c0f8198 commit ae3f31b

File tree

1 file changed

+29
-27
lines changed

1 file changed

+29
-27
lines changed

test/cctest/test_inspector_socket.cc

+29-27
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,11 @@ static const int MAX_LOOP_ITERATIONS = 10000;
1010

1111
#define SPIN_WHILE(condition) \
1212
{ \
13-
bool timed_out = false; \
14-
uv_timer_t* timer = start_timer(&timed_out); \
15-
while (((condition)) && !timed_out) { \
13+
Timeout timeout(&loop); \
14+
while ((condition) && !timeout.timed_out) { \
1615
uv_run(&loop, UV_RUN_NOWAIT); \
1716
} \
1817
ASSERT_FALSE((condition)); \
19-
cleanup_timer(timer); \
2018
}
2119

2220
static bool connected = false;
@@ -46,32 +44,36 @@ static const char HANDSHAKE_REQ[] = "GET /ws/path HTTP/1.1\r\n"
4644
"Sec-WebSocket-Key: aaa==\r\n"
4745
"Sec-WebSocket-Version: 13\r\n\r\n";
4846

49-
static void dispose_handle(uv_handle_t* handle) {
50-
*static_cast<bool*>(handle->data) = true;
51-
}
52-
53-
static void set_timeout_flag(uv_timer_t* timer) {
54-
*(static_cast<bool*>(timer->data)) = true;
55-
}
47+
class Timeout {
48+
public:
49+
explicit Timeout(uv_loop_t* loop) : timed_out(false), done_(false) {
50+
uv_timer_init(loop, &timer_);
51+
uv_timer_start(&timer_, Timeout::set_flag, 5000, 0);
52+
}
5653

57-
static uv_timer_t* start_timer(bool* flag) {
58-
uv_timer_t* timer = new uv_timer_t();
59-
uv_timer_init(&loop, timer);
60-
timer->data = flag;
61-
uv_timer_start(timer, set_timeout_flag, 5000, 0);
62-
return timer;
63-
}
54+
~Timeout() {
55+
uv_timer_stop(&timer_);
56+
uv_close(reinterpret_cast<uv_handle_t*>(&timer_), mark_done);
57+
while (!done_) {
58+
uv_run(&loop, UV_RUN_NOWAIT);
59+
}
60+
}
61+
bool timed_out;
62+
private:
63+
static void set_flag(uv_timer_t* timer) {
64+
Timeout* t = node::ContainerOf(&Timeout::timer_, timer);
65+
t->timed_out = true;
66+
}
6467

65-
static void cleanup_timer(uv_timer_t* timer) {
66-
bool done = false;
67-
timer->data = &done;
68-
uv_timer_stop(timer);
69-
uv_close(reinterpret_cast<uv_handle_t*>(timer), dispose_handle);
70-
while (!done) {
71-
uv_run(&loop, UV_RUN_NOWAIT);
68+
static void mark_done(uv_handle_t* timer) {
69+
Timeout* t = node::ContainerOf(&Timeout::timer_,
70+
reinterpret_cast<uv_timer_t*>(timer));
71+
t->done_ = true;
7272
}
73-
delete timer;
74-
}
73+
74+
bool done_;
75+
uv_timer_t timer_;
76+
};
7577

7678
static void stop_if_stop_path(enum inspector_handshake_event state,
7779
const std::string& path, bool* cont) {

0 commit comments

Comments
 (0)