44.delete mapping
2023-07-16 16:54:47 # 00.security

delete mapping

1.setBalancesStruct

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
2023-07-16 16:56:41.633 | SUCCESS  | main.Blockchain:__init__:38 - 
[Chain][Initialize] Connected to [HTTP://127.0.0.1:8545]
----------------------------------------------------------------------------------------------------
2023-07-16 16:56:41.696 | SUCCESS | main.Blockchain:GetStorage:281 -
[Chain][GetStorage]
[Address] 0x2Eb99D1DeBD3D92b3c6bfC85b09e8864E14d1606
[SlotIndex] 53527352400092408662675107845356305355237133692174925906457297885835689536801
[Value] [Hex][0x15a26f4a022b81c85ace9d82c223237d53a7c885] <=> [Dec][123511221556980525555710516015044264263871350917]
--------------------------------------------------------------------------------
2023-07-16 16:56:41.711 | SUCCESS | main.Blockchain:GetStorage:281 -
[Chain][GetStorage]
[Address] 0x2Eb99D1DeBD3D92b3c6bfC85b09e8864E14d1606
[SlotIndex] 53527352400092408662675107845356305355237133692174925906457297885835689536802
[Value] [Hex][0x00] <=> [Dec][0]
--------------------------------------------------------------------------------
2023-07-16 16:56:41.727 | SUCCESS | main.Blockchain:GetStorage:281 -
[Chain][GetStorage]
[Address] 0x2Eb99D1DeBD3D92b3c6bfC85b09e8864E14d1606
[SlotIndex] 53527352400092408662675107845356305355237133692174925906457297885835689536803
[Value] [Hex][0x09] <=> [Dec][9]
--------------------------------------------------------------------------------
2023-07-16 16:56:41.743 | SUCCESS | main.Blockchain:GetStorage:281 -
[Chain][GetStorage]
[Address] 0x2Eb99D1DeBD3D92b3c6bfC85b09e8864E14d1606
[SlotIndex] 33426032559509467770105053423268718544989244874896990205098647808323176206627
[Value] [Hex][0x0a] <=> [Dec][10]

2.remove

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
2023-07-16 16:56:57.078 | SUCCESS  | main.Blockchain:__init__:38 - 
[Chain][Initialize] Connected to [HTTP://127.0.0.1:8545]
----------------------------------------------------------------------------------------------------
2023-07-16 16:56:57.142 | SUCCESS | main.Blockchain:GetStorage:281 -
[Chain][GetStorage]
[Address] 0x2Eb99D1DeBD3D92b3c6bfC85b09e8864E14d1606
[SlotIndex] 53527352400092408662675107845356305355237133692174925906457297885835689536801
[Value] [Hex][0x00] <=> [Dec][0]
--------------------------------------------------------------------------------
2023-07-16 16:56:57.158 | SUCCESS | main.Blockchain:GetStorage:281 -
[Chain][GetStorage]
[Address] 0x2Eb99D1DeBD3D92b3c6bfC85b09e8864E14d1606
[SlotIndex] 53527352400092408662675107845356305355237133692174925906457297885835689536802
[Value] [Hex][0x00] <=> [Dec][0]
--------------------------------------------------------------------------------
2023-07-16 16:56:57.175 | SUCCESS | main.Blockchain:GetStorage:281 -
[Chain][GetStorage]
[Address] 0x2Eb99D1DeBD3D92b3c6bfC85b09e8864E14d1606
[SlotIndex] 53527352400092408662675107845356305355237133692174925906457297885835689536803
[Value] [Hex][0x00] <=> [Dec][0]
--------------------------------------------------------------------------------
2023-07-16 16:56:57.190 | SUCCESS | main.Blockchain:GetStorage:281 -
[Chain][GetStorage]
[Address] 0x2Eb99D1DeBD3D92b3c6bfC85b09e8864E14d1606
[SlotIndex] 33426032559509467770105053423268718544989244874896990205098647808323176206627
[Value] [Hex][0x0a] <=> [Dec][10]

A deletion in a structure containing a mapping will not delete the mapping (see the Solidity documentation). The remaining data may be used to compromise the contract.

remove deletes an item of stackBalance. The mapping balances is never deleted, so remove does not work as intended.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
pragma solidity 0.5.17;
contract A{

struct BalancesStruct{
address owner;
mapping(address => uint) balances;
uint256 hello;
}

mapping(address => BalancesStruct) public stackBalance;

function setBalancesStruct() public {
BalancesStruct memory x;
x.owner = msg.sender;
x.hello = 9;
stackBalance[msg.sender] = x;
stackBalance[msg.sender].balances[msg.sender] = 10;
}

function remove() public {
delete stackBalance[msg.sender];
}

function getStorageLocationForKey(address _key,uint256 _slot) public pure returns(bytes32) {
// keccak256( msg.sender, keccak256(msg.sender,0) )
return keccak256(abi.encode(_key, _slot));
}
}

Recommendation

Use a lock mechanism instead of a deletion to disable structure containing a mapping.

or use high solidity compiler

Prev
2023-07-16 16:54:47 # 00.security
Next