Skip to content

修复:IndeterminateProgressScaleWriter 在 lead 参数为空时的死循环问题#159

Open
LovageSunny wants to merge 2 commits into
p-ranav:masterfrom
LovageSunny:master
Open

修复:IndeterminateProgressScaleWriter 在 lead 参数为空时的死循环问题#159
LovageSunny wants to merge 2 commits into
p-ranav:masterfrom
LovageSunny:master

Conversation

@LovageSunny
Copy link
Copy Markdown

修复:IndeterminateProgressScaleWriterlead 参数为空时的死循环问题

问题背景 (Problem)

IndeterminateProgressScaleWriterlead 选项被设置为空字符串 "" 时,程序会陷入死循环,导致 CPU 占用过高且无法正常渲染进度条。

根本原因 (Root Cause)

stream_helper.hppwrite() 方法中,循环通过 i += unicode::display_width(next) 来推进位置。

  • 如果 lead""unicode::display_width("") 返回值为 0
  • 这导致 i += 0,索引 i 永远无法到达目标位置。
  • 循环会不断命中 else if (i == progress) 分支,陷入无限循环。

解决方案 (Solution)

在计算出 current_display_width 后增加一次校验:

if (current_display_width == 0) {
    current_display_width = 1;
}

逻辑说明: 这确保了循环步进值至少为 1。对于正常的非空字符串(宽度 $\ge 1$),原有行为保持不变;对于空字符串,此改动可以强制循环向下推进,从而打破死循环。

测试验证 (Verification)

新增测试程序 samples/test_indeterminate_lead_fix.cpp,涵盖了以下场景:

  1. 空 lead 居中:验证在进度条中间位置时不再卡死。
  2. 正常 lead ("<==>"):回归测试,确保原有功能不受影响。
  3. 空 lead 在末尾:验证边界情况下的渲染逻辑。
  4. 空 lead 在起始:验证初始状态下的渲染逻辑。

结果: 所有测试用例均通过断言,程序正常退出,输出长度符合预期。

…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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant