Skip to content

fix: guard ProgressBarState.String against out-of-range values#1731

Open
SAY-5 wants to merge 1 commit into
charmbracelet:mainfrom
SAY-5:fix-progressbarstate-string-panic
Open

fix: guard ProgressBarState.String against out-of-range values#1731
SAY-5 wants to merge 1 commit into
charmbracelet:mainfrom
SAY-5:fix-progressbarstate-string-panic

Conversation

@SAY-5

@SAY-5 SAY-5 commented Jul 10, 2026

Copy link
Copy Markdown
  • I have read CONTRIBUTING.md.
  • I have created a discussion that was approved by a maintainer (for new features).

Summary

Closes #1711.

ProgressBarState.String() indexed a fixed length-5 array directly with the receiver, so any value outside [0, 4] panicked with an index-out-of-range error. Since State is an exported field on ProgressBar, a value can reach the method without going through NewProgressBar.

Fix

Bounds-check before indexing and return a formatted fallback (ProgressBarState(N)) for unknown values, matching the convention used by stringer-generated code.

Before / after

Before, ProgressBarState(5).String() panics. After, it returns "ProgressBarState(5)", and valid states are unchanged.

Added a table test covering the five defined states plus an out-of-range and a negative value.

Signed-off-by: Sai Asish Y <say.apm35@gmail.com>
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.

ProgressBarState.String() panics on out-of-range values

1 participant