Skip to content

Commit

Permalink
Merge pull request #9007 from BBasile/hdrgen-unsafe-accesses
Browse files Browse the repository at this point in the history
fix issue 15876 - various cases of SEGFAULT when formatting parser errors
merged-on-behalf-of: Nicholas Wilson <thewilsonator@users.noreply.github.com>
  • Loading branch information
dlang-bot committed Nov 24, 2018
2 parents 239c037 + 08b1cb8 commit da15e2f
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 17 deletions.
24 changes: 14 additions & 10 deletions src/dmd/hdrgen.d
Expand Up @@ -124,7 +124,8 @@ public:

override void visit(ExpStatement s)
{
if (s.exp && s.exp.op == TOK.declaration)
if (s.exp && s.exp.op == TOK.declaration &&
(cast(DeclarationExp)s.exp).declaration)
{
// bypass visit(DeclarationExp)
(cast(DeclarationExp)s.exp).declaration.accept(this);
Expand Down Expand Up @@ -636,7 +637,8 @@ public:
{
buf.writestring(Token.toString(s.tok));
buf.writeByte(' ');
s.statement.accept(this);
if (s.statement)
s.statement.accept(this);
}

override void visit(ThrowStatement s)
Expand Down Expand Up @@ -1847,7 +1849,7 @@ public:
buf.writeByte(' ');
typeToBuffer(d.type, d.ident);
}
else
else if (d.ident)
{
declstring = (d.ident == Id.string || d.ident == Id.wstring || d.ident == Id.dstring);
buf.writestring(d.ident.toString());
Expand Down Expand Up @@ -2730,19 +2732,21 @@ public:
* are handled in visit(ExpStatement), so here would be used only when
* we'll directly call Expression.toChars() for debugging.
*/
if (auto v = e.declaration.isVarDeclaration())
if (e.declaration)
{
if (auto v = e.declaration.isVarDeclaration())
{
// For debugging use:
// - Avoid printing newline.
// - Intentionally use the format (Type var;)
// which isn't correct as regular D code.
buf.writeByte('(');
visitVarDecl(v, false);
buf.writeByte(';');
buf.writeByte(')');
buf.writeByte('(');
visitVarDecl(v, false);
buf.writeByte(';');
buf.writeByte(')');
}
else e.declaration.accept(this);
}
else
e.declaration.accept(this);
}

override void visit(TypeidExp e)
Expand Down
18 changes: 11 additions & 7 deletions src/dmd/parse.d
Expand Up @@ -8767,13 +8767,17 @@ final class Parser(AST) : Lexer

AST.Expression parseAssignExp()
{
auto e = parseCondExp();
// require parens for e.g. `t ? a = 1 : b = 2`
// Deprecated in 2018-05.
// @@@DEPRECATED_2.091@@@.
if (e.op == TOK.question && !e.parens && precedence[token.value] == PREC.assign)
dmd.errors.deprecation(e.loc, "`%s` must be surrounded by parentheses when next to operator `%s`",
e.toChars(), Token.toChars(token.value));
AST.Expression e;
e = parseCondExp();
if (e is null)
return e;

// require parens for e.g. `t ? a = 1 : b = 2`
// Deprecated in 2018-05.
// @@@DEPRECATED_2.091@@@.
if (e.op == TOK.question && !e.parens && precedence[token.value] == PREC.assign)
dmd.errors.deprecation(e.loc, "`%s` must be surrounded by parentheses when next to operator `%s`",
e.toChars(), Token.toChars(token.value));

const loc = token.loc;
switch (token.value)
Expand Down
1 change: 1 addition & 0 deletions test/fail_compilation/e15876_1.d
@@ -0,0 +1 @@
o[{scope(x
1 change: 1 addition & 0 deletions test/fail_compilation/e15876_2.d
@@ -0,0 +1 @@
o[{template
1 change: 1 addition & 0 deletions test/fail_compilation/e15876_3.d
@@ -0,0 +1 @@
d(={for
1 change: 1 addition & 0 deletions test/fail_compilation/e15876_4.d
@@ -0,0 +1 @@
typeof){for
1 change: 1 addition & 0 deletions test/fail_compilation/e15876_5.d
@@ -0,0 +1 @@
p[{alias
1 change: 1 addition & 0 deletions test/fail_compilation/e15876_6.d
@@ -0,0 +1 @@
auto unaryExParseError = immutable(int).;

0 comments on commit da15e2f

Please sign in to comment.