solidity-parser/parser

Error encountered when parsing valid assembly switch statement

Closed this issue · 7 comments

Here's the Solidity - I tried to trim most of the code out so it's just left with the problematic stuff.

pragma solidity ^0.8.4;

contract SampleContract {

    uint t;

    function test() internal view {
        bool result;
        assembly {
            let a := 1
            if iszero(a) {
                switch a
                case 32 {
                    a := 1
                }
                default {
                    result := true
                }
            }
        }
    }
}

Here's the error I'm getting:

Error parsing /home/proj/scratch/SampleContract.sol
RangeError: index parameter must be between >= 0 and <= number of children.
    at AssemblyExpressionContext.getChild (/home/proj/scratch/node_modules/@solidity-parser/parser/dist/index.cjs.js:9347:15)
    at ASTBuilder.visitAssemblyExpression (/home/proj/scratch/node_modules/@solidity-parser/parser/dist/index.cjs.js:36579:27)
    at ASTBuilder.visitAssemblyAssignment (/home/proj/scratch/node_modules/@solidity-parser/parser/dist/index.cjs.js:36696:24)
    at AssemblyAssignmentContext.accept (/home/proj/scratch/node_modules/@solidity-parser/parser/dist/index.cjs.js:34682:22)
    at ASTBuilder.visit (/home/proj/scratch/node_modules/@solidity-parser/parser/dist/index.cjs.js:17669:19)
    at ASTBuilder.visitAssemblyItem (/home/proj/scratch/node_modules/@solidity-parser/parser/dist/index.cjs.js:36576:17)
    at /home/proj/scratch/node_modules/@solidity-parser/parser/dist/index.cjs.js:36541:62
    at Array.map (<anonymous>)
    at ASTBuilder.visitAssemblyBlock (/home/proj/scratch/node_modules/@solidity-parser/parser/dist/index.cjs.js:36541:43)
    at ASTBuilder.visitAssemblyCase (/home/proj/scratch/node_modules/@solidity-parser/parser/dist/index.cjs.js:36638:19)

After shaving more off, I think this is actually related to the statement result := true

I get a similar error from this file:

// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.4;

contract SampleContract {
    
    function verify() external view returns (bool) {
        assembly {
            let p := true
        }
    }
}
XD-OK commented

I encountered the same problem

This seems to be working in the latest version of the parser, so I'm going to close this issue.

@XD-OK if you are running into a problem and have a snippet of code that reproduces it, please open a new issue.

@fvictorio thanks for maintaining this incredible package. God bless you.

The same error happens in these modifiers:

     modifier onlyTokenholders {
          if (balanceOf(msg.sender) == 0) throw;
              _
      }

and

    modifier noEther() {if (msg.value > 0) throw; _}

Adding a ; after those _ fixes the error.

These modifiers exist in The Dao smart contract 0xBB9bc244D798123fDe783fCc1C72d3Bb8C189413 but other projects may not add a ; after the _ in every modifier.

I s**k at javascript and debugging large codebases, another way I would submit a pull request myself.
How can we proceed to fix this?

Looking forward.

Hey @infosec-us-team, thanks for letting me know. Do you happen to know what's the latest version of solc that accepts _ without semicolon? That's not allowed in the latest versions.

Apparently even 0.4.11 (the first version supported by Hardhat) doesn't allow this. So this seems to be a really old syntax. That being said, I'm not against adding support for it, unless it turns out to be super hard to do. Feel free to open a new issue with this feature request.

The compiler version used in the deployment of that smart contract is v0.3.1-2016-04-12-3ad5e82 - yes, way older than 0.4.11.

I will open a feature request. Thank you for replying this fast and being so kind.

Have a great day.