Skip to content

Commit 930857c

Browse files
glebmxzyfer
authored andcommitted
Fix heap-use-after-free in Parser error handling
Fixes #2643
1 parent 122d9f3 commit 930857c

File tree

3 files changed

+9
-5
lines changed

3 files changed

+9
-5
lines changed

src/error_handling.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ namespace Sass {
1515
prefix("Error"), pstate(pstate), traces(traces)
1616
{ }
1717

18-
InvalidSass::InvalidSass(ParserState pstate, Backtraces traces, std::string msg)
19-
: Base(pstate, msg, traces)
18+
InvalidSass::InvalidSass(ParserState pstate, Backtraces traces, std::string msg, char* owned_src)
19+
: Base(pstate, msg, traces), owned_src(owned_src)
2020
{ }
2121

2222

src/error_handling.hpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,9 @@ namespace Sass {
3737

3838
class InvalidSass : public Base {
3939
public:
40-
InvalidSass(ParserState pstate, Backtraces traces, std::string msg);
41-
virtual ~InvalidSass() throw() {};
40+
InvalidSass(ParserState pstate, Backtraces traces, std::string msg, char* owned_src = nullptr);
41+
virtual ~InvalidSass() throw() { sass_free_memory(owned_src); };
42+
char *owned_src;
4243
};
4344

4445
class InvalidParent : public Base {

src/parser.cpp

+4-1
Original file line numberDiff line numberDiff line change
@@ -3054,8 +3054,11 @@ namespace Sass {
30543054
{
30553055
Position p(pos.line ? pos : before_token);
30563056
ParserState pstate(path, source, p, Offset(0, 0));
3057+
// `pstate.src` may not outlive stack unwind so we must copy it.
3058+
char *src_copy = sass_copy_c_string(pstate.src);
3059+
pstate.src = src_copy;
30573060
traces.push_back(Backtrace(pstate));
3058-
throw Exception::InvalidSass(pstate, traces, msg);
3061+
throw Exception::InvalidSass(pstate, traces, msg, src_copy);
30593062
}
30603063

30613064
void Parser::error(std::string msg)

0 commit comments

Comments
 (0)