Skip to content

[kernel] fix vsnprintf - #11834

Open
luchenxu73 wants to merge 2 commits into
RT-Thread:masterfrom
luchenxu73:fix-printf
Open

luchenxu73 wants to merge 2 commits into
RT-Thread:masterfrom
luchenxu73:fix-printf

Conversation

@luchenxu73

@luchenxu73 luchenxu73 commented Sep 24, 2026 •

Copy link
Copy Markdown
Contributor

拉取/合并请求描述:(PR description)

[

为什么提交这份PR (why to submit this PR)

修复vsnprintf实现问题 #11833

你的解决方案是什么 (what is your solution)

  1. 新增边界检测
  2. 实现 [width].[precision]功能
  3. 优化测试用例

请提供验证的bsp和config (provide the config and bsp)

  • BSP: bsp/qemu-vexpress-a9
  • .config: CONFIG_RT_KLIBC_USING_VSNPRINTF_STANDARD

]

当前拉取/合并请求的状态 Intent for your PR

必须选择一项 Choose one (Mandatory):

  • 本拉取/合并请求是一个草稿版本 This PR is for a code-review and is intended to get feedback
  • 本拉取/合并请求是一个成熟版本 This PR is mature, and ready to be integrated into the repo

代码质量 Code Quality:

我在这个拉取/合并请求中已经考虑了 As part of this pull request, I've considered the following:

  • 已经仔细查看过代码改动的对比 Already check the difference between PR and old code
  • 代码风格正确,包括缩进空格,命名及其他风格 Style guide is adhered to, including spacing, naming and other styles
  • 没有垃圾代码,代码尽量精简,不包含#if 0代码,不包含已经被注释了的代码 All redundant code is removed and cleaned up
  • 所有变更均有原因及合理的,并且不会影响到其他软件组件代码或BSP All modifications are justified and not affect other components or BSP
  • 对难懂代码均提供对应的注释 I've commented appropriately where code is tricky
  • 代码是高质量的 Code in this PR is of high quality
  • 已经使用clang-format 源码格式化工具确保格式符合RT-Thread代码规范 This PR has been formatted with clang-format and complies with RT-Thread code specification
  • 如果是新增bsp, 已经添加ci检查到.github/ALL_BSP_COMPILE.json 详细请参考链接BSP自查

@github-actions github-actions Bot added the Kernel PR has src relate code label Sep 24, 2026
@github-actions

Copy link
Copy Markdown

👋 感谢您对 RT-Thread 的贡献!Thank you for your contribution to RT-Thread!

为确保代码符合 RT-Thread 的编码规范,请在你的仓库中执行以下步骤运行代码格式化工作流(如果格式化CI运行失败)。
To ensure your code complies with RT-Thread's coding style, please run the code formatting workflow by following the steps below (If the formatting of CI fails to run).


🛠 操作步骤 | Steps

  1. 前往 Actions 页面 | Go to the Actions page
    点击进入工作流 → | Click to open workflow →

  2. 点击 Run workflow | Click Run workflow

  • Use workflow from 保持默认分支(通常为 master)
    Keep the default branch (usually master) in Use workflow from
  • 在 branch 输入框填写 PR 分支 fix-printf
    Enter PR branch fix-printf in the branch field
  • 设置需排除的文件/目录(目录请以"/"结尾)
    Set files/directories to exclude (directories should end with "/")
  1. 等待工作流完成 | Wait for the workflow to complete
    格式化后的代码将作为独立提交推送至你的分支。
    The formatting changes will be pushed to your branch as a separate commit.

完成后,提交将自动更新至 fix-printf 分支,关联的 Pull Request 也会同步更新。
Once completed, commits will be pushed to the fix-printf branch automatically, and the related Pull Request will be updated.

如有问题欢迎联系我们,再次感谢您的贡献!💐
If you have any questions, feel free to reach out. Thanks again for your contribution!

@github-actions

github-actions Bot commented Sep 24, 2026 •

Copy link
Copy Markdown

📌 Code Review Assignment

🏷️ Tag: kernel

