Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Nov 17, 2025

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

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

实现issue讨论中的方案1:移除对象名称长度断言,仅保留error/warning级别提示,同时使用rt_strncpy安全处理名称截断。

Implements solution 1 from issue discussion: removes RT_ASSERT on object name length, retains error/warning messages, and uses rt_strncpy for safe name truncation.

相关Issue: #10927
相关PR: #10537

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

主要变更 (Main Changes):

  • rt_object_init()rt_object_allocate(): 移除 RT_ASSERT 断言,使用 rt_strncpy 替代 rt_memcpy 进行安全的名称截断
  • rt_object_init() and rt_object_allocate(): Remove RT_ASSERT assertions, use rt_strncpy instead of rt_memcpy for safe name truncation
  • 保留 LOG_E 消息以警告开发者名称被截断
  • Retain LOG_E messages to warn developers about truncation

实现细节 (Implementation Details):

原始代码存在断言导致系统崩溃的问题:
Original code would crash the system with assertion:

// Before:
if(obj_name_len > RT_NAME_MAX - 1) {
    LOG_E("Object name %s exceeds RT_NAME_MAX=%d...", name, RT_NAME_MAX);
    RT_ASSERT(obj_name_len <= RT_NAME_MAX - 1);  // System crash
}
rt_memcpy(object->name, name, obj_name_len);    // Potential buffer overflow

更新后使用rt_strncpy安全处理截断:
Updated to use rt_strncpy for safe truncation:

// After:
if(obj_name_len > RT_NAME_MAX - 1) {
    LOG_E("Object name %s exceeds RT_NAME_MAX=%d...", name, RT_NAME_MAX);
}
rt_strncpy(object->name, name, RT_NAME_MAX - 1);  // Safe automatic truncation
object->name[RT_NAME_MAX - 1] = '\0';              // Ensure null termination

影响 (Impact):

  • ✅ 长名称对象可以成功创建(自动截断)/ Objects with long names are created successfully (with truncation)
  • ✅ 不再出现断言导致的系统崩溃 / No production system crashes from assertion failures
  • ✅ 通过LOG_E为开发者提供清晰的警告 / Clear warnings via LOG_E for developers
  • ✅ 使用rt_strncpy确保内存安全 / Memory safety ensured using rt_strncpy
  • ✅ 代码更简洁,与代码库其他部分保持一致 / Cleaner code, consistent with other parts of the codebase
  • ✅ 向后兼容 / Backwards compatible

修改文件 (Files modified): src/object.c

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


当前拉取/合并请求的状态 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
  • 已经使用formatting 等源码格式化工具确保格式符合RT-Thread代码规范 This PR complies with RT-Thread code specification
  • 如果是新增bsp, 已经添加ci检查到.github/ALL_BSP_COMPILE.json 详细请参考链接BSP自查
Original prompt

This section details on the original issue you should resolve

<issue_title>[Bug] object name争议性讨论</issue_title>
<issue_description>### RT-Thread Version

v5.2.2

Hardware Type/Architectures

all

Develop Toolchain

Other

Describe the bug

相关PR:#10537

鉴于社区开发者对此PR争议性较大,这里单独讨论该PR遗留问题,上周社区例会召开后,对大家反馈的问题进行了一个了解,并提出两种优化方案:

1.将object name断言去除,仅保留error/warning级别提示

2.将object name哈希化,这样相比较传统的字节查找方式效率会高很多,但是相对应的也会带来额外更多的内存开销,并且对内核这块改动会比较大

现召集社区开发者一起讨论一下,看看采取什么方案合适,有其他好的解决方案也可以提出

Other additional context

No response</issue_description>

<agent_instructions>请针对ISSUE中提到的方案1进行代码改进</agent_instructions>

Comments on the Issue (you are @copilot in this section)

@kurisaW @dongly @Ryan-CW-Code @kaidegit @milo-9各位大佬能否再一起来讨论下?

💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

@CLAassistant
Copy link

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@github-actions
Copy link

