Commit 7c2a09af authored by ben-chain's avatar ben-chain Committed by GitHub

Fix RLPWriter Memory Allocation (#84)

* add failing test case with this issue

* add _malloc which works

* malloc words not bytes
parent 8e116f2a
......@@ -89,6 +89,7 @@ library Lib_RLPWriter {
bytes memory _out
)
{
_malloc(0x20);
bytes memory inputBytes;
assembly {
let m := mload(0x40)
......@@ -308,4 +309,22 @@ library Lib_RLPWriter {
return flattened;
}
/**
* Clears memory wherever the free memory is pointing.
* @param _numBytes Number of bytes to clear out (will be rounded up to nearest word).
*/
function _malloc(
uint _numBytes
)
private
pure
{
assembly {
let free_mem := mload(0x40)
for { let offset := 0x00 } lt(offset, _numBytes) { offset := add(offset, 0x20) } {
mstore(add(free_mem, offset), 0x00)
}
}
}
}
......@@ -4,6 +4,7 @@ pragma experimental ABIEncoderV2;
/* Library Imports */
import { Lib_RLPWriter } from "../../optimistic-ethereum/libraries/rlp/Lib_RLPWriter.sol";
import { TestERC20 } from "../../test-helpers/TestERC20.sol";
/**
* @title TestLib_RLPWriter
......@@ -93,4 +94,16 @@ contract TestLib_RLPWriter {
{
return Lib_RLPWriter.writeBool(_in);
}
function writeAddressWithOtherMemory(
address _in
)
public
returns (
bytes memory _out
)
{
new TestERC20();
return Lib_RLPWriter.writeAddress(_in);
}
}
......@@ -41,4 +41,13 @@ describe('Lib_RLPWriter', () => {
})
}
})
describe.only('Use of library with other memory-modifying operations', () => {
it('should allow creation of a contract beforehand and still work', async () => {
const randomAddress = '0x1234123412341234123412341234123412341234'
const rlpEncodedRandomAddress = '0x941234123412341234123412341234123412341234'
const encoded = await Lib_RLPWriter.callStatic.writeAddressWithOtherMemory(randomAddress)
expect(encoded).to.eq(rlpEncodedRandomAddress)
})
})
})
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment