aboutsummaryrefslogtreecommitdiff
path: root/common/span.h
diff options
context:
space:
mode:
authorColin Snover2017-05-24 23:12:23 -0500
committerColin Snover2017-06-08 10:45:55 -0500
commit58c83dcd140137cf5c5808a95e7b0711fa556933 (patch)
tree022ced757e426daa9f9abafd82df6bb84fd261d2 /common/span.h
parente29f60858dc4172f91d235219e76a9e13e4d72f9 (diff)
downloadscummvm-rg350-58c83dcd140137cf5c5808a95e7b0711fa556933.tar.gz
scummvm-rg350-58c83dcd140137cf5c5808a95e7b0711fa556933.tar.bz2
scummvm-rg350-58c83dcd140137cf5c5808a95e7b0711fa556933.zip
COMMON: Make SpanOwner copy assignment make a copy of the owned Span
To move data from one SpanOwner to another, use `moveFrom`. Thanks @waltervn for pointing out the problem.
Diffstat (limited to 'common/span.h')
-rw-r--r--common/span.h31
1 files changed, 23 insertions, 8 deletions
diff --git a/common/span.h b/common/span.h
index 2930003811..e0b1d139ca 100644
--- a/common/span.h
+++ b/common/span.h
@@ -942,20 +942,21 @@ public:
_span.allocateFromSpan(other._span);
}
- /**
- * Transfers ownership of the Span from the other owner to this owner.
- * If this owner already holds another Span, the old Span will be destroyed.
- */
inline SpanOwner &operator=(SpanOwner &other) {
if (this == &other) {
return *this;
}
- if (_span.data()) {
- delete[] const_cast<typename RemoveConst<value_type>::type *>(_span.data());
+ delete[] const_cast<typename RemoveConst<value_type>::type *>(_span.data());
+ _span.clear();
+
+ // Allocating memory when copy-assigning from an unallocated owner
+ // will break the new owner by making it appear allocated even though
+ // it doesn't (and shouldn't) contain data
+ if (other) {
+ _span.allocateFromSpan(other._span);
}
- _span = other._span;
- other.release();
+
return *this;
}
@@ -964,6 +965,20 @@ public:
}
/**
+ * Transfers ownership of the Span from the other owner to this owner.
+ */
+ inline SpanOwner &moveFrom(SpanOwner &other) {
+ if (this == &other) {
+ return *this;
+ }
+
+ delete[] const_cast<typename RemoveConst<value_type>::type *>(_span.data());
+ _span = other._span;
+ other.release();
+ return *this;
+ }
+
+ /**
* Releases the memory owned by this SpanOwner to the caller.
*/
inline pointer release() {