Skip to content

Allow length 0 for string, buffer, wrapped and array - #320

Merged
keichi merged 1 commit into
masterfrom
fix-allow-zero-length
Oct 18, 2026
Merged

keichi merged 1 commit into
masterfrom
fix-allow-zero-length

Conversation

@keichi

@keichi keichi commented Oct 18, 2026

Copy link
Copy Markdown
Owner

Problem

Validation in string(), buffer(), wrapped() and array() checks length/lengthInBytes by truthiness, so a literal 0 is treated as missing.

Repro

new Parser().buffer("b", { length: 0 });
// Error: length or readUntil must be defined for buffer.

Same for string("s", { length: 0 }) and array("a", { length: 0, type: "uint8" }).

Fix

  • Use == null checks for length/lengthInBytes in the validations (including the greedy mutual-exclusion and stripNull checks).
  • generateString used truthiness to pick the length branch; switched to != null.
  • For length + zeroTerminated, check the length bound before reading a byte, so length 0 no longer consumes one byte (behavior for length >= 1 is unchanged).
  • generateArray already compared against undefined, so length 0 arrays work once validation passes.

Added regression tests for length 0 strings (plain, zeroTerminated, stripNull), buffers and arrays (length and lengthInBytes).

@keichi
keichi merged commit a7e1a7d into master Oct 18, 2026
8 checks passed
@keichi
keichi deleted the fix-allow-zero-length branch October 18, 2026 13:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant