Repository navigation
Description
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 isError: 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 aParserErrorthat lists the syntax errors. - With
tolerant: true: return the AST and theerrorslist. 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,visitFileLevelConstantand others callthis.visitExpression(ctx.expression())whenctx.expression()isnull.visitExpressionthen readsctx.children!.length(src/ASTBuilder.ts:1032).visitContractPartcallsthis.visit(ctx.getChild(0))on aContractPartContextthat has no child.
Possible fix
- In non-tolerant mode, check
listener.hasErrors()beforeastBuilder.visit(...), and throw aParserErrorbefore the AST is built. - Put
astBuilder.visit(...)in atry/catch. If the listener has errors, throw aParserErrorin non-tolerant mode, or return the errors in tolerant mode, instead of the internalTypeError. - Add null checks in
ASTBuilderwhere error recovery can leave a child missing.
Option 1 or 2 fixes all cases of this type, including #116.
Related
- v0.18.0 - "TypeError: Cannot read properties of null (reading 'accept')" when parsing old-style value transfers
msg.sender.send{1 ETH}()#116: same crash (reading 'accept') formsg.sender.send{1 ETH}() - Parser crashes when parsing exactly's codebase via solhint #87:
The specified node does not exist - Parser crashes with TypeError when visiting contract parts protofire/solhint#725 and Crash TypeError: Cannot read properties of null (reading 'accept') on a function named fallback protofire/solhint#817: solhint stops on this crash (found in paritytech/polkadot-sdk)
Activity