Skip to content

Program would not terminate if two contracts import each other. #2

@Jonah246

Description

@Jonah246

Reproduce

pragma solidity ^0.4.24;
import "./B.sol";
contract A {
  B kid;
  constructor() public {
    kid = new B();
  }
}
pragma solidity ^0.4.24;
import "./A.sol";
contract B {
  A mom;
  constructor() publicv{
    mom = A(msg.sender);
  }
}

Recommend fix

def unfold_imports(imports, infile):
    buffer = []

    if infile not in imports:
        # should add to traversed paths here.
        imports.append(infile)
        with open(infile, "r+") as f:
            for line in f:
                # Remove the pragma line in all imports
                if "pragma" in line[:6]:
                    continue

                # Read the imports
                if "import" in line[:6]:
                    match = re.search(r".*[\'|\"](.*)[\'|\"]", line)
                    if match:
                        dirname = os.path.dirname(infile)
                        file = os.path.join(dirname, match.group(1))
                        absfile = os.path.abspath(file)

                        buffer.append(unfold_imports(imports, absfile))
                        # imports.append(absfile)
                    else:
                        print("There's syntax error of import in {}".format(infile))
                        sys.exit(-3)
                else:
                    buffer.append(line)

    return ''.join(buffer)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions