Skip to content

Commit cdaba01

Browse files
authored
v0.0.5 - Critical fix to the slice() method (#14)
* Adding test to account for #13 * Fix for #13 Multiples of 32 bytes can now be safely sliced * More tests for #13 * Bump version to v0.0.5
1 parent 34383c7 commit cdaba01

File tree

4 files changed

+24
-10
lines changed

4 files changed

+24
-10
lines changed

contracts/BytesLib.sol

+8-2
Original file line numberDiff line numberDiff line change
@@ -232,11 +232,17 @@ library BytesLib {
232232
// the actual length of the slice.
233233
let lengthmod := and(_length, 31)
234234

235-
let mc := add(tempBytes, lengthmod)
235+
// The multiplication in the next line is necessary
236+
// because when slicing multiples of 32 bytes (lengthmod == 0)
237+
// the following copy loop was copying the origin's length
238+
// and then ending prematurely not copying everything it should.
239+
let mc := add(add(tempBytes, lengthmod), mul(0x20, iszero(lengthmod)))
236240
let end := add(mc, _length)
237241

238242
for {
239-
let cc := add(add(_bytes, lengthmod), _start)
243+
// The multiplication in the next line has the same exact purpose
244+
// as the one above.
245+
let cc := add(add(add(_bytes, lengthmod), mul(0x20, iszero(lengthmod))), _start)
240246
} lt(mc, end) {
241247
mc := add(mc, 0x20)
242248
cc := add(cc, 0x20)

ethpm.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"package_name": "bytes",
3-
"version": "0.0.4",
3+
"version": "0.0.5",
44
"description": "Solidity bytes tightly packed arrays utility library.",
55
"authors": [
66
"Gonçalo Sá <[email protected]>"

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "solidity-bytes-utils",
3-
"version": "0.0.4",
3+
"version": "0.0.5",
44
"description": "Solidity bytes tightly packed arrays utility library.",
55
"main": "truffle.js",
66
"repository": {

test/TestBytesLib2.sol

+14-6
Original file line numberDiff line numberDiff line change
@@ -54,30 +54,38 @@ contract TestBytesLib2 {
5454
*/
5555

5656
function testSlice() public {
57-
bytes memory memBytes33 = hex"f00d0000000000000000000000000000000000000000000000000000000000feed";
57+
bytes memory memBytes = hex"f00d0000000000000000000000000000000000000000000000000000000000feedf00d00000000000000000000000000000000000000000000000000000000feed";
5858

5959
bytes memory testBytes;
6060
bytes memory resultBytes;
6161

6262
testBytes = hex"f00d";
63-
resultBytes = memBytes33.slice(0,2);
63+
resultBytes = memBytes.slice(0,2);
6464
AssertBytes.equal(resultBytes, testBytes, "Normal slicing array failed.");
6565

6666
testBytes = hex"";
67-
resultBytes = memBytes33.slice(1,0);
67+
resultBytes = memBytes.slice(1,0);
6868
AssertBytes.equal(resultBytes, testBytes, "Slicing with zero-length failed.");
6969

7070
testBytes = hex"";
71-
resultBytes = memBytes33.slice(0,0);
71+
resultBytes = memBytes.slice(0,0);
7272
AssertBytes.equal(resultBytes, testBytes, "Slicing with zero-length on index 0 failed.");
7373

7474
testBytes = hex"feed";
75-
resultBytes = memBytes33.slice(31,2);
75+
resultBytes = memBytes.slice(31,2);
7676
AssertBytes.equal(resultBytes, testBytes, "Slicing across the 32-byte slot boundary failed.");
7777

7878
testBytes = hex"f00d0000000000000000000000000000000000000000000000000000000000feed";
79-
resultBytes = memBytes33.slice(0,33);
79+
resultBytes = memBytes.slice(0,33);
8080
AssertBytes.equal(resultBytes, testBytes, "Full length slice failed.");
81+
82+
testBytes = hex"f00d0000000000000000000000000000000000000000000000000000000000fe";
83+
resultBytes = memBytes.slice(0,32);
84+
AssertBytes.equal(resultBytes, testBytes, "Multiple of 32 bytes slice failed.");
85+
86+
testBytes = hex"f00d0000000000000000000000000000000000000000000000000000000000feedf00d00000000000000000000000000000000000000000000000000000000fe";
87+
resultBytes = memBytes.slice(0,64);
88+
AssertBytes.equal(resultBytes, testBytes, "Multiple (*2) of 32 bytes slice failed.");
8189

8290
// Now we're going to test for slicing actions that throw present in the functions below
8391
// with a ThrowProxy contract

0 commit comments

Comments
 (0)