From 84eaca254ca726531def3569c990089b3154e640 Mon Sep 17 00:00:00 2001 From: Gleb Mazovetskiy Date: Fri, 23 Nov 2018 01:19:36 +0000 Subject: [PATCH] Add a copy constructor to InvalidSass exception The copy constructor transfers the ownership of `owned_src` from `rhs` to `lhs`. Otherwise, `owned_src` may be destroyed too early and more than once. In the libsass codebase, this copy can only happen on `throw`. Modern compilers elide such copies. In our CI, only VS 2013 does not. --- src/error_handling.hpp | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/error_handling.hpp b/src/error_handling.hpp index d5433bf63..5ba036aaa 100644 --- a/src/error_handling.hpp +++ b/src/error_handling.hpp @@ -37,6 +37,17 @@ namespace Sass { class InvalidSass : public Base { public: + InvalidSass(InvalidSass& other) : Base(other), owned_src(other.owned_src) { + // Assumes that `this` will outlive `other`. + other.owned_src = nullptr; + } + + // Required because the copy constructor's argument is not const. + // Can't use `std::move` here because we build on Visual Studio 2013. + InvalidSass(InvalidSass &&other) : Base(other), owned_src(other.owned_src) { + other.owned_src = nullptr; + } + InvalidSass(ParserState pstate, Backtraces traces, std::string msg, char* owned_src = nullptr); virtual ~InvalidSass() throw() { sass_free_memory(owned_src); }; char *owned_src;