From d15b96b498d7cad51a0cb1dd949f70a04af246e9 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Sun, 31 Oct 2021 19:11:04 +0200 Subject: [PATCH] Check for self-assignment in non-trivial assignment operators --- source/core/refptr.h | 6 ++++++ source/core/variant.h | 3 +++ source/fs/stat.cpp | 3 +++ source/strings/format.cpp | 3 +++ 4 files changed, 15 insertions(+) diff --git a/source/core/refptr.h b/source/core/refptr.h index 34560ec..79401dc 100644 --- a/source/core/refptr.h +++ b/source/core/refptr.h @@ -151,6 +151,9 @@ template template RefPtr &RefPtr::assign(const RefPtr &p) { + if(static_cast(&p)==this) + return *this; + decref(); data = p.data; counts = p.counts; @@ -195,6 +198,9 @@ template template WeakPtr &WeakPtr::assign(const WeakPtr &p) { + if(&p==this) + return *this; + decref(); data = p.get(); counts = (data ? p.counts : nullptr); diff --git a/source/core/variant.h b/source/core/variant.h index 443befb..338ec90 100644 --- a/source/core/variant.h +++ b/source/core/variant.h @@ -69,6 +69,9 @@ public: Variant &operator=(const Variant &v) { + if(&v==this) + return *this; + delete store; store = (v.store ? v.store->clone() : nullptr); return *this; diff --git a/source/fs/stat.cpp b/source/fs/stat.cpp index 773cdef..e296ead 100644 --- a/source/fs/stat.cpp +++ b/source/fs/stat.cpp @@ -19,6 +19,9 @@ Stat::Stat(const Stat &other): Stat &Stat::operator=(const Stat &other) { + if(&other==this) + return *this; + exists = other.exists; type = other.type; size = other.size; diff --git a/source/strings/format.cpp b/source/strings/format.cpp index d2065e9..42926e1 100644 --- a/source/strings/format.cpp +++ b/source/strings/format.cpp @@ -19,6 +19,9 @@ Formatter::Formatter(const Formatter &other): Formatter &Formatter::operator=(const Formatter &other) { + if(&other==this) + return *this; + fmt = other.fmt; pos = fmt.begin()+(other.pos-other.fmt.begin()); result = other.result; -- 2.45.2