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

Overwrites when assigning to bytes elements #212

Closed
axic opened this issue Nov 16, 2015 · 1 comment
Closed

Overwrites when assigning to bytes elements #212

axic opened this issue Nov 16, 2015 · 1 comment
Labels

Comments

@axic
Copy link
Member

axic commented Nov 16, 2015

Apologies if this is more of a question for EVM, than Solidity.

Basically what I am doing is trying to find the cheapest (in gas) way to build up a string (using dynamic bytes), but in a non-linear fashion.

The following works:

string memory holder = "     ";
bytes memory ret = bytes(holder);
ret[0] = 0x30;
ret[1] = 0x31;
ret[2] = 0x32;
return string(ret);

Output is: 30313220

But the following will be truncated to the last access:

string memory holder = "     ";
bytes memory ret = bytes(holder);
ret[2] = 0x32;
ret[1] = 0x31;
ret[0] = 0x30;
return string(ret);

Output is: 30

Of course I have digged deeper and found out the very simple reason. Solidity compiles the above into MLOAD + MSTORE instructions, and those according to the the yellow paper, will set the size of the memory to that index (truncate). Therefore by the last ret[0] it will be truncated to 1 entries.

Simple way to confirm this behaviour:

string memory holder = "      ";
bytes memory ret = bytes(holder);
ret[2] = 0x32;
ret[1] = 0x31;
ret[0] = 0x30;
ret[3] = 0x20;
return string(ret);

Output is: 30000020

What is the best way to do this properly? Mappings work, at a cost.

@chriseth chriseth changed the title Out-of-order array access Overwrites when assigning to bytes elements Nov 16, 2015
@chriseth
Copy link
Contributor

This is a bug in the compiler and will be fixed shortly. The reason is not that the length is changed, the reason is that adjacent memory locations are overwritten by the assignments.

chriseth pushed a commit to chriseth/solidity that referenced this issue Dec 7, 2015
ca46cb5 updated examples
aff3497 updated icap example
9fa9b16 gulp
61f1ba6 Merge pull request ethereum#224 from alexvandesande/prefix-name-reorg
448dd30 Merge branch 'master' into develop
7753724 build files
fc3dc7a build files
c9ebd7e version 0.5.0
448cf03 Merge branch 'master' into develop
43e8f0e Merge pull request ethereum#207 from ethereum/icap
0a56733 updated icap example
e67e705 Merge pull request ethereum#223 from ethereum/revert-222-master
f229f4e Revert "sync sendTransaction returning tx address"
ca58837 Merge pull request ethereum#222 from jesuscript/master
66a2b6c sync sendTransaction returning tx address
b19e46c updated "deposit" method description, updated icap example contract abi
fbb9a41 Merge branch 'develop' into icap
3bb6e4f sha3 backward compatibility. ethereum#205
d0be181 fixed const functions calls handling errors
858d0c6 lint
95aabe3 sendIBANTransaction && tests
5866f08 milli should have two l's
bacb03c Rename Kwei to kwei, added support for some SI base units for ether
dfd5060 use "official" namereg, updated examples
d8ad2b7 Merge branch 'develop' into icap
3fb420f Merge branch 'master' into develop
ea4d66e updated examples
e6209c6 Merge branch 'master' into develop
71ae809 version 0.4.3
92e2a2f Merge branch 'master' into develop
d03bec6 decoding of empty array, fixed ethereum#210, fixed ethereum#211
9abf38a fixed encoding of empty arrays
2ad458c Merge pull request ethereum#212 from ethereum/estimateGas
be2e93f build
d4bf850 fixed typo
0594e7f add estimateGas to contract methods and fixed sendTransaction return value
55c4653 test/isIBAN.js
e9483a6 icap.html example
6fb04d8 namereg example allows to register custom names
60c9bf8 removed natspec example, added namereg example
cd773fc updated docs
4af0085 web3.eth.namereg contract, icap in progress
02556ea removed unnecessary file
d320552 crypto-js integrated into project
adf91df sha3 init

git-subtree-dir: libjsqrc/ethereumjs
git-subtree-split: ca46cb5c94da4d37e9f4a5b8f6c0d117b72668d7
axic pushed a commit that referenced this issue Nov 20, 2018
Precompiled contract for pairing check.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants