Skip to content

Instantly share code, notes, and snippets.

View bartubozkurt's full-sized avatar
🎯
Focusing

Bartu Bozkurt bartubozkurt

🎯
Focusing
View GitHub Profile
/* Bad */
contract Bad{}
contract Tom is Bad{
constructor() public Bad(){}
}
/* Better */
contract Good{}
contract Tom is Good{
constructor() public {} // remove the constructor call.
/* Bad */
contract A {
uint x;
constructor() public {
x = 0;
}
function A() public {
x = 1;
}
/* Bad */
modifier myModif(){
if(..){
_;
}
}
function get() myModif returns(uint){}
/* Better */
modidfier myModif(){
/* Bad */
contract BadGuy {
function isLove(address _addr) external returns(bool) {}
}
contract BadGirl {
BadGuy badguy;
modifier isCheck(address _addr) {
require(badguy.isLove(_addr));
_;
/* Bad */
contract Bad {
function badDelegate(address _yourContract, bytes calldata _data) payable public returns (bytes memory) {
(bool success, bytes memory data) = _yourContract.delegatecall(_data);
require(success);
return data;
}
}
/* Vulnerability
Anyone can destroy the Bad contract using by “selfdestruct”
/* Bad */
contract SendEth{
mapping(address => uint256) public balanceOf;
function withdraw(address user, uint256 numTokens) public {
require(balanceOf[user] >= numTokens);
balanceOf[user] -= numTokens;
user.transfer(numTokens * 1 ether);
}
}
/* Bad */
address owner;
function setOwner() public {
owner = msg.sender;
}
/* Better */
contract Buggy{
/*
modifier onlyOwner() {
/* Bad */
// Sample1.sol
pragma solidity =0.8.4;
// Sample2.sol
pragma solidity =0.8.0;
/* Better */
// Sample1.sol
pragma solidity =0.8.4;
/* Bad */
pragma solidity ^0.8.4;
/* Better */
pragma solidity =0.8.4;
/* Bad */
pragma solidity ^0.4.25;
/* Better */
pragma solidity 0.8.17;