Description
There should be a warning when calls to other contracts is made inside a constructor.
Motivation
Calling another contract (B) in the constructor of a contract (A) will give B access to the partially initialized account of A (through msg.sender
, or in asm, caller
). Account creation is a special circumstance where the account that is being initialized actually has code in it, but any calls to that account during initialization (i.e calls from contracts that are themselves called in the constructor) will be done to a version of the account with no code in it. Generally speaking - code inside a constructor does not always behave as "regular" code, and can also cause other code not to behave as expected, and this is one of those cases.
Suggestion
Adding a warning to the analyzer when someone tries to call another account from inside a constructor.
This would of course not be water proof, as it could still call (internal) functions that in turn calls other contracts etc. Perhaps it should warn when calling any function from inside a constructor (or at least those that are not pure/view), though that would become very complicated. Feels like this is the type of issue that would likely become more complicated as work progresses.
Additionally, the use of codesize
and extcodesize(address)
should probably be flagged too, and potentially other things that expects a fully initialized object.
If accepted, I would not mind trying to work this in myself, and make a PR!
More info
If someone is interested in initialization issues in general, i wrote a short blog post about it here (https://github.com/androlo/solidity-workshop/blob/master/blogs/2017-07-26-constructors-classes-and-contracts.md). It brings up similar issues from other (mainly object oriented) languages, and talks a bit about the dangers of partially initialized objects. Not all of it applies to Solidity though.