修复:IndeterminateProgressScaleWriter 在 lead 参数为空时的死循环问题#159
Open
LovageSunny wants to merge 2 commits into
Open
Conversation
…inateProgressBar constructor - Add guard in ProgressScaleWriter::write() to prevent infinite loop when fill, lead, or remainder has zero display width (same pattern as the IndeterminateProgressScaleWriter fix) - Guard IndeterminateProgressBar constructor: when Lead is empty, set max_progress_ to bar_width instead of bar_width + start + end - Add comprehensive test covering both ProgressScaleWriter and IndeterminateProgressScaleWriter with empty strings
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.
修复:
IndeterminateProgressScaleWriter在lead参数为空时的死循环问题问题背景 (Problem)
当
IndeterminateProgressScaleWriter的lead选项被设置为空字符串""时,程序会陷入死循环,导致 CPU 占用过高且无法正常渲染进度条。根本原因 (Root Cause)
在
stream_helper.hpp的write()方法中,循环通过i += unicode::display_width(next)来推进位置。lead为"",unicode::display_width("")返回值为0。i += 0,索引i永远无法到达目标位置。else if (i == progress)分支,陷入无限循环。解决方案 (Solution)
在计算出
current_display_width后增加一次校验:逻辑说明: 这确保了循环步进值至少为 1。对于正常的非空字符串(宽度$\ge 1$ ),原有行为保持不变;对于空字符串,此改动可以强制循环向下推进,从而打破死循环。
测试验证 (Verification)
新增测试程序
samples/test_indeterminate_lead_fix.cpp,涵盖了以下场景:结果: 所有测试用例均通过断言,程序正常退出,输出长度符合预期。