Solidity&Foundry 安全审计测试 脏字节

名称:

脏字节

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."
        );
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

0xweb3q

有钱的捧个钱场,没钱的捧个人场

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值