aionnetwork / AVM

Enabling Java code to run in a blockchain environment
https://theoan.com/
MIT License
49 stars 25 forks source link

[CLOSED] shadow String constructor that takes a String parameter seems not working properly #262

Closed aionbot closed 5 years ago

aionbot commented 5 years ago

Issue created by nancyaion (on Thursday Sep 27, 2018 at 15:17 GMT)

Figured out with the test shadow.PrimitiveShadowingTest, for example, in ByteTest.Decode.main()

check(new String(""+Byte.MIN_VALUE), Byte.MIN_VALUE); // does not work

check(String.valueOf(Byte.MIN_VALUE), Byte.MIN_VALUE); // works

aionbot commented 5 years ago

Comment by JunhanHu-aion (on Thursday Sep 27, 2018 at 15:19 GMT)

This is caused by invokeDynamic, (String + int) is not working properly. This is a known issue for string concat

aionbot commented 5 years ago

Comment by nancyaion (on Thursday Sep 27, 2018 at 15:26 GMT)

Thanks Junhan. Changing the title.

aionbot commented 5 years ago

Comment by nancyaion (on Thursday Sep 27, 2018 at 15:53 GMT)

Also not work of (String + other primitive types)

aionbot commented 5 years ago

Comment by nancyaion (on Thursday Sep 27, 2018 at 20:35 GMT)

Actually the issue is still related to the shadow String constructor. public String(String original) {

IHelper.currentContractHelper.get().externalChargeEnergy(RuntimeMethodFeeSchedule.String_avm_constructor_1);

    this.v = new java.lang.String(original.v);
}`

The problem is gone after replacing original.v to original.getUnderlying(). It is because the fields of the shadow String original is not yet loaded when original.v is referenced and that causes the null pointer exception. Instead, getUnderlying() method does lazyLoad() so the problem is gone. It is recommended to use the methods to access the shadow class fields, not directly access them.