Skip to content

Commit df79b23

Browse files
committed
Fix some segfaults caused by the parser being too forgiving (#2367)
* Make whitespace trimming in parser error messages optional Forcing whitespace trimming makes it impossible to match some Ruby Sass error messages. * Fix segault when support conditions is missing Originally reported by @MrTuxracer * Fix segault when while predicate is missing Originally reported by @MrTuxracer
1 parent 1687cbb commit df79b23

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

src/parser.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2118,6 +2118,10 @@ namespace Sass {
21182118
While_Obj call = SASS_MEMORY_NEW(While, pstate, 0, 0);
21192119
// parse mandatory predicate
21202120
Expression_Obj predicate = parse_list();
2121+
List_Obj l = Cast<List>(predicate);
2122+
if (!predicate || (l && !l->length())) {
2123+
css_error("Invalid CSS", " after ", ": expected expression (e.g. 1px, bold), was ", false);
2124+
}
21212125
call->predicate(predicate);
21222126
// parse mandatory block
21232127
call->block(parse_block(root));
@@ -2209,6 +2213,9 @@ namespace Sass {
22092213
Supports_Block_Obj Parser::parse_supports_directive()
22102214
{
22112215
Supports_Condition_Obj cond = parse_supports_condition();
2216+
if (!cond) {
2217+
css_error("Invalid CSS", " after ", ": expected @supports condition (e.g. (display: flexbox)), was ", false);
2218+
}
22122219
// create the ast node object for the support queries
22132220
Supports_Block_Obj query = SASS_MEMORY_NEW(Supports_Block, pstate, cond);
22142221
// additional block is mandatory
@@ -2851,7 +2858,7 @@ namespace Sass {
28512858
}
28522859

28532860
// print a css parsing error with actual context information from parsed source
2854-
void Parser::css_error(const std::string& msg, const std::string& prefix, const std::string& middle)
2861+
void Parser::css_error(const std::string& msg, const std::string& prefix, const std::string& middle, const bool trim)
28552862
{
28562863
int max_len = 18;
28572864
const char* end = this->end;
@@ -2863,7 +2870,7 @@ namespace Sass {
28632870
utf8::prior(last_pos, source);
28642871
}
28652872
// backup position to last significant char
2866-
while (last_pos > source && last_pos < end) {
2873+
while (trim && last_pos > source && last_pos < end) {
28672874
if (!Prelexer::is_space(*last_pos)) break;
28682875
utf8::prior(last_pos, source);
28692876
}

src/parser.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,8 @@ namespace Sass {
227227
// text before and in the middle are configurable
228228
void css_error(const std::string& msg,
229229
const std::string& prefix = " after ",
230-
const std::string& middle = ", was: ");
230+
const std::string& middle = ", was: ",
231+
const bool trim = true);
231232
void read_bom();
232233

233234
Block_Obj parse();

0 commit comments

Comments
 (0)