-
Notifications
You must be signed in to change notification settings - Fork 5.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
constructor
modifier
#3196
Comments
If anything, I'd rather prefer
|
@axic: My proposal above was meant to fix the issue with as little changes to the language as possible. I completely agree with you: If we can change the syntactic structure of the language, then having |
We could also go the C++/Java/C# route and just have Any option that ensures that renaming a contract will not accidentally turn a constructor into a function is good for me. |
I like As an aside, not sure if your preferred linter does this, but if it doesn't I would highly recommend writing a rule to ban all UpperCamelCase functions that are not the constructor. This, together with the rule for enforcing UpperCamelCase contract names should prevent you from making this particular mistake. |
Adding a built in "modifier" called |
I also prefer |
I strongly agree with this, I've many times changed the contract name and forgotten to change the constructor name, but as I was using a full IDE with solc-lint it just remembered "hey this function is not using camelCase". This should also be linked to #3185 where the LibraryContract should deploy the constructor method, instead of being simply run at creation and not stored. |
This should not be too hard to do once we have the keyword. Anyone opposing to using |
I am not aware of any technical advantage of Having said that, I personally prefer the D.R.Y. |
constructor
modifierconstructor
modifier
The original proposal suggested |
WIP: Tests are not adjusted yet. Remove misleading comment implying that unnamed functions are always anonymous functions, resp. fallback functions. Constructors with the new syntax are unnamed functions as well.
WIP: Tests are not adjusted yet. Remove misleading comment implying that unnamed functions are always anonymous functions, resp. fallback functions. Constructors with the new syntax are unnamed functions as well.
WIP: Tests are not adjusted yet. Remove misleading comment implying that unnamed functions are always anonymous functions, resp. fallback functions. Constructors with the new syntax are unnamed functions as well.
WIP: Tests are not adjusted yet. Remove misleading comment implying that unnamed functions are always anonymous functions, resp. fallback functions. Constructors with the new syntax are unnamed functions as well.
how can i input an address on constructor? I have added the function to my code to create tokens and now i want to be able to input a central mint address on the constructor's parameters |
Better to ask the question on https://ethereum.stackexchange.com/ and be a bit more verbose. |
Feature request: Add
constructor
modifierProblem
Solidity currently has no special syntax to distinguish a constructor from a function. A constructor is simply a function whose name equals that of the contract.
This introduces a dependence between the name of the contract and the constructor that is easily overlooked when refactoring code, and can have disastrous consequences: If the contract is renamed, but the constructor isn't, the constructor becomes a public function callable by anyone. This allows the state of the contract to be reset or modified by an adversary. There are examples of such a vulnerability in real-world contracts, e.g. in the case of Rubixi.
I have also encountered this issue during smart contract audits.
Suggested solution
I suggest the following path for fixing this issue:
In a first phase, solidity should introduce a new
constructor
function modifier. The compiler should emit a warning when it encounters a constructor that does not carry the modifier. It should emit an error if it encounters a function that isn't a constructor but that carries the modifier. Here are some examples showing the desired behavior:No warning
Warning
Error
In a second phase, use of the
constructor
modifier would be made mandatory, perhaps as part of the 0.5.0 release. Having a constructor without theconstructor
modifier would result in an error instead of a warning.Acknowledgements
I would like to thank Florian Tramèr (@ftramer) and Phil Daian (@pdaian) for comments & feedback.
The text was updated successfully, but these errors were encountered: