Solidity Internals Variable Clean Up Main Tips
- Variable clean up in Solidity is a feature that covers how types handle invalid values.
- This is an internal Solidity compiler feature.
Solidity Internals Variable Clean Up
In cases with values less than 256-bit long, occasionally the remaining bits have to be cleaned. The design of Solidity compiler instructs it to clean remaining bits like that before any operations which may be affected adversely by the garbage that potentially is in the bits that are remaining. As an example, prior to writing a value into memory, the remainder of bits must be cleared since the contents of the memory may be utilized for computing hashes or, alternatively, sent as the message call data. In a similar way, before a value is stored inside the storage, bits that are remaining must be cleaned since otherwise this garbled value is visible and may be observed.
However, the bits are not cleaned in case the immediately following operation has not been affected. For example, because any value that is non-zero value is to be considered true by the instruction JUMPI, the boolean values are not cleaned before being used as JUMPI‘s condition.
As an addition to the design principle above, the compiler of Solidity will clean input data once it gets loaded onto the stack.
Keep in mind that different types have contrasting rules when it comes to cleaning up values that are invalid:
|Type||If Valid||If Invalid|
|enums of n members||0 until n – 1||throw exception|
|bool||0 or 1||1|
|signed integers (int)||word, sign-extended||silent wrapping currently; planned to make it throw an exception|
|unsigned integers (uint)||zeroed higher-bits||silent wrapping currently; planned to make it throw an exception|