mfontanini / libtins

High-level, multiplatform C++ network packet sniffing and crafting library.
http://libtins.github.io/
BSD 2-Clause "Simplified" License
1.91k stars 375 forks source link

Memory leak in active tests #339

Closed m-peko closed 5 years ago

m-peko commented 5 years ago

Hi there,

Currently, I am making some performance tests in this project and, while doing so, I ran into following lines of code in libtins/tests/active_tests/include/active_test_runner.h:

template <typename T>
void ActiveTestRunner::add_test() {
    tests_.emplace_back(new T(packet_sender_, configuration_));
}

I think they are not safe because they would create memory leak. The documentation says that if an exception is thrown, the call to emplace has no effect - which means the plain pointer we have passed in is never deleted.

Therefore, I would suggest changing lines above to something like this:

template <typename T>
void ActiveTestRunner::add_test() {
    tests_.emplace_back(std::unique_ptr<T>(new T(packet_sender_, configuration_)));
}

What do you think? PR is made with this fix.

mfontanini commented 5 years ago

Comments in the PR. Thanks!