Commit 1f4a6986 authored by Dominik Charousset's avatar Dominik Charousset

Protect against self-assignment, close #1169

parent f71e95d7
...@@ -39,7 +39,9 @@ error::error(const error& x) : data_(x ? new data(*x.data_) : nullptr) { ...@@ -39,7 +39,9 @@ error::error(const error& x) : data_(x ? new data(*x.data_) : nullptr) {
} }
error& error::operator=(const error& x) { error& error::operator=(const error& x) {
if (x) { if (this == &x) {
// nop
} else if (x) {
if (data_ == nullptr) if (data_ == nullptr)
data_.reset(new data(*x.data_)); data_.reset(new data(*x.data_));
else else
......
...@@ -131,15 +131,17 @@ bool load_data(Deserializer& source, message::data_ptr& data) { ...@@ -131,15 +131,17 @@ bool load_data(Deserializer& source, message::data_ptr& data) {
swap(meta, other.meta); swap(meta, other.meta);
} }
object_ptr& operator=(object_ptr&& other) noexcept { object_ptr& operator=(object_ptr&& other) noexcept {
if (obj) { if (this != &other) {
meta->destroy(obj); if (obj) {
free(obj); meta->destroy(obj);
obj = nullptr; free(obj);
meta = nullptr; obj = nullptr;
meta = nullptr;
}
using std::swap;
swap(obj, other.obj);
swap(meta, other.meta);
} }
using std::swap;
swap(obj, other.obj);
swap(meta, other.meta);
return *this; return *this;
} }
~object_ptr() noexcept { ~object_ptr() noexcept {
......
...@@ -62,9 +62,11 @@ ip_endpoint::ip_endpoint(const ip_endpoint& other) { ...@@ -62,9 +62,11 @@ ip_endpoint::ip_endpoint(const ip_endpoint& other) {
} }
ip_endpoint& ip_endpoint::operator=(const ip_endpoint& other) { ip_endpoint& ip_endpoint::operator=(const ip_endpoint& other) {
ptr_.reset(new ip_endpoint::impl); if (this != &other) {
memcpy(address(), other.caddress(), sizeof(sockaddr_storage)); ptr_.reset(new ip_endpoint::impl);
*length() = *other.clength(); memcpy(address(), other.caddress(), sizeof(sockaddr_storage));
*length() = *other.clength();
}
return *this; return *this;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment