Skip to content
This repository has been archived by the owner on Jul 24, 2024. It is now read-only.

Commit

Permalink
Fix heap-use-after-free in Parser error handling
Browse files Browse the repository at this point in the history
Fixes #2643
  • Loading branch information
glebm authored and xzyfer committed Nov 23, 2018
1 parent 122d9f3 commit 930857c
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/error_handling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ namespace Sass {
prefix("Error"), pstate(pstate), traces(traces)
{ }

InvalidSass::InvalidSass(ParserState pstate, Backtraces traces, std::string msg)
: Base(pstate, msg, traces)
InvalidSass::InvalidSass(ParserState pstate, Backtraces traces, std::string msg, char* owned_src)
: Base(pstate, msg, traces), owned_src(owned_src)
{ }


Expand Down
5 changes: 3 additions & 2 deletions src/error_handling.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ namespace Sass {

class InvalidSass : public Base {
public:
InvalidSass(ParserState pstate, Backtraces traces, std::string msg);
virtual ~InvalidSass() throw() {};
InvalidSass(ParserState pstate, Backtraces traces, std::string msg, char* owned_src = nullptr);
virtual ~InvalidSass() throw() { sass_free_memory(owned_src); };
char *owned_src;
};

class InvalidParent : public Base {
Expand Down
5 changes: 4 additions & 1 deletion src/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3054,8 +3054,11 @@ namespace Sass {
{
Position p(pos.line ? pos : before_token);
ParserState pstate(path, source, p, Offset(0, 0));
// `pstate.src` may not outlive stack unwind so we must copy it.
char *src_copy = sass_copy_c_string(pstate.src);
pstate.src = src_copy;
traces.push_back(Backtrace(pstate));
throw Exception::InvalidSass(pstate, traces, msg);
throw Exception::InvalidSass(pstate, traces, msg, src_copy);
}

void Parser::error(std::string msg)
Expand Down

0 comments on commit 930857c

Please sign in to comment.