argotorg / argotorg/solcore

abi_decode returns the length and not the payload

Open Beginner friendly
#577 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Haskell
Stars
113
Forks
9
Avg merge
4d 9h
Merged PRs (30d)
1

Description

BUG: abi_encode returns memory(bytes) laid out as [length | payload].
abi_decode should decode its payload, but the memory(bytes) WordReader starts at the length.
Calling roundTrip(42) therefore returns 32, not 42.

```solididy
import std.{*};
import std.dispatch.{*};

contract AbiDecodeBufferOrigin {
public function roundTrip(value: uint256) -> uint256 {
let encoded: memory(bytes) = abi_encode(value);
return abi_decode(encoded, Proxy:Proxy(uint256), Proxy:Proxy(MemoryWordReader)); // returns 32
}
}
```
SOLUTION: change memory(bytes):HasWordReader.getWordReader in std/std.solc to return MemoryWordReader(Typedef.rep(x) + 32).
The first word is the in-memory byte length; ABI decoding must begin at the payload.

PATCH: https://github.com/argotorg/solcore-rs/blob/8d5576699a8ca139e43b72c39f4545d3d2312de1/std/std.solc#L1201
```diff
- return MemoryWordReader(Typedef.rep(x));
+ return MemoryWordReader(Typedef.rep(x) + 32);
```

Contributor guide

No contributing guide indexed for this repository

Research direction

Start in std/std.solc at memory(bytes):HasWordReader.getWordReader, around line 1201, and inspect how MemoryWordReader is initialized. Verify the roundTrip(42) example through abi_encode and abi_decode; done means decoding starts after the 32-byte length word and returns 42 rather than 32.

Written by the indexing model from the issue text.

Assessment

Tech stack
solidity
Domain
compilers
Issue type
Bug
Difficulty
1/5
Estimated time
Under an hour
Activity status
Active
Clarity
Clearly specified
Newbie friendliness
92/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.