Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple fix of memory errors #47

Open
wants to merge 10 commits into
base: dev
Choose a base branch
from
9 changes: 7 additions & 2 deletions src/abi.c
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,7 @@ int eth_abi_address(struct eth_abi *abi, char **addr) {
memcpy(&(cframebuf->buf[cframebuf->offset + 12]), tmp, 20);
cframebuf->offset += ETH_ABI_WORD_SIZE;
cframebuf->len += ETH_ABI_WORD_SIZE;
free(tmp);
return 1;
}

Expand All @@ -326,8 +327,7 @@ int eth_abi_call(struct eth_abi *abi, char **fn, int *len) {
cframebuf = abi->cframe->buf;

if (abi->m == ETH_ABI_ENCODE) {
if (len == NULL)
fnlen = strlen(*fn);
fnlen = len ? *len : strlen(*fn);

if (eth_keccak256(keccak, (uint8_t*)*fn, fnlen) < 0)
return -1;
Expand Down Expand Up @@ -775,6 +775,11 @@ int eth_abi_from_hex(struct eth_abi *abi, char *hex, int len) {
if (ethc_abi_frame_init(&nframe) < 0)
return -1;

if (nframe->buf->buf != NULL) {
free(nframe->buf->buf);
nframe->buf->buf = NULL;
}

if ((len = eth_hex_to_bytes(&(nframe->buf->buf), hex, len)) < 0)
return -1;

Expand Down
6 changes: 5 additions & 1 deletion src/rlp.c
Original file line number Diff line number Diff line change
Expand Up @@ -563,11 +563,14 @@ int eth_rlp_from_hex(struct eth_rlp *dest, char *hex, int len) {
if ((sbuf = eth_hex_to_bytes(&buf, hex, len)) <= 0)
return -1;

if (eth_rlp_frame_init(&nframe, buf, sbuf) <= 0)
if (eth_rlp_frame_init(&nframe, buf, sbuf) <= 0) {
free(buf);
return -1;
}

dest->cframe = nframe;
dest->m = ETH_RLP_DECODE;
free(buf);
return 1;
}

Expand All @@ -579,5 +582,6 @@ int eth_rlp_free(struct eth_rlp *dest) {
cframe = dest->cframe;

free(cframe->buf);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is recursive free needed here?

free(cframe);
return 1;
}
4 changes: 3 additions & 1 deletion test/test-eth-abi.c
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ void test_eth_abi_mpint(void) {
ok(eth_abi_mpint(&abi0, mpz1) == 1);
ok(eth_abi_mpint(&abi0, mpz2) == 1);
ok(eth_abi_to_hex(&abi0, &hex, &hexlen) == 1);
ok(eth_abi_free(&abi0) == 1);

is(hex, "00000000000000000000000000000000000000000000000000000000000000ff"
"0000000000000000000000000000000000000000000000000000000000000fff"
Expand All @@ -268,6 +269,7 @@ void test_eth_abi_mpint(void) {
mpz_init_set_str(mpz0, "0x0000000000ffffffffffffffffffffffffffffffffffffffffffffffffffffff", 0);
mpz_init(mpz1);
ok(eth_abi_mpint(&abi1, mpz1) == 1);
ok(eth_abi_free(&abi1) == 1);
ok(mpz_cmp(mpz0, mpz1) == 0);
mpz_clears(mpz0, mpz1, NULL);
}
Expand Down Expand Up @@ -336,7 +338,7 @@ void test_eth_abi_bytes32() {
ok(eth_abi_from_hex(&abi1,
"f507a54fef38fbd2718fff5f6b54a65d57b079df170b0cd7985d9ce699c031c3", -1) == 1);
ok(eth_abi_bytes32(&abi1, b1) == 1);
ok(eth_abi_free(&abi0) == 1);
ok(eth_abi_free(&abi1) == 1);
cmp_mem(b1, b0, 32);
}

Expand Down
1 change: 1 addition & 0 deletions test/test-eth-bloom.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ void test_eth_bloom_or(void) {

eth_bloom_from_bytes(&bloom1, address, 20);
eth_bloom_from_bytes(&bloom2, topic, 32);
eth_bloom_init(&bloom3);

eth_bloom_or(&bloom3, &bloom1);
eth_bloom_or(&bloom3, &bloom2);
Expand Down
11 changes: 11 additions & 0 deletions test/test-eth-rlp.c
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ void test_eth_rlp_to_bytes(void) {
ok(eth_rlp_free(&rlp0) == 1);
cmp_mem(bout, bytes, boutlen);
ok(boutlen == 3);
free(bout);
}

void test_eth_rlp_decode_eip1559_tx(void) {
Expand Down Expand Up @@ -292,6 +293,7 @@ void test_eth_rlp_decode_eip1559_tx(void) {
ok(eth_rlp_hex(&rlp, &r, NULL));
ok(eth_rlp_hex(&rlp, &s, NULL));
ok(eth_rlp_array_end(&rlp));
ok(eth_rlp_free(&rlp) == 1);

// check
is(chain_id_hex, "aa36a7");
Expand All @@ -308,4 +310,13 @@ void test_eth_rlp_decode_eip1559_tx(void) {
ok(v == 1);
is(r, "99c06b4f79b805ae7be2dcd21191e470362c9d66b7cfea90b185015893a1477e");
is(s, "3c16ce20c94c5ee0598154007a67fae010769fe3db29a1e40ac9532a91835a0c");

free(chain_id_hex);
free(max_priority_fee_per_gas);
free(max_fee_per_gas);
free(gas_limit);
free(to_addr);
free(abi_hex);
free(r);
free(s);
}