Skip to content

fix: guard InRange from empty/inverted ranges - #982

Merged
skevetter merged 1 commit into
mainfrom
pkg-system-platform/random-inrange-guard
Aug 10, 2026
Merged

skevetter merged 1 commit into
mainfrom
pkg-system-platform/random-inrange-guard

Conversation

@devsy-app

@devsy-app devsy-app Bot commented Aug 10, 2026

Copy link
Copy Markdown
Contributor

Package reviewed

pkg/random (system/platform/utility category).

Issue found

random.InRange(min, max) is implemented as rand.Intn(max-min) + min. rand.Intn panics when its argument is <= 0, so any caller passing an empty (max == min) or inverted (max < min) range crashes the whole program. The single current caller (pkg/credentials/start.go) always passes valid bounds, but the panic is a latent crash for any future misuse.

The package also had no test file at all — zero coverage of String or InRange.

Change

  1. pkg/random/random.go — add a guard at the top of InRange: when max <= min (degenerate/inverted range), return min instead of panicking. This is a small correctness fix; for valid inputs (max > min) behavior is unchanged.
  2. pkg/random/random_test.go (new) — adds table-driven tests:
    • String length (0/1/typical/long), charset (lowercase a–z only), and randomness (distinctness across 100 samples).
    • InRange bounds (1000 samples within [min, max)) and the degenerate case (equal/inverted bounds return min).

pkg/random now reports 100% statement coverage.

Verification performed

  • task cli:format — clean.
  • task cli:lint:ci — 0 issues (new-from-patch against origin/main).
  • task cli:test — pkg/random passes (ok ... coverage: 100.0%). The only failing suite is pkg/git (TestRepoClone*), a known pre-existing failure on origin/main unrelated to this change (this change does not touch pkg/git).

This PR was created by an AI agent as part of an automated daily package review job.

random.InRange(min, max) called rand.Intn(max-min), which panics when max <= min. Return min for degenerate ranges instead.
@netlify

netlify Bot commented Aug 10, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for devsydev canceled.

Name Link
🔨 Latest commit aa5c7c8
🔍 Latest deploy log https://app.netlify.com/projects/devsydev/deploys/6a79bbd477ed7b00075bae70

@netlify

netlify Bot commented Aug 10, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for images-devsy-sh canceled.

Name Link
🔨 Latest commit aa5c7c8
🔍 Latest deploy log https://app.netlify.com/projects/images-devsy-sh/deploys/6a79bbd4cf1364000827ba01

@codacy-production

Copy link
Copy Markdown

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

🟢 Metrics 19 complexity · 0 duplication

Metric Results
Complexity 19
Duplication 0

View in Codacy

AI Reviewer: run a review on demand. To trigger the first review automatically, go to your organization or repository integration settings. AI can make mistakes. Always validate suggestions.

Run reviewer

TIP This summary will be updated as you push new changes.

@skevetter
skevetter marked this pull request as ready for review August 10, 2026 13:41
@skevetter
skevetter merged commit 8006d88 into main Aug 10, 2026
67 checks passed
@skevetter
skevetter deleted the pkg-system-platform/random-inrange-guard branch August 10, 2026 13:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant