Skip to content

parse() throws TypeError instead of ParserError on some syntax errors (AST is built before the syntax-error check) #131

Open

Description

@kaypee90

When the input has some syntax errors, parse() throws an unhandled TypeError (Cannot read properties of null), not a ParserError. This occurs with tolerant: true too.

Tools that use the parser, for example solhint, catch only ParserError. Thus one bad file stops the full run, and the tool gives no results for the other files (protofire/solhint#725, protofire/solhint#817).

Environment

  • @solidity-parser/parser: 0.20.2. Also seen in 0.16.x, where the error is Error: The specified node does not exist.
  • Node.js: v20.20.2

Reproduction

const parser = require('@solidity-parser/parser')

const inputs = [
  'contract A { uint x = ; }',
  'contract A { function f() public { if () {} } }',
  'uint constant X = ;',
  'contract A { function f() public { emit ; } }',
  'contract A { function fallback(address payable r) public {} }',
]

for (const src of inputs) {
  for (const opts of [{}, { tolerant: true }]) {
    try {
      parser.parse(src, opts)
      console.log('OK', JSON.stringify(opts), src)
    } catch (e) {
      const kind = e instanceof parser.ParserError ? 'ParserError' : e.constructor.name
      console.log(kind, JSON.stringify(opts), src, '->', e.message)
    }
  }
}

Actual result

All inputs throw TypeError, with and without tolerant: true:

Input Error First frame
contract A { uint x = ; } Cannot read properties of null (reading 'length') ASTBuilder.visitExpression
contract A { function f() public { if () {} } } Cannot read properties of null (reading 'length') ASTBuilder.visitExpression
uint constant X = ; Cannot read properties of null (reading 'length') ASTBuilder.visitExpression
contract A { function f() public { emit ; } } Cannot read properties of null (reading 'expressionList') ASTBuilder.visitFunctionCall
contract A { function fallback(address payable r) public {} } Cannot read properties of null (reading 'accept') ASTBuilder.visit ← visitContractPart

For comparison, contract A { function f() public { require(, "x"); } } gives a ParserError (missing ';' at '('), as expected.

Expected result

  • Without tolerant: throw a ParserError that lists the syntax errors.
  • With tolerant: true: return the AST and the errors list. Do not throw.

Cause

In src/parser.ts, parse() builds the AST (astBuilder.visit(sourceUnit), line 70) before it checks for syntax errors (listener.hasErrors(), line 90).

When ANTLR recovers from a syntax error, it can make a context with a required child missing. The ASTBuilder does not check for this. For example:

  • visitVariableDeclarationStatement, visitIfStatement, visitFileLevelConstant and others call this.visitExpression(ctx.expression()) when ctx.expression() is null. visitExpression then reads ctx.children!.length (src/ASTBuilder.ts:1032).
  • visitContractPart calls this.visit(ctx.getChild(0)) on a ContractPartContext that has no child.

Possible fix

  1. In non-tolerant mode, check listener.hasErrors() before astBuilder.visit(...), and throw a ParserError before the AST is built.
  2. Put astBuilder.visit(...) in a try/catch. If the listener has errors, throw a ParserError in non-tolerant mode, or return the errors in tolerant mode, instead of the internal TypeError.
  3. Add null checks in ASTBuilder where error recovery can leave a child missing.

Option 1 or 2 fixes all cases of this type, including #116.

Related

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions