Skip to content

Commit

Permalink
Mark destructor noexcept and satisfy rule-of-five
Browse files Browse the repository at this point in the history
Follow the RAII paradigm and define all 5 special class function if any
of them has a non-default implementation. Avoids duplicate destruction
or other unwanted effects.
  • Loading branch information
bad code committed Sep 22, 2024
1 parent 36b438b commit 4b3410b
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 6 deletions.
12 changes: 10 additions & 2 deletions src/btop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -468,10 +468,14 @@ namespace Runner {
pthread_mutex_init(&pt_mutex, nullptr);
status = pthread_mutex_lock(&pt_mutex);
}
~thread_lock() {
~thread_lock() noexcept {
if (status == 0)
pthread_mutex_unlock(&pt_mutex);
}
thread_lock(const thread_lock& other) = delete;
thread_lock& operator=(const thread_lock& other) = delete;
thread_lock(thread_lock&& other) = delete;
thread_lock& operator=(thread_lock&& other) = delete;
};

//* Wrapper for raising privileges when using SUID bit
Expand All @@ -482,10 +486,14 @@ namespace Runner {
if (Global::real_uid != Global::set_uid)
this->status = seteuid(Global::set_uid);
}
~gain_priv() {
~gain_priv() noexcept {
if (status == 0)
status = seteuid(Global::real_uid);
}
gain_priv(const gain_priv& other) = delete;
gain_priv& operator=(const gain_priv& other) = delete;
gain_priv(gain_priv&& other) = delete;
gain_priv& operator=(gain_priv&& other) = delete;
};

string output;
Expand Down
6 changes: 5 additions & 1 deletion src/btop_shared.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,11 @@ namespace Net {
int status;
public:
IfAddrsPtr() { status = getifaddrs(&ifaddr); }
~IfAddrsPtr() { freeifaddrs(ifaddr); }
~IfAddrsPtr() noexcept { freeifaddrs(ifaddr); }
IfAddrsPtr(const IfAddrsPtr &) = delete;
IfAddrsPtr& operator=(IfAddrsPtr& other) = delete;
IfAddrsPtr(IfAddrsPtr &&) = delete;
IfAddrsPtr& operator=(IfAddrsPtr&& other) = delete;
[[nodiscard]] constexpr auto operator()() -> struct ifaddrs* { return ifaddr; }
[[nodiscard]] constexpr auto get() -> struct ifaddrs* { return ifaddr; }
[[nodiscard]] constexpr auto get_status() const noexcept -> int { return status; };
Expand Down
8 changes: 6 additions & 2 deletions src/btop_tools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,7 @@ namespace Tools {
else this->atom.store(true);
}

atomic_lock::~atomic_lock() {
atomic_lock::~atomic_lock() noexcept {
this->atom.store(false);
}

Expand Down Expand Up @@ -654,11 +654,15 @@ namespace Logger {
this->status = seteuid(Global::real_uid);
}
}
~lose_priv() {
~lose_priv() noexcept {
if (status == 0) {
status = seteuid(Global::set_uid);
}
}
lose_priv(const lose_priv& other) = delete;
lose_priv& operator=(const lose_priv& other) = delete;
lose_priv(lose_priv&& other) = delete;
lose_priv& operator=(lose_priv&& other) = delete;
};

void set(const string& level) {
Expand Down
10 changes: 9 additions & 1 deletion src/btop_tools.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,11 @@ namespace Tools {
bool not_true{};
public:
explicit atomic_lock(atomic<bool>& atom, bool wait = false);
~atomic_lock();
~atomic_lock() noexcept;
atomic_lock(const atomic_lock& other) = delete;
atomic_lock& operator=(const atomic_lock& other) = delete;
atomic_lock(atomic_lock&& other) = delete;
atomic_lock& operator=(atomic_lock&& other) = delete;
};

//* Read a complete file and return as a string
Expand Down Expand Up @@ -440,6 +444,10 @@ namespace Tools {
DebugTimer() = default;
explicit DebugTimer(const string name, bool start = true, bool delayed_report = true);
~DebugTimer();
DebugTimer(const DebugTimer& other) = delete;
DebugTimer& operator=(const DebugTimer& other) = delete;
DebugTimer(DebugTimer&& other) = delete;
DebugTimer& operator=(DebugTimer&& other) = delete;

void start();
void stop(bool report = true);
Expand Down

0 comments on commit 4b3410b

Please sign in to comment.