Skip to content

Commit 25486ed

Browse files
committed
fix bug in copy constructor codegen
properly generate offset when length is more than 1 byte long
1 parent 0b89ff5 commit 25486ed

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

puff/codegen.rkt

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,15 @@
1313
[sz-str (if (odd? (string-length sz-str))
1414
(string-append "0" sz-str)
1515
sz-str)]
16-
[sz-hex-str (string-append "0x" sz-str)])
17-
(append (hex->instrs sz-hex-str) '("DUP1" "PUSH1" "0x09" "RETURNDATASIZE" "CODECOPY" "RETURNDATASIZE" "RETURN"))))
16+
[sz-hex-str (string-append "0x" sz-str)]
17+
;; Calculate the correct offset based on length bytes
18+
[length-bytes (ceiling (/ (string-length sz-str) 2))]
19+
[offset-str (number->string (+ 9 (- length-bytes 1)) 16)]
20+
[offset-str (if (odd? (string-length offset-str))
21+
(string-append "0" offset-str)
22+
offset-str)]
23+
[offset-val (format "0x~a" offset-str)])
24+
(append (hex->instrs sz-hex-str) (list "DUP1" "PUSH1" offset-val "RETURNDATASIZE" "CODECOPY" "RETURNDATASIZE" "RETURN"))))
1825

1926
(provide
2027
generate-copy-constructor)

test/LongJumps.t.sol

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
pragma solidity ^0.8.13;
33

44
import {Test, console} from "forge-std/Test.sol";
5-
import {PuffDeployer} from "../src/Deployers.sol";
5+
import {PuffDeployer, HuffDeployer} from "../src/Deployers.sol";
66

77
// These interfaces define callable functions on our test contracts
88
interface ISaysHello {
@@ -11,9 +11,11 @@ interface ISaysHello {
1111

1212
contract LongJumpsTest is Test {
1313
PuffDeployer public puffDeployer;
14+
HuffDeployer public huffDeployer;
1415

1516
function setUp() public {
1617
puffDeployer = new PuffDeployer();
18+
huffDeployer = new HuffDeployer();
1719
}
1820

1921
function test_longJump() public {
@@ -26,4 +28,14 @@ contract LongJumpsTest is Test {
2628
assertEq(message, 0x0000000000000000000000000000000000000048656c6c6f2c20576f726c6421);
2729
}
2830

31+
function test_longJump_Huff() public {
32+
address newContract = huffDeployer.deployContract("examples/long_jump.huff");
33+
ISaysHello jumper = ISaysHello(newContract);
34+
bytes32 message = jumper.sayHello();
35+
36+
// The expected output is "Hello, World!" right-aligned in a bytes32
37+
38+
assertEq(message, 0x0000000000000000000000000000000000000048656c6c6f2c20576f726c6421);
39+
}
40+
2941
}

0 commit comments

Comments
 (0)