jesseduffield / jesseduffield/lazygit
panic: runtime error: index out of range [0] with length 0 in StatusManager.GetStatusString
- Dominant language
- Go
- Stars
- 82.4k
- Forks
- 3k
- Avg merge
- 2d 18h
- Merged PRs (30d)
- 19
Description
`StatusManager.GetStatusString` (and `HasStatus`) read `self.statuses` without holding the mutex, while `addStatus` and `removeStatus` both do hold it. This creates a TOCTOU race: the `len == 0` check passes, another goroutine calls `removeStatus` between the check and the `self.statuses[0]` access, and the index-out-of-range panic results.
**Stack trace:**
```
panic: runtime error: index out of range [0] with length 0
goroutine 2424671 [running]:
github.com/jesseduffield/lazygit/pkg/gui/status.(*StatusManager).GetStatusString(...)
pkg/gui/status/status_manager.go:76
github.com/jesseduffield/lazygit/pkg/gui/controllers/helpers.(*AppStatusHelper).renderAppStatus.func1(...)
pkg/gui/controllers/helpers/app_status_helper.go:106
```
**Fix:** lock the mutex for the full read in both `GetStatusString` and `HasStatus`:
```go
func (self *StatusManager) GetStatusString(userConfig *config.UserConfig) (string, gocui.Attribute) {
self.mutex.Lock()
defer self.mutex.Unlock()
if len(self.statuses) == 0 {
return "", gocui.ColorDefault
}
// ...
}
func (self *StatusManager) HasStatus() bool {
self.mutex.Lock()
defer self.mutex.Unlock()
return len(self.statuses) > 0
}
```
Contributor guide
Assessment
This issue has not been assessed yet.