Reviewers: @GorrayLi @ReviewSun @hamburger-os @lianux-mm @wdfk-prog @xu18838022837

Changed Files (Click to expand)
  • src/klibc/rt_vsnprintf_std.c
  • src/klibc/rt_vsnprintf_tiny.c
  • src/klibc/utest/TC_rt_sprintf.c

📊 Current Review Status (Last Updated: 2026-11-24 10:33 CST)


📝 Review Instructions

  1. 维护者可以通过单击此处来刷新审查状态: 🔄 刷新状态
    Maintainers can refresh the review status by clicking here: 🔄 Refresh Status

  2. 确认审核通过后评论 LGTM/lgtm
    Comment LGTM/lgtm after confirming approval

  3. PR合并前需至少一位维护者确认
    PR must be confirmed by at least one maintainer before merging

ℹ️ 刷新CI状态操作需要具备仓库写入权限。
ℹ️ Refresh CI status operation requires repository Write permission.

@github-actions

github-actions Bot commented Sep 24, 2026 •

Copy link
Copy Markdown

MemBrowse Memory Report · Explore what changed →

hpmicro-hpm5100evk

  • XPI0: .text +16 B (+0.0%, 102,924 B / 1,048,576 B, total: 10% used)

hpmicro-hpm5301evklite

  • XPI0: .text +8 B (+0.0%, 76,660 B / 1,048,576 B, total: 7% used)

loongson-ls1cdev

  • Code: .text +8 B (+0.0%, 362,948 B)

nordic-nrf51822

  • FLASH: .text +8 B (+0.0%, 52,140 B / 262,144 B, total: 20% used)

nuvoton-m487

  • CODE: .text +88 B (+0.0%, 357,176 B / 524,288 B, total: 68% used)

raspberry-pico-rp2040

  • FLASH: .text +8 B (+0.0%, 114,384 B / 2,097,152 B, total: 5% used)

renesas-ra2l1

  • FLASH: .text +8 B (+0.0%, 98,376 B / 262,144 B, total: 38% used)

simulator

  • Code: .text -20 B (-0.0%, 2,677,102 B)

stm32l475-atk-pandora-llvm

  • ROM: .text -8 B (-0.0%, 82,880 B / 524,288 B, total: 16% used)

wch-ch32v208w-r0

  • FLASH: .text -8 B (-0.0%, 160,120 B / 491,520 B, total: 33% used)

x86

  • Code: .text -16 B (-0.0%, 199,293 B)

No memory changes detected for:


Catch memory regressions in CI with MemBrowse. Free for open source.

@luchenxu73 luchenxu73 closed this Sep 28, 2026
@luchenxu73 luchenxu73 reopened this Sep 28, 2026
@luchenxu73 luchenxu73 closed this Sep 29, 2026
@luchenxu73 luchenxu73 reopened this Sep 29, 2026
@luchenxu73
luchenxu73 force-pushed the fix-printf branch 2 times, most recently from 7420716 to 8cd8cec Compare November 10, 2026 14:38
@luchenxu73

Copy link
Copy Markdown
Contributor Author

@Rbb666 请问是否可以安排一位reviewer审查?

@BernardXiong BernardXiong left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review / 审查 — 8cd8cecd

结论:APPROVE — 修改与 C 标准语义一致,未发现阻塞问题。

逐点核对:

  • rt_vsnprintf_std.c:377-383 strnlen_s_ 先判断 maxsize 再解引用,修复了 maxsize == 0 时仍读 str[0] 的越界读;maxsize > 0 时计数与旧实现一致。
  • rt_vsnprintf_std.c:1167-1176 %.*s 的负精度清除 FLAGS_PRECISION,符合“负精度视同未指定”;%.s 仍得到 precision = 0。
  • rt_vsnprintf_std.c:1423-1440 字符串路径改用 FLAGS_PRECISION 判断,%.0s 不再输出内容;%.3s 对未 NUL 结尾的缓冲区最多读 3 字节。
  • rt_vsnprintf_tiny.c:88、165、276:is_zero 与 precision_bak 的组合使 %.0d 对 0 输出空、对 5 输出 "5";负精度在 tiny 字符串路径以 -1 作为“未指定”哨兵。
  • 测试把 %.0s、负精度和未终止缓冲区用例移入通用路径,并对 libc 后端做了合理隔离。

