earendil-works/pi · error · CborError

Decoded CBOR integer is outside the safe range

Error message

Decoded CBOR integer is outside the safe range

What it means

CborError thrown when decoding a negative integer (major type 1) whose value falls outside Number.isSafeInteger range — the payload encodes a magnitude beyond ±2^53-1, which cannot be represented exactly as a JS number.

Solutions

  1. Encode large integers as strings or bignums (CBOR tags) instead of native ints
  2. Keep integer values within the safe range on the encoding side
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at packages/protocol/src/cbor/decoder.ts:39 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of earendil-works/pi@4af9d21d3b (2026-08-24). Data as JSON: /api/errors/283dd00701246a32. Report an issue: GitHub.

Appendix: source

Thrown at packages/protocol/src/cbor/decoder.ts:39

		const value = this.readItem(0);
		if (this.offset !== this.bytes.byteLength) throw new CborError("CBOR payload contains trailing data");
		return value;
	}

	private readItem(depth: number): unknown {
		if (depth > this.options.maxDepth) {
			throw new CborError(`CBOR nesting depth exceeds configured limit of ${this.options.maxDepth}`);
		}
		const initial = this.readByte();
		const majorType = initial >>> 5;
		const additionalInformation = initial & 0x1f;

		switch (majorType) {
			case 0:
				return this.readArgument(additionalInformation);
			case 1: {
				const value = -1 - this.readArgument(additionalInformation);
				if (!Number.isSafeInteger(value)) throw new CborError("Decoded CBOR integer is outside the safe range");
				return value;
			}
			case 2: {
				const length = this.readLength(additionalInformation, "byte string", this.options.maxByteLength);
				return new Uint8Array(this.readBytes(length));
			}
			case 3: {
				const length = this.readLength(additionalInformation, "text string", this.options.maxByteLength);
				const bytes = this.readBytes(length);
				try {
					return textDecoder.decode(bytes);
				} catch (_error) {
					throw new CborError("CBOR text string contains invalid UTF-8");
				}
			}
			case 4: {
				const length = this.readLength(additionalInformation, "array", this.options.maxContainerLength);
				const result: unknown[] = [];

View on GitHub (pinned to 4af9d21d3b)