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

Sway functions which take arrays as inputs must validate length of array #6895

Closed
zees-dev opened this issue Feb 5, 2025 · 1 comment
Closed
Labels
bug Something isn't working triage This issue was opened with a template and needs to be triaged by code owners.

Comments

@zees-dev
Copy link
Contributor

zees-dev commented Feb 5, 2025

Related Component

compiler

Problem

As part of a PR review - have encountered a weird edge case for sway functions which take in list of elements - e.g.:

fn test_array(input: [u64; 10]) -> [u64; 10] {
    input
}

As part of this test we've realized that the function can be called with an input array which is less than length of 10 (e.g. [42, 42])
^ which seems to return:
[42, 42, 0, 4718592, 65536, 65536, 0, 0, 0, 0]

  • firstly id expect a failure here; i.e. revert or something since function should not be callable with lower-length input
  • secondly; i'd expect the remaining values returned to be zeroed out - e.g. [42, 42, 0, 0, 0, 0, 0, 0, 0, 0]

Steps

  1. Manually construct ContractCall struct with encoded args - which represent smaller length array than what the sway function requires
  2. Build tx and call contract using this struct
  3. Analyse the return value (using logdecoder and receiptparser)

Possible Solution(s)

  • The ideal solution would be to revert; as the input array length must match the sway function input arg
  • If we dont want to do the above; then i'd expect the returned array to have default values for the remaining list of items

Notes

This may not be replicable with the SDK when using abigen! macro on contracts since the compiler requires strict typing of input params.

  • The bug above was identified when calling contract functions with manually encoded input params - using the ContractCall struct directly.

Installed components

latest  - Update available (8 updates)
nightly - Update available (9 updates)
fuelup  - Up to date
@zees-dev zees-dev added bug Something isn't working triage This issue was opened with a template and needs to be triaged by code owners. labels Feb 5, 2025
@zees-dev
Copy link
Contributor Author

zees-dev commented Feb 6, 2025

This is a case of garbage-in-garbage-out; i.e. if you provide input lower than the desired length of the input function; the decoder will continue to read memory input (in this case) until the specified output length (of the return param) is reached.

@zees-dev zees-dev closed this as completed Feb 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working triage This issue was opened with a template and needs to be triaged by code owners.
Projects
None yet
Development

No branches or pull requests

1 participant