Skip to content

Commit

Permalink
test: fix use-after-free in timertest
Browse files Browse the repository at this point in the history
There is a bug in the test, which sometimes causes a use-after-free to be
reported by the sanitizer in the debug build. The problem is that the
callback is an std::function held inside the t1 object, and when the
callback deletes t1, it also deletes the std::function being executed.

In commit 902d5b2 we had exactly the same bug in the sleep() function,
and the same fix works here too: free t1 only after the future has
returned, since by that time we know the callback is done.

Fixes #77

Tested-by: Vlad Zolotarov <[email protected]>
Signed-off-by: Nadav Har'El <[email protected]>
  • Loading branch information
nyh authored and avikivity committed Nov 23, 2015
1 parent 84cb6df commit aff91d2
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions tests/timertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ struct timer_test {
t1.arm(100ms);
t1.cancel();

t1.set_callback([this, t1 = &t1] { OK(); pr2.set_value(); delete t1; });
t1.set_callback([this] { OK(); pr2.set_value(); });
t1.arm(100ms);
return pr2.get_future();
return pr2.get_future().then([&t1] { delete &t1; });
}
};

Expand Down

0 comments on commit aff91d2

Please sign in to comment.