Skip to content

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

@MBpanzz

Description

@MBpanzz

Describe the bug

Calling ProgressBarState.String() appears to panic when the receiver value is outside the [0, 4] range.

The method looks like this:

func (s ProgressBarState) String() string {
    return [...]string{
        "None",
        "Default",
        "Error",
        "Indeterminate",
        "Warning",
    }[s]
}

ProgressBarState is defined as type ProgressBarState int, and only five constants (0–4) are defined. The array literal is fixed at length 5 and indexed directly with s, so a value outside that range might cause an index-out-of-range panic.

ProgressBar.State is also an exported field, so it might be possible to assign arbitrary values without going through the NewProgressBar constructor.


Setup

  • OS: not terminal-specific
  • Shell: not terminal-specific
  • Terminal Emulator: not terminal-specific
  • Terminal Multiplexer: not terminal-specific

The issue appears to be in pure Go runtime code, unrelated to terminal environment.


To Reproduce

  1. Create a file main.go with the following content:
package main

import tea "charm.land/bubbletea/v2"

func main() {
    var s tea.ProgressBarState = 5
    println(s.String())
}
  1. Run go run main.go

  2. Observe the output:

panic: runtime error: index out of range [5] with length 5

Negative values seem to behave similarly — replacing 5 with -1 produces:

panic: runtime error: index out of range [-1]

Source Code

// tea.go, around line 321
func (s ProgressBarState) String() string {
    return [...]string{
        "None",
        "Default",
        "Error",
        "Indeterminate",
        "Warning",
    }[s]
}

Expected behavior

Unsure if this is intentional. The method might simply never be called with unexpected values in practice, in which case this is a non-issue. If out-of-range inputs are a realistic concern, some bounds protection might be needed so the method doesn't panic.


Screenshots

N/A


Additional context

N/A

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions