fix: clamp ProgressBar barLength to non-negative value to avoid crash#1525
Open
ErwanLegrand wants to merge 1 commit into
Open
fix: clamp ProgressBar barLength to non-negative value to avoid crash#1525ErwanLegrand wants to merge 1 commit into
ErwanLegrand wants to merge 1 commit into
Conversation
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:).
jglogan
requested changes
May 11, 2026
| 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))) |
Contributor
There was a problem hiding this comment.
@dkovba I'd welcome suggestions on how to make this more readable. The clamp makes sense in terms of preventing a negative bar length, but the way it's written, it's pretty complicated to reason about.
There is no need to include the entire thing in the clamp since remainingWidth is already guaranteed to be greater than 1.
The value Int(Int64(remainingWidth) * value / total)) should be extracted to its own let assignment with a constant name that makes sense.
This would make more sense to me if we did something like:
// reserve spaces between components, plus 45 for components rendered after the bar (size, speed, time, etc.)
let reservedWidth = (useColor ? joinedComponents.visibleLength : joinedComponents.count) + 45
let completedBarWidth = max(config.width - usedWidth, 1)
let progressWidth = max(0, Int(Int64(completedBarWidth) * value / total))
let currentBarWidth: Int
if state.finished {
currentBarWidth = completedBarWidth
} else {
currentBarWidth = min(completedBarWidth, progressWidth)
}
jglogan
requested changes
May 11, 2026
Contributor
jglogan
left a comment
There was a problem hiding this comment.
Fix looks good but let's make the logic a little more readable. Thanks!
Code Coverage
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:).
Type of Change
Motivation and Context
This fixes an Apple container crash
Testing