审查范围为本 PR 的 3 个文件;CI 为 success。

Comment thread src/klibc/rt_vsnprintf_tiny.c Outdated

/* put number in the temporary buffer */
while (i-- > 0 && (precision_bak != 0))
while (i-- > 0 && !((precision_bak == 0) && is_zero))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

精度为 0 的零值到这里才跳过,但前面已经按一位数字扣了宽度。tiny 下 %5.0d 传 0 会少输出一个空格。请在计算宽度前先确定有效数字位数,并补上带字段宽度的用例。

@luchenxu73 luchenxu73 Nov 23, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

精度为 0 的零值到这里才跳过,但前面已经按一位数字扣了宽度。tiny 下 %5.0d 传 0 会少输出一个空格。请在计算宽度前先确定有效数字位数,并补上带字段宽度的用例。

我修改了下,现在简化了很多,麻烦再看下。

在num为0的时候判断下precision: if(precison!=0)正常处理,但如果为0,就不往tmp里填0了。

然后最后输出时 while (i-- > 0 && !((precision_bak == 0) && is_zero)) 里的 !((precision_bak == 0) && is_zero) 其实就是在判断为0精度的0值场景。

由于前面没有 temp[i++] = 0,因此i还是0,tmp没有多出一个0,也不进while。并且, !((precision_bak == 0) && is_zero) 这个条件可以去掉,因为前面已经处理好precision=0,num=0的缓冲区读写了.

@BernardXiong BernardXiong left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

增量复核 0f834f0:num == 0 && precision == 0 时不再向 tmp 写入 '0',%5.0d / %-+5.0d 的宽度计算已正确,新增用例覆盖了 Rbb666 指出的少一个空格问题。

非阻塞边界:rt_vsnprintf_tiny.c:162 处虽然按精度决定是否写入数字,但 ZEROPAD 分支没有因“指定了精度”而清除 0 标志,%05.0d 传 0 仍输出 00000;C 标准要求此时忽略 0 标志,期望 5 个空格。该问题在改动前也存在,可顺带补一个用例或后续处理。

@CLAassistant

CLAassistant commented Nov 24, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@luchenxu73

Copy link
Copy Markdown
Contributor Author

问题比较多,就多提交了一个commit。请审查,谢谢。 @Rbb666 @BernardXiong

  1. 0值的宽度实现在原commit里修改了,见原评论。
  2. ZEROPAD在新commit里修复了,请审查。

@Rbb666 Rbb666 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

复核了 e14cd997。上一轮反馈的 %5.0d 和 %05.3d 用例已在 QEMU Cortex-A9 上通过。建议合并前修复下面的 tiny 八进制回归。

Comment on lines +167 to +169
if (precision != 0)
{
tmp[i++] = '0';

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P2] 避免给八进制零重复添加前导零

在 QEMU 上,tiny 用 %#.*o、精度 -1 格式化 0U 时输出 00,返回 2。负精度等同于省略精度,这里已经写入数字 0,后面的 SPECIAL 分支又补了一个 0。请在计算字段宽度前确定是否需要八进制前导零,并补一个回归用例,同时断言输出为 0、返回值为 1。

SPRINTF_CHECK("", buffer, "%.*s", 0, "Hello testing");
#if !defined(RT_KLIBC_USING_LIBC_VSNPRINTF)
SPRINTF_CHECK("Hello testing", buffer, "%.*s", -1, "Hello testing");
SPRINTF_CHECK("ABC", buffer, "%.3s", unterminated);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P3] 让测试能检出超出字符串精度的读取

这个 %.3s 用例只比较输出。实现即使多读一个字节,仍可能输出 ABC 并通过断言。建议在启用 AddressSanitizer 的主机测试中也运行这组未终止字符串用例,让越界读取直接触发测试失败。

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Kernel PR has src relate code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants