[Kernel][Object] Remove object name assertions, use rt_strncpy for safe truncation#10943
[Kernel][Object] Remove object name assertions, use rt_strncpy for safe truncation#10943
Conversation
|
|
|
👋 感谢您对 RT-Thread 的贡献!Thank you for your contribution to RT-Thread! 为确保代码符合 RT-Thread 的编码规范,请在你的仓库中执行以下步骤运行代码格式化工作流(如果格式化CI运行失败)。 🛠 操作步骤 | Steps
完成后,提交将自动更新至 如有问题欢迎联系我们,再次感谢您的贡献!💐 |
- 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>
📌 Code Review Assignment🏷️ Tag: kernelReviewers: GorrayLi ReviewSun hamburger-os lianux-mm wdfk-prog xu18838022837 Changed Files (Click to expand)
📊 Current Review Status (Last Updated: 2025-11-19 13:48 CST)
📝 Review Instructions
|
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; |
- 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>




拉取/合并请求描述:(PR description)
为什么提交这份PR (why to submit this PR)
实现issue讨论中的方案1:移除对象名称长度断言,仅保留error/warning级别提示,同时使用rt_strncpy安全处理名称截断。
Implements solution 1 from issue discussion: removes
RT_ASSERTon object name length, retains error/warning messages, and usesrt_strncpyfor safe name truncation.相关Issue: #10927
相关PR: #10537
你的解决方案是什么 (what is your solution)
主要变更 (Main Changes):
RT_ASSERT断言,使用rt_strncpy替代rt_memcpy进行安全的名称截断RT_ASSERTassertions, usert_strncpyinstead ofrt_memcpyfor safe name truncationLOG_E消息以警告开发者名称被截断LOG_Emessages to warn developers about truncation实现细节 (Implementation Details):
原始代码存在断言导致系统崩溃的问题:
Original code would crash the system with assertion:
更新后使用rt_strncpy安全处理截断:
Updated to use rt_strncpy for safe truncation:
影响 (Impact):
修改文件 (Files modified):
src/object.c请提供验证的bsp和config (provide the config and bsp)
BSP: All BSPs (kernel-level change)
.config: N/A (affects all configurations with RT_NAME_MAX > 0)
action: https://github.com/RT-Thread/rt-thread/actions
当前拉取/合并请求的状态 Intent for your PR
必须选择一项 Choose one (Mandatory):
代码质量 Code Quality:
我在这个拉取/合并请求中已经考虑了 As part of this pull request, I've considered the following:
#if 0代码,不包含已经被注释了的代码 All redundant code is removed and cleaned upOriginal prompt
💡 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.