aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Tolnay <dtolnay@gmail.com>2021-01-02 16:47:33 -0800
committerDavid Tolnay <dtolnay@gmail.com>2021-01-02 16:48:50 -0800
commit4ddae80e7d9ef26959067c671ec935e0e64ac498 (patch)
tree9412621f2c6bf7a43289aed38b451629aa3bc429
parent10c48f5904077748f3531a8c7062862cda5e0b49 (diff)
downloadcxx-4ddae80e7d9ef26959067c671ec935e0e64ac498.tar.gz
Allow through bad_alloc from Error(const Error &) and operator=
-rw-r--r--src/cxx.cc18
1 files changed, 13 insertions, 5 deletions
diff --git a/src/cxx.cc b/src/cxx.cc
index 56c36967..9983fbaa 100644
--- a/src/cxx.cc
+++ b/src/cxx.cc
@@ -353,15 +353,21 @@ static_assert(!std::is_same<Vec<std::uint8_t>::const_iterator,
Vec<std::uint8_t>::iterator>::value,
"Vec<T>::const_iterator != Vec<T>::iterator");
+static const char *errorCopy(const char *ptr, std::size_t len) {
+ char *copy = new char[len];
+ std::memcpy(copy, ptr, len);
+ return copy;
+}
+
extern "C" {
const char *cxxbridge1$error(const char *ptr, std::size_t len) noexcept {
- return ptr ? static_cast<char *>(std::memcpy(new char[len], ptr, len))
- : nullptr;
+ return errorCopy(ptr, len);
}
} // extern "C"
Error::Error(const Error &other)
- : std::exception(other), msg(cxxbridge1$error(other.msg, other.len)),
+ : std::exception(other),
+ msg(other.msg ? errorCopy(other.msg, other.len) : nullptr),
len(other.len) {}
Error::Error(Error &&other) noexcept
@@ -377,8 +383,10 @@ Error &Error::operator=(const Error &other) {
std::exception::operator=(other);
delete[] this->msg;
this->msg = nullptr;
- this->msg = cxxbridge1$error(other.msg, other.len);
- this->len = other.len;
+ if (other.msg) {
+ this->msg = errorCopy(other.msg, other.len);
+ this->len = other.len;
+ }
}
return *this;
}