Skip to content

Commit 648f763

Browse files
mgreterxzyfer
authored andcommitted
Fix memory corruption on error in parse_selector_schema
1 parent eb32404 commit 648f763

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

src/eval.cpp

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1783,11 +1783,28 @@ namespace Sass {
17831783
Expression_Obj sel = s->contents()->perform(this);
17841784
std::string result_str(sel->to_string(ctx.c_options));
17851785
result_str = unquote(Util::rtrim(result_str));
1786-
Parser p = Parser::from_c_str(result_str.c_str(), ctx, s->pstate());
1786+
char* temp_cstr = sass_copy_c_string(result_str.c_str());
1787+
ctx.strings.push_back(temp_cstr); // attach to context
1788+
Parser p = Parser::from_c_str(temp_cstr, ctx, s->pstate());
17871789
p.last_media_block = s->media_block();
17881790
// a selector schema may or may not connect to parent?
17891791
bool chroot = s->connect_parent() == false;
17901792
Selector_List_Obj sl = p.parse_selector_list(chroot);
1793+
auto vec_str_rend = ctx.strings.rend();
1794+
auto vec_str_rbegin = ctx.strings.rbegin();
1795+
// remove the first item searching from the back
1796+
// we cannot assume our item is still the last one
1797+
// order is not important, so we can optimize this
1798+
auto it = std::find(vec_str_rbegin, vec_str_rend, temp_cstr);
1799+
// undefined behavior if not found!
1800+
if (it != vec_str_rend) {
1801+
// overwrite with last item
1802+
*it = ctx.strings.back();
1803+
// remove last one from vector
1804+
ctx.strings.pop_back();
1805+
// free temporary copy
1806+
free(temp_cstr);
1807+
}
17911808
flag_is_in_selector_schema.reset();
17921809
return operator()(sl);
17931810
}

src/file.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@ namespace Sass {
415415
file.seekg(0, std::ios::beg);
416416
file.read(contents, size);
417417
contents[size+0] = '\0';
418-
contents[size+0] = '\0';
418+
contents[size+1] = '\0';
419419
file.close();
420420
}
421421
#endif

0 commit comments

Comments
 (0)