名称:

脏字节

https://github.com/XuHugo/solidityproject/tree/master/vulnerable-defi

描述:

将bytes数组从memory或calldata复制到storage,是以32字节为单位进行复制的,当长度不是32的倍数,额外字节会从calldata或memory复制到storage中。存储中的脏值只有在bytes数组中进行空的.push()时才可见。

这个问题已经在0.8.15修复了。

过程:

部署Dirtybytes合约,在合约初始化的时候,创建一个memory变量,将值赋给一个storage变量,然后使用空的push,就可以看到storage的非空值了;

解决方法:

这个问题已经在0.8.15修复了。不过实际应用,大概也不会有人使用空的push吧。

合约:

contract Dirtybytes {
    event ev(uint[], uint);
    bytes s;

    constructor() {

        emit ev(new uint[](2), 0);
        bytes memory m = new bytes(63);
        s = m;
    }

    function h() external returns (bytes memory) {
        s.push();
        return s;
    }
}

 foundry 合约:测试版本是0.8.0,修改为0.8.15,就不会有问题了;

contract ContractTest is Test {
    Dirtybytes Dirtybytesontract;

    function testDirtybytes() public {
        Dirtybytesontract = new Dirtybytes();
        emit log_named_bytes(
            "Array element in h() not being zero::",
            Dirtybytesontract.h()
        );
        console.log(
            "Such that the byte after the 63 bytes allocated below will be 0x02."
        );
    }
}