-
Notifications
You must be signed in to change notification settings - Fork 5.3k
[kennel][object]:Add strict name checks #11042
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -396,10 +396,28 @@ void rt_object_init(struct rt_object *object, | |||||
| if (name) | ||||||
| { | ||||||
| obj_name_len = rt_strlen(name); | ||||||
| if(obj_name_len > RT_NAME_MAX - 1) | ||||||
|
|
||||||
| #ifdef RT_USING_STRICT_NAME_CHECKS | ||||||
| /* Strict name checks */ | ||||||
| { | ||||||
| rt_object_t duplicate = rt_object_find(name, type); | ||||||
|
|
||||||
| if (duplicate) | ||||||
| { | ||||||
| LOG_E("Object name %s already exists in type %d.", name, type); | ||||||
| RT_ASSERT(duplicate == RT_NULL); | ||||||
| } | ||||||
| } | ||||||
|
Comment on lines
+400
to
+410
|
||||||
| #endif /* RT_USING_STRICT_NAME_CHECKS */ | ||||||
|
|
||||||
| if (obj_name_len > RT_NAME_MAX - 1) | ||||||
| { | ||||||
| LOG_E("Object name %s exceeds RT_NAME_MAX=%d, consider increasing RT_NAME_MAX.", name, RT_NAME_MAX); | ||||||
| #ifdef RT_USING_STRICT_NAME_CHECKS | ||||||
| RT_ASSERT(obj_name_len <= RT_NAME_MAX - 1); | ||||||
| #endif /* RT_USING_STRICT_NAME_CHECKS */ | ||||||
| } | ||||||
|
|
||||||
| rt_strncpy(object->name, name, RT_NAME_MAX - 1); | ||||||
| object->name[RT_NAME_MAX - 1] = '\0'; | ||||||
| } | ||||||
|
|
@@ -510,10 +528,27 @@ rt_object_t rt_object_allocate(enum rt_object_class_type type, const char *name) | |||||
| #if RT_NAME_MAX > 0 | ||||||
| if (name) | ||||||
| { | ||||||
|
|
||||||
| #ifdef RT_USING_STRICT_NAME_CHECKS | ||||||
| /* Strict name checks */ | ||||||
| { | ||||||
| rt_object_t duplicate = rt_object_find(name, type); | ||||||
|
|
||||||
| if (duplicate) | ||||||
| { | ||||||
| LOG_E("Object name %s already exists in type %d.", name, type); | ||||||
| RT_ASSERT(duplicate == RT_NULL); | ||||||
| } | ||||||
| } | ||||||
| #endif /* RT_USING_STRICT_NAME_CHECKS */ | ||||||
|
Comment on lines
+532
to
+543
|
||||||
|
|
||||||
| obj_name_len = rt_strlen(name); | ||||||
| if(obj_name_len > RT_NAME_MAX - 1) | ||||||
|
||||||
| if(obj_name_len > RT_NAME_MAX - 1) | |
| if (obj_name_len > RT_NAME_MAX - 1) |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -8,6 +8,7 @@ | |||||||||||||||||||||||||||
| * 2025-07-18 kurisaW First commit | ||||||||||||||||||||||||||||
| * 2025-11-13 CYFS Add standardized documentation block for object_tc | ||||||||||||||||||||||||||||
| * 2025-11-19 Rbb666 Refactor tests, add stress and error-path coverage | ||||||||||||||||||||||||||||
| * 2025-12-12 CYFS add strict name-check tests | ||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||
|
|
@@ -140,6 +141,30 @@ static rt_err_t generate_unique_name(char *buf, | |||||||||||||||||||||||||||
| return -RT_ENOMEM; | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| #if defined(RT_USING_STRICT_NAME_CHECKS) && defined(RT_DEBUGING_ASSERT) | ||||||||||||||||||||||||||||
| struct strict_assert_capture | ||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||
| rt_bool_t armed; | ||||||||||||||||||||||||||||
| rt_uint16_t hit_count; | ||||||||||||||||||||||||||||
| const char *expr; | ||||||||||||||||||||||||||||
| const char *func; | ||||||||||||||||||||||||||||
| rt_size_t line; | ||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| static struct strict_assert_capture strict_assert_capture_state; | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| static void strict_assert_hook(const char *ex, const char *func, rt_size_t line) | ||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||
| if (!strict_assert_capture_state.armed) | ||||||||||||||||||||||||||||
| return; | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| strict_assert_capture_state.hit_count++; | ||||||||||||||||||||||||||||
| strict_assert_capture_state.expr = ex; | ||||||||||||||||||||||||||||
| strict_assert_capture_state.func = func; | ||||||||||||||||||||||||||||
| strict_assert_capture_state.line = line; | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
Comment on lines
+145
to
+165
|
||||||||||||||||||||||||||||
| #endif /* defined(RT_USING_STRICT_NAME_CHECKS) && defined(RT_DEBUGING_ASSERT) */ | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| static void test_object_name_handling(void) | ||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||
| struct rt_object static_obj; | ||||||||||||||||||||||||||||
|
|
@@ -415,6 +440,57 @@ static void test_object_error_paths(void) | |||||||||||||||||||||||||||
| rt_object_detach(&obj); | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| #if defined(RT_USING_STRICT_NAME_CHECKS) && defined(RT_DEBUGING_ASSERT) | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
| #if defined(RT_USING_STRICT_NAME_CHECKS) && defined(RT_DEBUGING_ASSERT) | |
| #if defined(RT_USING_STRICT_NAME_CHECKS) && defined(RT_DEBUGING_ASSERT) | |
| /** | |
| * Test strict object name checking behavior. | |
| * | |
| * This test exercises the RT_USING_STRICT_NAME_CHECKS logic under | |
| * RT_DEBUGING_ASSERT by: | |
| * - Initializing a base object with a unique name. | |
| * - Attempting to initialize a second object with the same name to | |
| * verify duplicate name detection via the assertion hook. | |
| * - Constructing a name longer than TEST_RT_NAME_MAX to validate | |
| * name-length overflow handling. | |
| */ |
Copilot
AI
Jan 6, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
English: The test function properly validates the strict name check feature by capturing assert calls. However, it detaches the duplicate_obj and overflow_obj even though their initialization failed (assert was triggered). This could lead to detaching uninitialized objects. Consider checking if the object was successfully initialized before calling detach, or verify that rt_object_detach handles partially initialized objects safely.
中文:测试函数通过捕获断言调用正确验证了严格名称检查功能。但是,它会分离 duplicate_obj 和 overflow_obj,即使它们的初始化失败(触发了断言)。这可能导致分离未初始化的对象。考虑在调用 detach 之前检查对象是否成功初始化,或验证 rt_object_detach 是否安全处理部分初始化的对象。
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
感觉这块弄得越来越繁琐了,传入的名称已经是确定的name,这里再查找一次感觉没有意义,更多还是要多次验证_match_name的健壮性