2020-04-20 19:43:24 +00:00
|
|
|
.. index: variable cleanup
|
|
|
|
|
|
|
|
*********************
|
|
|
|
Cleaning Up Variables
|
|
|
|
*********************
|
|
|
|
|
|
|
|
When a value is shorter than 256 bit, in some cases the remaining bits
|
|
|
|
must be cleaned.
|
|
|
|
The Solidity compiler is designed to clean such remaining bits before any operations
|
|
|
|
that might be adversely affected by the potential garbage in the remaining bits.
|
|
|
|
For example, before writing a value to memory, the remaining bits need
|
|
|
|
to be cleared because the memory contents can be used for computing
|
|
|
|
hashes or sent as the data of a message call. Similarly, before
|
|
|
|
storing a value in the storage, the remaining bits need to be cleaned
|
|
|
|
because otherwise the garbled value can be observed.
|
|
|
|
|
2021-10-11 11:18:43 +00:00
|
|
|
Note that access via inline assembly is not considered such an operation:
|
|
|
|
If you use inline assembly to access Solidity variables
|
|
|
|
shorter than 256 bits, the compiler does not guarantee that
|
|
|
|
the value is properly cleaned up.
|
|
|
|
|
|
|
|
Moreover, we do not clean the bits if the immediately
|
2020-04-20 19:43:24 +00:00
|
|
|
following operation is not affected. For instance, since any non-zero
|
|
|
|
value is considered ``true`` by ``JUMPI`` instruction, we do not clean
|
|
|
|
the boolean values before they are used as the condition for
|
|
|
|
``JUMPI``.
|
|
|
|
|
|
|
|
In addition to the design principle above, the Solidity compiler
|
|
|
|
cleans input data when it is loaded onto the stack.
|
|
|
|
|
|
|
|
Different types have different rules for cleaning up invalid values:
|
|
|
|
|
|
|
|
+---------------+---------------+-------------------+
|
|
|
|
|Type |Valid Values |Invalid Values Mean|
|
|
|
|
+===============+===============+===================+
|
|
|
|
|enum of n |0 until n - 1 |exception |
|
|
|
|
|members | | |
|
|
|
|
+---------------+---------------+-------------------+
|
|
|
|
|bool |0 or 1 |1 |
|
|
|
|
+---------------+---------------+-------------------+
|
|
|
|
|signed integers|sign-extended |currently silently |
|
|
|
|
| |word |wraps; in the |
|
|
|
|
| | |future exceptions |
|
|
|
|
| | |will be thrown |
|
|
|
|
| | | |
|
|
|
|
| | | |
|
|
|
|
+---------------+---------------+-------------------+
|
|
|
|
|unsigned |higher bits |currently silently |
|
|
|
|
|integers |zeroed |wraps; in the |
|
|
|
|
| | |future exceptions |
|
|
|
|
| | |will be thrown |
|
|
|
|
+---------------+---------------+-------------------+
|