👋 感谢您对 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

  • 设置需排除的文件/目录(目录请以"/"结尾)
    Set files/directories to exclude (directories should end with "/")
  • 将目标分支设置为 \ Set the target branch to:copilot/discuss-object-name-issue
  • 设置PR number为 \ Set the PR number to:10943
  1. 等待工作流完成 | Wait for the workflow to complete
    格式化后的代码将自动推送至你的分支。
    The formatted code will be automatically pushed to your branch.

完成后,提交将自动更新至 copilot/discuss-object-name-issue 分支,关联的 Pull Request 也会同步更新。
Once completed, commits will be pushed to the copilot/discuss-object-name-issue 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!

Copilot AI and others added 2 commits November 17, 2025 03:48
- Remove RT_ASSERT from rt_object_init() (line 402)
- Remove RT_ASSERT from rt_object_allocate() (line 518)
- Keep LOG_E error messages to warn about name length issues
- This allows objects with long names to be created with truncation warning

Co-authored-by: Rbb666 <64397326+Rbb666@users.noreply.github.com>
- Truncate obj_name_len to RT_NAME_MAX - 1 before memcpy
- Prevents buffer overflow in both rt_object_init() and rt_object_allocate()
- Ensures safe truncation of long object names

Co-authored-by: Rbb666 <64397326+Rbb666@users.noreply.github.com>
@Rbb666 Rbb666 requested a review from kurisaW November 17, 2025 03:54
Copilot AI changed the title [WIP] Discuss optimization options for object name controversy [Kernel][Object] Remove object name assertions, add safe truncation Nov 17, 2025
Copilot AI requested a review from Rbb666 November 17, 2025 03:54
@github-actions
Copy link

github-actions bot commented Nov 17, 2025

📌 Code Review Assignment

🏷️ Tag: kernel

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

Changed Files (Click to expand)
  • src/object.c

📊 Current Review Status (Last Updated: 2025-11-19 13:48 CST)

  • GorrayLi Pending Review
  • ReviewSun Pending Review
  • hamburger-os Pending Review
  • lianux-mm Pending Review
  • wdfk-prog Pending Review
  • xu18838022837 Pending Review

📝 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 added the Kernel PR has src relate code label Nov 17, 2025
src/object.c Outdated
{
LOG_E("Object name %s exceeds RT_NAME_MAX=%d, consider increasing RT_NAME_MAX.", name, RT_NAME_MAX);
RT_ASSERT(obj_name_len <= RT_NAME_MAX - 1);
obj_name_len = RT_NAME_MAX - 1;
Copy link
Member

Choose a reason for hiding this comment

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

这一句直接去掉就好了

Copy link
Member

Choose a reason for hiding this comment

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

需要@copilot才能修改哦

Copy link
Contributor Author

Choose a reason for hiding this comment

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

已更新,使用 rt_strncpy 替代手动截断。Commit: 9a4f00c

Updated to use rt_strncpy instead of manual truncation. Commit: 9a4f00c

- Replace rt_memcpy with rt_strncpy for safer name handling
- Remove manual obj_name_len truncation
- rt_strncpy automatically handles truncation safely
- Ensure null termination at RT_NAME_MAX - 1

Co-authored-by: Rbb666 <64397326+Rbb666@users.noreply.github.com>
Copilot AI changed the title [Kernel][Object] Remove object name assertions, add safe truncation [Kernel][Object] Remove object name assertions, use rt_strncpy for safe truncation Nov 18, 2025
@Rbb666 Rbb666 marked this pull request as ready for review November 19, 2025 05:08
@Rbb666 Rbb666 merged commit 45f2246 into master Nov 19, 2025
57 of 87 checks passed
@Rbb666 Rbb666 deleted the copilot/discuss-object-name-issue branch November 19, 2025 06:12
@Rbb666
Copy link
Member

Rbb666 commented Nov 19, 2025

已测试,可以正常截断

image image image image

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.

[Bug] object name争议性讨论

5 participants