Skip to content

Commit 6791726

Browse files
Address cs audit 5.5 (#123)
* address cs audit 5.5 * now empty code is not valid * Update test/quark-core/CodeJar.t.sol Co-authored-by: Kevin Cheng <[email protected]> * Update test/quark-core/CodeJar.t.sol Co-authored-by: Kevin Cheng <[email protected]> * address comments, remove line 43, and use saveCode() * update gas snapshot * update snapshot --------- Co-authored-by: Kevin Cheng <[email protected]>
1 parent 3e46e98 commit 6791726

File tree

3 files changed

+28
-18
lines changed

3 files changed

+28
-18
lines changed

.gas-snapshot

+8-7
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,16 @@ CallbacksTest:testCallcodeReentrancyProtection() (gas: 189755)
88
CallbacksTest:testNestedCallWithNoCallbackSucceeds() (gas: 118558)
99
CallbacksTest:testPayableCallback() (gas: 114193)
1010
CallbacksTest:testRevertsOnCallbackWhenNoActiveCallback() (gas: 100390)
11+
CodeJarTest:testCodeJarCodeDoesNotExistOnEmptyScriptWithETH() (gas: 56347)
1112
CodeJarTest:testCodeJarCounter() (gas: 377506)
12-
CodeJarTest:testCodeJarDeployConstructor() (gas: 47037)
13-
CodeJarTest:testCodeJarDeployNotAffectedByChangedCodeHash() (gas: 81705)
14-
CodeJarTest:testCodeJarDifferentZeros() (gas: 90061)
13+
CodeJarTest:testCodeJarDeployConstructor() (gas: 47059)
14+
CodeJarTest:testCodeJarDeployNotAffectedByChangedCodeHash() (gas: 81727)
15+
CodeJarTest:testCodeJarDifferentZeros() (gas: 90083)
1516
CodeJarTest:testCodeJarFirstDeploy() (gas: 44694)
16-
CodeJarTest:testCodeJarInputVariety() (gas: 451689)
17-
CodeJarTest:testCodeJarLarge() (gas: 70676086)
18-
CodeJarTest:testCodeJarSecondDeploy() (gas: 47605)
19-
CodeJarTest:testCodeJarSelfDestruct() (gas: 50952)
17+
CodeJarTest:testCodeJarInputVariety() (gas: 407557)
18+
CodeJarTest:testCodeJarLarge() (gas: 70676108)
19+
CodeJarTest:testCodeJarSecondDeploy() (gas: 47627)
20+
CodeJarTest:testCodeJarSelfDestruct() (gas: 50976)
2021
CometClaimRewardsTest:testClaimComp() (gas: 132589)
2122
CometRepayAndWithdrawMultipleAssetsTest:testInvalidInput() (gas: 78255)
2223
CometRepayAndWithdrawMultipleAssetsTest:testRepayAndWithdrawMultipleAssets() (gas: 161976)

quark-core/src/CodeJar.sol

+1-2
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,14 @@ contract CodeJar {
4040

4141
/**
4242
* @notice Checks if code was already deployed by CodeJar
43-
* @dev Use `saveCode` to get the address of the contract with that code
4443
* @param code The runtime bytecode of the code to check
4544
* @return True if code already exists in Code Jar
4645
*/
4746
function codeExists(bytes calldata code) external view returns (bool) {
4847
bytes memory initCode = getInitCode(code);
4948
address codeAddress = getCodeAddress(initCode);
5049

51-
return codeAddress.codehash == keccak256(code);
50+
return codeAddress.code.length != 0 && codeAddress.codehash == keccak256(code);
5251
}
5352

5453
/**

test/quark-core/CodeJar.t.sol

+19-9
Original file line numberDiff line numberDiff line change
@@ -84,16 +84,15 @@ contract CodeJarTest is Test {
8484
}
8585

8686
function testCodeJarInputVariety() public {
87-
bytes[] memory scripts = new bytes[](8);
88-
scripts[0] = hex"";
89-
scripts[1] = hex"00";
90-
scripts[2] = hex"11";
91-
scripts[3] = hex"112233";
92-
scripts[4] = hex"00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff";
93-
scripts[5] = hex"00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff11";
94-
scripts[6] =
87+
bytes[] memory scripts = new bytes[](7);
88+
scripts[0] = hex"00";
89+
scripts[1] = hex"11";
90+
scripts[2] = hex"112233";
91+
scripts[3] = hex"00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff";
92+
scripts[4] = hex"00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff11";
93+
scripts[5] =
9594
hex"00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff";
96-
scripts[7] =
95+
scripts[6] =
9796
hex"00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff11";
9897

9998
for (uint8 i = 0; i < scripts.length; i++) {
@@ -155,5 +154,16 @@ contract CodeJarTest is Test {
155154
assertEq(returnData, hex"abcd");
156155
}
157156

157+
function testCodeJarCodeDoesNotExistOnEmptyScriptWithETH() public {
158+
bytes memory code = hex"";
159+
assertEq(codeJar.codeExists(code), false);
160+
address scriptAddress = codeJar.saveCode(code);
161+
vm.deal(address(this), 1 ether);
162+
scriptAddress.call{value: 1}("");
163+
164+
// Ensure codeExists correctness holds for empty code with ETH
165+
assertEq(codeJar.codeExists(code), false);
166+
}
167+
158168
// Note: cannot test code too large, as overflow impossible to test
159169
}

0 commit comments

Comments
 (0)