Skip to content

modifiers should generate branches checks on all place they are used #618

Closed
@wighawag

Description

@wighawag

Currently modifiers are considered individually and not as part of where they are called
this cause coverage to wrongly assume the modifier code has been called on all branches simply because if it was executed in another context

solidity-coverage should copy-paste the modifier code and generate branching check on that resulting code.

For now, the only workaround is to not use modifier and instead copy the modifier code in place.

for example:

function transferOwnership(address newOwner) external onlyOwner {
  emit OwnershipTransferred(owner, newOwner);
  owner = newOwner;
}

function setFee(uint256 fee) external onlyOwner {
  _fee = fee;
}

modifier onlyOwner() {
  require(msg.sender == owner, "NOT_AUTHORIZED");
  _;
}

will generate 100% coverage of branches even if only the following test are executed

  • transferOwnership from owner
  • setFee from owner
  • setFee from non-owner

the missing test for "transferOwnership from non-owner" will not be seen by the coverage.

On the contrary the following code will correctly trigger solidity-coverage to see less than 100% coverage as the require(... branching will be detected

function transferOwnership(address newOwner) external {
  require(msg.sender == owner, "NOT_AUTHORIZED");
  emit OwnershipTransferred(owner, newOwner);
  owner = newOwner;
}
function setFee(uint256 fee) external  {
  require(msg.sender == owner, "NOT_AUTHORIZED");
  _fee = fee;
}

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