neo-devpack-dotnet icon indicating copy to clipboard operation
neo-devpack-dotnet copied to clipboard

AllowOverflow option to avoid checking int range

Open Hecate2 opened this issue 1 year ago • 13 comments

unchecked((int)bigInteger)) Will conflict. Draft for now.

Hecate2 avatar Oct 16 '24 03:10 Hecate2

Changed the methodology. Skip range checks and throws with compilation option

Hecate2 avatar Oct 25 '24 04:10 Hecate2

Need a way to compile neo-devpack-dotnet\tests\Neo.Compiler.CSharp.TestContracts\Contract_BigInteger.cs with SimulateOverflow=true

Hecate2 avatar Oct 25 '24 04:10 Hecate2

Need a way to compile neo-devpack-dotnet\tests\Neo.Compiler.CSharp.TestContracts\Contract_BigInteger.cs with SimulateOverflow=true

Allowed CompilationOptions to be used in tests. TestGasConsume is set to false.

Hecate2 avatar Oct 25 '24 05:10 Hecate2

This name is so scary....

Jim8y avatar Oct 29 '24 12:10 Jim8y

this can save tons of gas, yes,,,,,, but,,,,,,,

Jim8y avatar Oct 29 '24 12:10 Jim8y

rather than allow overflow, i think biginteger only is better

Jim8y avatar Oct 29 '24 16:10 Jim8y

We should not save gas if the security is involved

shargon avatar Oct 29 '24 22:10 shargon

Sorry miss click

shargon avatar Oct 29 '24 22:10 shargon

We should not save gas if the security is involved

No security problem is involved. This is just handling all int, long, short as BigInteger.

Hecate2 avatar Oct 30 '24 00:10 Hecate2

rather than allow overflow, i think biginteger only is better

Sometimes it's impossible to write BigInteger everywhere literally in codes. For example, shifting operations (>> and <<) in C# supports only int as the shifted count of bits. Also indexes of arrays have to be int. Then we have to write an explicit coversion (int) which wastes a range check.

Hecate2 avatar Oct 30 '24 00:10 Hecate2

And our current default settings (with or without my PR) are --checked=false (--allow-overflow=false). This allows silent overflows (from int.MaxValue to int.MinValue, without or without my PR). I think this is even more dangerous. Maybe it's better to have default as --checked=false --allow-overflow=true, or --checked=true --allow-overflow=false

Hecate2 avatar Oct 30 '24 02:10 Hecate2

we should detect unchecked keyword

shargon avatar Oct 30 '24 09:10 shargon

we should detect unchecked keyword

We are actually detecting it (with or without this PR). But by default everything is unchecked.

Hecate2 avatar Oct 30 '24 09:10 Hecate2

rather than allow overflow, i think biginteger only is better

Sometimes it's impossible to write BigInteger everywhere literally in codes. For example, shifting operations (>> and <<) in C# supports only int as the shifted count of bits. Also indexes of arrays have to be int. Then we have to write an explicit coversion (int) which wastes a range check.

excatly, just use what is allowed in biginteger.

Jim8y avatar Nov 01 '24 02:11 Jim8y

@shargon please review

Jim8y avatar Nov 13 '24 03:11 Jim8y

This is an option that should be false by default, so after you @shargon reviewed, please dont merge it, i will update the test to remove the option and revert gas change.

Jim8y avatar Nov 13 '24 03:11 Jim8y

I still don't know why we need this option, which is the use case?

shargon avatar Nov 13 '24 20:11 shargon

I still don't know why we need this option, which is the use case?

where user can handle the integer range and no need to do automatic range check. currently, every numerous operation requires a range check, its expensive.

Jim8y avatar Nov 14 '24 01:11 Jim8y

I still don't know why we need this option, which is the use case?

where user can handle the integer range and no need to do automatic range check. currently, every numerous operation requires a range check, its expensive.

expensive not because it's required for safety

shargon avatar Nov 14 '24 08:11 shargon

Can we discuss this pe in the meeting?

shargon avatar Nov 21 '24 08:11 shargon

Can we discuss this pe in the meeting?

That's really great

Hecate2 avatar Nov 21 '24 08:11 Hecate2

gas examples to demonstrate gas saving. /or use biginteger as much as possible

Jim8y avatar Nov 21 '24 13:11 Jim8y

Take a look to https://github.com/neo-project/neo-devpack-dotnet/pull/1249

shargon avatar Nov 21 '24 13:11 shargon

close as can be partially replaced by using BigInteger, though not fully.

Jim8y avatar Dec 12 '24 07:12 Jim8y