Contract A has a state variable owner
, which initialized well to msg.sender
.
But this owner
variable became 0x0
when invoked from another contract function which take address of contract A's instance and convert to A type.
(code also available at https://github.com/ethereum/solidity/issues/5210)
pragma solidity 0.4.25;
contract A{
address public owner;
event Ret(string flag, address sender, address owner);
event ConstrutEvt(string flag, address owner );
function A() public{
owner = msg.sender;
emit ConstrutEvt("A", msg.sender);
}
function doSomething() public view returns(string flag, address sender, address owner){
emit Ret("A::doSomething", msg.sender, owner);
return ("A::doSomething", msg.sender, owner);
}
}
contract EvilDoer{
// step1: deploy contract A on remix IDE.
// and the log show `owner` is a valid address value.
// step2: deploy contract EvilDoer.
// step3: on remix IDE run tab, invoke doSomething() use the contract A address as argument.
// This time the log show that 'owner' is zero. Why ?
function doSomethingEvil(address instanceAddrOfA) public {
A contractA = A(instanceAddrOfA);
contractA.doSomething();
}
}
Your issue is not with EvilDoer
, but rather with the doSomething()
function in A
.
When you set your return values, you are assigning them values string flag, address sender, address owner
. While this is a valid action, what you are inadvertently doing is overwriting the owner
variable you defined above.
To fix this, change the name of owner
in your return declaration to something like _owner
so that you do not overwrite the global variable.