ssz icon indicating copy to clipboard operation
ssz copied to clipboard

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

Open kutluhaneth46 opened this issue 1 week ago • 0 comments

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.

kutluhaneth46 avatar Nov 23 '26 13:11 kutluhaneth46