mirror of
https://github.com/apple/container.git
synced 2026-09-27 01:21:04 -04:00
fix: clamp ProgressBar barLength to non-negative value to avoid crash (#1525)
- Closes #1547. - A negative progress value (e.g. from a race in progress events) would produce a negative barLength and crash inside String(repeating:count:). Wrap the computed length in max(0, ...) and add a regression test that calls set(size: -10) and exercises draw(state:detail:).
This commit is contained in:
@@ -275,7 +275,7 @@ extension ProgressBar {
|
||||
// 45 reserves space for components rendered after the bar (size, speed, time, etc.)
|
||||
let usedWidth = (useColor ? joinedComponents.visibleLength : joinedComponents.count) + 45
|
||||
let remainingWidth = max(config.width - usedWidth, 1)
|
||||
let barLength = min(remainingWidth, state.finished ? remainingWidth : Int(Int64(remainingWidth) * value / total))
|
||||
let barLength = min(remainingWidth, max(0, state.finished ? remainingWidth : Int(Int64(remainingWidth) * value / total)))
|
||||
let barPaddingLength = remainingWidth - barLength
|
||||
if useColor {
|
||||
let filledBar = EscapeSequence.colored(String(repeating: config.theme.bar, count: barLength), EscapeSequence.green)
|
||||
|
||||
Reference in New Issue
Block a user