tact-lang / tact

Tact compiler main repository
https://tact-lang.org
MIT License
280 stars 56 forks source link

Incorrect bit length in `.storeUint()` and `.storeInt()` functions fails silently in presence of serialization/storage modifiers when doing parsing via `Struct.fromCell()` #472

Open novusnota opened 4 days ago

novusnota commented 4 days ago

Consider the following snippet:

import "@stdlib/deploy";

struct Coin {
    first: Int as coins;
    second: Int as uint32;
}

fun directParse(payload: Cell): Coin {
    return Coin.fromCell(payload);
}

contract Example with Deployable {
    receive("example") {
        let coin = directParse(
            beginCell()
            .storeCoins(42)
            .storeUint(35, 31) // here, I only specified 31 bit for storing the Uint, but could've specified 33 bits and get the same issue
            .endCell()
        );
        dump(coin.first); // would display nothing!
        dump(coin.second); // neither would this!
    }
}

Although it's up to user to correctly specify serialization modifiers, such as uint32 and then use .storeUint(NUM_TO_STORE, 32) and similar, it would be nice to display an error when mistakes occur.

This may be an edge case of some bigger issue.

Also it's unknown what value coin has in the showcased example, because an attempt to dump(coin) will be prohibited as dump() doesn't support Structs and Messages directly and type of coin is still considered to be Coin.


P.S.: Kinda offtopic, but this reminded me that we may consider adding .storeUint32(), .storeUint64() and similar functions for convenience. Or, to make a special function for Ints, which would accept serialization modifiers, like so:

// Option 1
beginCell().storeValue(VALUE as uint32);

// Option 2
beginCell().storeValue(VALUE, uint32);
Gusarich commented 23 hours ago

What do you mean by "fails silently"? I've just ran that code and it resulted exit code 9 - just as expected.

anton-trunov commented 22 hours ago

@Gusarich Let's add a failing test to the test suite to resolve the issue

anton-trunov commented 22 hours ago

@novusnota So it looks like indeed the dumps do not work because execution fails with the Cell Underflow error message

novusnota commented 22 hours ago

What do you mean by "fails silently"?

Testing in Blueprint via Sandbox and Jest don't show anything if one is looking at dump() output. I see that tact-emulator shows the error, but most of the Tact users are working in Blueprint

P.S.: It would be nice to synchronize those two (tact-emulator and sandbox) in the future somehow

anton-trunov commented 22 hours ago

@novusnota Please open an issue in Blueprint's repo (if it's somehow a sandbox issue we'll just move it there)

anton-trunov commented 22 hours ago

P.S.: It would be nice to syncronize those two (tact-emulator and sandbox) in the future somehow

It should be Sandbox

novusnota commented 22 hours ago

@novusnota Please open an issue in Blueprint's repo (if it's somehow a sandbox issue we'll just move it there)

https://github.com/ton-org/blueprint/issues/129

anton-trunov commented 22 hours ago

@novusnota Thanks!

Btw, here is a new related issue: #510