Skip to content

Commit

Permalink
Fix memory leak
Browse files Browse the repository at this point in the history
  • Loading branch information
marek22k committed Mar 13, 2024
1 parent 3a1be57 commit 19e7e44
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 27 deletions.
40 changes: 16 additions & 24 deletions bindings/cpp/tuntap++.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,79 +7,71 @@
namespace tuntap {

tuntap::tuntap(int mode, int id)
: _dev{::tuntap_init()}, _started{true}
: _dev{::tuntap_init()}
{
if (mode != TUNTAP_MODE_ETHERNET && mode != TUNTAP_MODE_TUNNEL) {
throw std::invalid_argument("Unknown tuntap mode");
}
if (id < 0 || id > TUNTAP_ID_MAX) {
throw std::invalid_argument("Tunnel ID is invalid");
}
if (::tuntap_start(_dev, mode, id)) {
if (::tuntap_start(_dev.get(), mode, id)) {
throw std::runtime_error("tuntap_start failed");
}
}

tuntap::~tuntap()
{
if (_started) {
::tuntap_destroy(_dev);
}
}

tuntap::tuntap(tuntap &&t) noexcept
: _dev(nullptr)
: _dev()
{
std::swap(t._dev, this->_dev);
t._dev.swap(this->_dev);
}

void
tuntap::release() noexcept
{
::tuntap_release(_dev);
_started = false;
_dev.release();
}

std::string
tuntap::name() const noexcept
{
return std::string(::tuntap_get_ifname(_dev));
return std::string(::tuntap_get_ifname(_dev.get()));
}

void
tuntap::name(std::string const &s)
{
if (::tuntap_set_ifname(_dev, s.c_str())) {
if (::tuntap_set_ifname(_dev.get(), s.c_str())) {
throw std::runtime_error("Failed to set ifname");
}
}

t_tun
tuntap::native_handle() const noexcept
{
return ::tuntap_get_fd(this->_dev);
return ::tuntap_get_fd(_dev.get());
}

void
tuntap::up()
{
if (::tuntap_up(_dev)) {
if (::tuntap_up(_dev.get())) {
throw std::runtime_error("Failed to bring up tuntap device");
}
}

void
tuntap::down()
{
if (::tuntap_down(_dev)) {
if (::tuntap_down(_dev.get())) {
throw std::runtime_error("Failed to bring down tuntap device");
}
}

int
tuntap::mtu() const noexcept
{
return ::tuntap_get_mtu(_dev);
return ::tuntap_get_mtu(_dev.get());
}

void
Expand All @@ -88,7 +80,7 @@ tuntap::mtu(int m)
if (m < 1 || m > 65535) {
throw std::invalid_argument("Invalid mtu");
}
if (::tuntap_set_mtu(_dev, m)) {
if (::tuntap_set_mtu(_dev.get(), m)) {
throw std::runtime_error("Failed to set mtu for tuntap device");
}
}
Expand All @@ -99,27 +91,27 @@ tuntap::ip(std::string const &s, int netmask)
if (netmask > 128) {
throw std::invalid_argument("Invalid netmask");
}
if (::tuntap_set_ip(_dev, s.c_str(), netmask)) {
if (::tuntap_set_ip(_dev.get(), s.c_str(), netmask)) {
throw std::runtime_error("Failed to set ip for tuntap device");
}
}

int
tuntap::read(void *buf, size_t len) noexcept
{
return ::tuntap_read(_dev, buf, len);
return ::tuntap_read(_dev.get(), buf, len);
}

int
tuntap::write(void *buf, size_t len) noexcept
{
return ::tuntap_write(_dev, buf, len);
return ::tuntap_write(_dev.get(), buf, len);
}

void
tuntap::nonblocking(bool b)
{
if (::tuntap_set_nonblocking(_dev, int(b))) {
if (::tuntap_set_nonblocking(_dev.get(), static_cast<int>(b))) {
throw std::runtime_error("Failed to change non-blocking state for tuntap device");
}
}
Expand Down
12 changes: 9 additions & 3 deletions bindings/cpp/tuntap++.hh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#define LIBTUNTAP_ALY0MA60

#include <string>
#include <memory>

#include <tuntap.h>

Expand All @@ -12,7 +13,6 @@ class tuntap
{
public:
tuntap(int, int = TUNTAP_ID_ANY);
~tuntap();
tuntap(tuntap const &) = delete;
tuntap & operator = (tuntap const &) = delete;
tuntap(tuntap &&) noexcept;
Expand All @@ -37,8 +37,14 @@ class tuntap
void release() noexcept;
void nonblocking(bool);
private:
struct device* _dev;
bool _started;
class TunTapDestroyer final {
public:
void operator()(device * dev) const noexcept {
if (dev)
::tuntap_destroy(dev);
}
};
std::unique_ptr<device, TunTapDestroyer> _dev;
};

} /* tuntap */
Expand Down

0 comments on commit 19e7e44

Please sign in to comment.