Skip to content

Commit 4ccec02

Browse files
authored
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 e14ca2b commit 4ccec02

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
@@ -2142,6 +2142,10 @@ namespace Sass {
21422142
While_Obj call = SASS_MEMORY_NEW(While, pstate, 0, 0);
21432143
// parse mandatory predicate
21442144
Expression_Obj predicate = parse_list();
2145+
List_Obj l = Cast<List>(predicate);
2146+
if (!predicate || (l && !l->length())) {
2147+
css_error("Invalid CSS", " after ", ": expected expression (e.g. 1px, bold), was ", false);
2148+
}
21452149
call->predicate(predicate);
21462150
// parse mandatory block
21472151
call->block(parse_block(root));
@@ -2233,6 +2237,9 @@ namespace Sass {
22332237
Supports_Block_Obj Parser::parse_supports_directive()
22342238
{
22352239
Supports_Condition_Obj cond = parse_supports_condition();
2240+
if (!cond) {
2241+
css_error("Invalid CSS", " after ", ": expected @supports condition (e.g. (display: flexbox)), was ", false);
2242+
}
22362243
// create the ast node object for the support queries
22372244
Supports_Block_Obj query = SASS_MEMORY_NEW(Supports_Block, pstate, cond);
22382245
// additional block is mandatory
@@ -2875,7 +2882,7 @@ namespace Sass {
28752882
}
28762883

28772884
// print a css parsing error with actual context information from parsed source
2878-
void Parser::css_error(const std::string& msg, const std::string& prefix, const std::string& middle)
2885+
void Parser::css_error(const std::string& msg, const std::string& prefix, const std::string& middle, const bool trim)
28792886
{
28802887
int max_len = 18;
28812888
const char* end = this->end;
@@ -2887,7 +2894,7 @@ namespace Sass {
28872894
utf8::prior(last_pos, source);
28882895
}
28892896
// backup position to last significant char
2890-
while (last_pos > source && last_pos < end) {
2897+
while (trim && last_pos > source && last_pos < end) {
28912898
if (!Prelexer::is_space(*last_pos)) break;
28922899
utf8::prior(last_pos, source);
28932900
}

src/parser.hpp

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

232233
Block_Obj parse();

0 commit comments

Comments
 (0)