Skip to content

Commit 84eaca2

Browse files
glebmxzyfer
authored andcommitted
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.
1 parent 5801404 commit 84eaca2

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

src/error_handling.hpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,17 @@ namespace Sass {
3737

3838
class InvalidSass : public Base {
3939
public:
40+
InvalidSass(InvalidSass& other) : Base(other), owned_src(other.owned_src) {
41+
// Assumes that `this` will outlive `other`.
42+
other.owned_src = nullptr;
43+
}
44+
45+
// Required because the copy constructor's argument is not const.
46+
// Can't use `std::move` here because we build on Visual Studio 2013.
47+
InvalidSass(InvalidSass &&other) : Base(other), owned_src(other.owned_src) {
48+
other.owned_src = nullptr;
49+
}
50+
4051
InvalidSass(ParserState pstate, Backtraces traces, std::string msg, char* owned_src = nullptr);
4152
virtual ~InvalidSass() throw() { sass_free_memory(owned_src); };
4253
char *owned_src;

0 commit comments

Comments
 (0)