FuelLabs/fuels-ts · error · FuelError
DECODE_ERROR
DECODE_ERROR
Error message
Invalid std string data size.
What it means
Thrown by StdStringCoder.decode() when the data buffer is shorter than WORD_SIZE (8 bytes). A Sway std::string::String is encoded as a u64 character-count prefix followed by UTF-8 bytes, so at least 8 bytes are required to read the length prefix alone.
Source
Thrown at packages/abi-coder/src/encoding/coders/StdStringCoder.ts:25
import { Coder } from './AbstractCoder';
import { BigNumberCoder } from './BigNumberCoder';
export class StdStringCoder extends Coder<string, string> {
static memorySize = 1;
constructor() {
super('struct', 'struct String', WORD_SIZE);
}
encode(value: string): Uint8Array {
const bytes = toUtf8Bytes(value);
const lengthBytes = new BigNumberCoder('u64').encode(value.length);
return new Uint8Array([...lengthBytes, ...bytes]);
}
decode(data: Uint8Array, offset: number): [string, number] {
if (data.length < this.encodedLength) {
throw new FuelError(ErrorCode.DECODE_ERROR, `Invalid std string data size.`);
}
const offsetAndLength = offset + WORD_SIZE;
const lengthBytes = data.slice(offset, offsetAndLength);
const length = bn(new BigNumberCoder('u64').decode(lengthBytes, 0)[0]).toNumber();
const dataBytes = data.slice(offsetAndLength, offsetAndLength + length);
if (dataBytes.length !== length) {
throw new FuelError(ErrorCode.DECODE_ERROR, `Invalid std string byte data size.`);
}
return [toUtf8String(dataBytes), offsetAndLength + length];
}
}
View on GitHub (pinned to b3f37c91ac)
Solutions
- Verify data.length >= 8 before calling decode().
- Confirm you are using StdStringCoder (variable-length struct String) and not StringCoder (fixed-length str[N]).
- Ensure the data originates from a compatible Sway ABI encoding.
Example fix
// before
stdStringCoder.decode(new Uint8Array([0, 0, 0]), 0); // throws DECODE_ERROR
// after
if (data.length < 8) {
throw new Error('Data too short for std String decode');
}
stdStringCoder.decode(data, 0); Defensive patterns
Strategy: validation
Validate before calling
function canDecodeStdString(data: Uint8Array): boolean {
return data.length >= 8; // WORD_SIZE
} Prevention
- Distinguish StdStringCoder (struct String, variable-length) from StringCoder (str[N], fixed-length).
- Validate minimum buffer size before decoding.
- Log the coder type and data length when debugging decode errors.
When it happens
Trigger: Passing fewer than 8 bytes to decode(). Decoding data that was not produced by StdStringCoder.encode(). Receiving a truncated ABI payload from an RPC response.
Common situations: Using the wrong coder for the data (e.g., StringCoder fixed-length vs StdStringCoder variable-length). Malformed data from chain. Buffer allocation or slicing errors upstream.
Related errors
AI-assisted analysis of FuelLabs/fuels-ts@b3f37c91ac (2026-08-12).
Data as JSON: /api/errors/33ad57ddff7265da.
Report an issue: GitHub.