Skip to content

Commit

Permalink
Add a copy constructor to InvalidSass exception
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
glebm authored and xzyfer committed Nov 23, 2018
1 parent 5801404 commit 84eaca2
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/error_handling.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 84eaca2

Please sign in to comment.