Skip to content

fix(stdlib): return error instead of panicking on windows size 0 🪟#170

Merged
timfennis merged 1 commit into
masterfrom
bugfix/windows-zero-size
Jun 2, 2026
Merged

fix(stdlib): return error instead of panicking on windows size 0 🪟#170
timfennis merged 1 commit into
masterfrom
bugfix/windows-zero-size

Conversation

@timfennis
Copy link
Copy Markdown
Owner

What

windows(seq, 0) panicked the VM instead of returning an error:

$ echo 'windows([1,2,3], 0)' | ndc
thread 'main' panicked at ndc_stdlib/src/sequence.rs:716:18:
window size must be non-zero

The stdlib windows function took length: i64, cast it to usize, and passed it straight to slice::windows(length) — which panics when the size is 0. Negative input was also wrong: it wrapped to a huge usize and silently returned [].

The sibling chunks function already handles both cases correctly (a length == 0 guard plus a usize parameter so negatives are rejected at argument conversion). This change brings windows in line.

Changes

  • ndc_stdlib/src/sequence.rs: windows now takes length: usize and returns an error when length == 0. Negatives are now rejected at arg conversion (arg 1: expected a non-negative integer), matching chunks.
  • tests/functional/programs/900_bugs/bug0024_windows_zero_size.ndc: regression test asserting the graceful window size must be non-zero error.

Context

Found while hunting for a way to panic the pipeline (lexer → parser → analyser → compiler → VM). The existing token-stream fuzzer never reaches this code because it only generates the identifiers a,b,c,x,f,g, so it never calls any stdlib function by name — the whole stdlib surface is outside its reach.

Behaviour unchanged on the happy path: windows([1,2,3,4], 2)[[1,2],[2,3],[3,4]], and a too-short sequence still yields [].

🤖

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@timfennis timfennis merged commit 5fc2d9c into master Jun 2, 2026
1 check passed
@timfennis timfennis deleted the bugfix/windows-zero-size branch June 2, 2026 05:55
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