]> git.tdb.fi Git - libs/core.git/commitdiff
Check for self-assignment in non-trivial assignment operators
authorMikko Rasa <tdb@tdb.fi>
Sun, 31 Oct 2021 17:11:04 +0000 (19:11 +0200)
committerMikko Rasa <tdb@tdb.fi>
Sun, 31 Oct 2021 19:35:23 +0000 (21:35 +0200)
source/core/refptr.h
source/core/variant.h
source/fs/stat.cpp
source/strings/format.cpp

index 34560ec0389beea43bbfb66fe2db1bd111bb954a..79401dce5be40094a9f55212c0c3fb4abd8f7dc9 100644 (file)
@@ -151,6 +151,9 @@ template<typename T>
 template<typename U>
 RefPtr<T> &RefPtr<T>::assign(const RefPtr<U> &p)
 {
+       if(static_cast<const void *>(&p)==this)
+               return *this;
+
        decref();
        data = p.data;
        counts = p.counts;
@@ -195,6 +198,9 @@ template<typename T>
 template<typename U>
 WeakPtr<T> &WeakPtr<T>::assign(const WeakPtr<U> &p)
 {
+       if(&p==this)
+               return *this;
+
        decref();
        data = p.get();
        counts = (data ? p.counts : nullptr);
index 443befb481620b4242e178a4b377192179af1adc..338ec90865d5a8dc0dc269e96f73577b1c42c04c 100644 (file)
@@ -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;
index 773cdefbf8743ce08b6815d6a5115030944b46d1..e296ead3816b9317ecd44f0bc82bf6fbd12cbbed 100644 (file)
@@ -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;
index d2065e91cad058cd74f63fa5a34dcec45f258cde..42926e158d593a8a5d84ffb88dfcdddce26a80d5 100644 (file)
@@ -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;