Union deserialize on empty input throws TypeError instead of a decode error

Open Beginner friendly
#567 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
78/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
typescript
Domain
data

Research direction

Start in the union type's value_deserializeFromBytes, where data.uint8Array[start] is read as the selector with no length check, then follow the this.types[selector] lookup. Add a check that at least one byte is available and throw a decode Error like the other size errors. Done when new UnionType([...]).deserialize(new Uint8Array([])) throws a clear Error rather than a TypeError, and the existing union tests still pass.

Written by the indexing model from the issue text.

Description

Describe the bug

UnionType.deserialize on an empty Uint8Array throws a raw TypeError instead of a stable SSZ decode error.

Root cause: value_deserializeFromBytes reads data.uint8Array[start] with no length check. On an empty buffer that value is undefined. The selector bounds check (selector > this.maxSelector) is false for undefined, so the code indexes this.types[undefined] and calls .value_deserializeFromBytes on it.

const selector = data.uint8Array[start];
if (selector > this.maxSelector) {
  throw Error(`Invalid selector ${selector}`);
}
return {
  selector,
  value: this.types[selector].value_deserializeFromBytes(...),
};

Expected behavior

Reject empty / truncated Union input with a clear decode error (for example requiring at least 1 byte for the selector), similar to how other types report size errors. Do not throw TypeError: Cannot read properties of undefined.

Steps to Reproduce

import { UnionType, UintNumberType } from "@chainsafe/ssz";

const type = new UnionType([new UintNumberType(1), new UintNumberType(2)]);
type.deserialize(new Uint8Array([]));

Actual:

TypeError: Cannot read properties of undefined (reading 'value_deserializeFromBytes')

A one-byte selector with no payload already fails with a proper Error (BasicType invalid size ...), so only the empty-buffer path hits the TypeError.

Version

Reproduced on @chainsafe/ssz 1.9.1 (current master tip) with Node.js v22.23.4.

Related but different: #507 (None variant trailing bytes). This report is only about empty input producing an uncaught TypeError.

Dominant language
TypeScript
Stars
62
Forks
27
Avg merge
2h 35m
Merged PRs (30d)
2

Getting set up

  • No Dockerfile or Docker Compose file
  • Has a pull request template
  • No contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from ChainSafe/ssz

All issues in ChainSafe/ssz

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.