Skip to content

Failed to encode CBOR: RangeError: Offset is outside the bounds of the DataView #3419

@lastmjs

Description

@lastmjs

Minimal Reproducible error:

https://github.com/demergent-labs/cbor_bug

Summary

There's a critical buffer management bug in the CBOR encoding implementation that occurs when encoding parameters with a very specific size and complexity. The bug manifests as RangeError: Offset is outside the bounds of the DataView during parameter serialization.

Core Problem

This bug is triggered by a buffer boundary condition where:

  1. The initial CBOR buffer is allocated with a fixed size (2048 bytes)
  2. Complex nested parameter structures fill the buffer to exactly its capacity
  3. When the encoder attempts to write additional header bytes, it exceeds the buffer bounds
  4. The buffer expansion logic fails to trigger at this specific boundary

Reproduction Difficulty

This bug is extremely sensitive to parameter structure and size:

  • Any reduction in parameter complexity → buffer usage drops below the boundary → works fine
  • Any addition to parameter complexity → buffer usage exceeds boundary, triggering proper expansion → works fine
  • Only this exact configuration → hits the precise boundary where expansion fails

Minimal Reproduction Case

The attached reproduction case contains:

  • A Rust canister with the exact type signature that triggers the bug
  • TypeScript test that calls the canister with the problematic arguments
  • The same setup in Rust CDK and Azle cause the same problem, confirming this is an @dfinity/agent issue

Technical Details

  • Problematic payload size: ~1968 bytes of argument data
  • Buffer size: 2048 bytes (exactly filled including CBOR overhead)
  • Failure point: Attempt to write at offset 2048 in a 0-2047 buffer
  • Root cause: Buffer expansion logic doesn't seem to handle this specific boundary condition

Original Logs:

See this test run: https://github.com/demergent-labs/azle/actions/runs/17111770847/job/48534604097?pr=3417

Running test: vec uo0Zg

 test vec uo0Zg failed InputError (Input): Failed to encode CBOR: RangeError: Offset is outside the bounds of the DataView
    at DataView.prototype.setUint8 (<anonymous>)
    at encodeHeader (/home/runner/work/azle/azle/node_modules/@dfinity/cbor/src/encode/encode.ts:165:16)
    at encodeBytes (/home/runner/work/azle/azle/node_modules/@dfinity/cbor/src/encode/encode.ts:240:16)
    at encodeTextString (/home/runner/work/azle/azle/node_modules/@dfinity/cbor/src/encode/encode.ts:270:3)
    at <anonymous> (/home/runner/work/azle/azle/node_modules/@dfinity/cbor/src/encode/encode.ts:158:5)
    at Array.forEach (<anonymous>)
    at encodeMap (/home/runner/work/azle/azle/node_modules/@dfinity/cbor/src/encode/encode.ts:157:14)
    at encodeItem (/home/runner/work/azle/azle/node_modules/@dfinity/cbor/src/encode/encode.ts:137:5)
    at <anonymous> (/home/runner/work/azle/azle/node_modules/@dfinity/cbor/src/encode/encode.ts:159:5)
    at Array.forEach (<anonymous>), input: [object Object]

To reproduce this exact test case, run:
cd /home/runner/work/azle/azle/examples/stable/test/property/candid_rpc/vec
AZLE_PROPTEST_SEED=-1239416472 AZLE_PROPTEST_PATH="0" AZLE_VERBOSE=true AZLE_DEV_TEMPLATE=true npm test
/home/runner/work/azle/azle/src/stable/test/property/default_prop_test_params.ts:20
                throw new Error(
                      ^


Error: To reproduce this exact test case, run:
cd /home/runner/work/azle/azle/examples/stable/test/property/candid_rpc/vec
AZLE_PROPTEST_SEED=-1239416472 AZLE_PROPTEST_PATH="0" AZLE_VERBOSE=true AZLE_DEV_TEMPLATE=true npm test

Also this run
https://github.com/demergent-labs/azle/actions/runs/17115903472/job/48546659291?pr=3421

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions