Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@ config RT_NAME_MAX
Each kernel object, such as thread, timer, semaphore etc, has a name,
the RT_NAME_MAX is the maximal size of this object name.

config RT_USING_STRICT_NAME_CHECKS
bool "Enable strict name checks for kernel objects"
default n
help
Enable duplicate-name detection and strict length checks for kernel
object names. The kernel will refuse to register a new object when its
name already exists in the same class or exceeds RT_NAME_MAX. This
option helps detect configuration mistakes during development.

config RT_USING_ARCH_DATA_TYPE
bool "Use the data types defined in ARCH_CPU"
default n
Expand Down
38 changes: 36 additions & 2 deletions src/object.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Copy link
Member

@kurisaW kurisaW Jan 7, 2026

Choose a reason for hiding this comment

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

感觉这块弄得越来越繁琐了,传入的名称已经是确定的name,这里再查找一次感觉没有意义,更多还是要多次验证_match_name的健壮性


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
Copy link

Copilot AI Jan 6, 2026

Choose a reason for hiding this comment

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

English: The duplicate name check is performed inside rt_object_init, which could have performance implications. The rt_object_find function may need to iterate through the object list, adding overhead to every object initialization. Consider documenting this performance trade-off in the Kconfig help text or code comments, especially since this is meant for development-time debugging.

中文:在 rt_object_init 中执行重复名称检查可能会影响性能。rt_object_find 函数可能需要遍历对象列表,为每次对象初始化增加开销。考虑在 Kconfig 帮助文本或代码注释中记录这种性能权衡,特别是因为这是用于开发时调试的。

Copilot uses AI. Check for mistakes.
Comment on lines +403 to +410
Copy link

Copilot AI Jan 6, 2026

Choose a reason for hiding this comment

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

English: The duplicate name check calls rt_object_find before the object is added to the object list. This is correct behavior, but there's a potential race condition in multi-threaded scenarios if two threads try to create objects with the same name simultaneously between the check and the actual registration. Consider documenting this limitation or adding additional protection if this is a concern for the use case.

中文:重复名称检查在对象添加到对象列表之前调用 rt_object_find。这是正确的行为,但在多线程场景中,如果两个线程在检查和实际注册之间同时尝试创建具有相同名称的对象,可能存在竞争条件。如果这对用例来说是一个问题,请考虑记录此限制或添加额外的保护。

Copilot uses AI. Check for mistakes.
#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';
}
Expand Down Expand Up @@ -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
Copy link

Copilot AI Jan 6, 2026

Choose a reason for hiding this comment

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

English: The strict name check code is duplicated between rt_object_init (lines 400-410) and rt_object_allocate (lines 532-543). This duplicated logic should be extracted into a helper function to improve maintainability and reduce code duplication. Consider creating a function like rt_object_check_name_validity(const char *name, enum rt_object_class_type type, rt_size_t obj_name_len) to handle both duplicate checking and length validation.

中文:严格名称检查代码在 rt_object_init(第 400-410 行)和 rt_object_allocate(第 532-543 行)之间重复。应将此重复逻辑提取到辅助函数中以提高可维护性并减少代码重复。考虑创建一个函数,如 rt_object_check_name_validity(const char *name, enum rt_object_class_type type, rt_size_t obj_name_len) 来处理重复检查和长度验证。

Copilot uses AI. Check for mistakes.

obj_name_len = rt_strlen(name);
if(obj_name_len > RT_NAME_MAX - 1)
Copy link

Copilot AI Jan 6, 2026

Choose a reason for hiding this comment

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

English: Missing space after 'if' keyword. Should be if (obj_name_len > RT_NAME_MAX - 1) to comply with RT-Thread coding style guide which requires a space between keywords and parentheses.

中文:if 关键字后缺少空格。应该是 if (obj_name_len > RT_NAME_MAX - 1) 以符合 RT-Thread 编码风格指南,该指南要求关键字和括号之间有空格。

Suggested change
if(obj_name_len > RT_NAME_MAX - 1)
if (obj_name_len > RT_NAME_MAX - 1)

Copilot uses AI. Check for mistakes.
{
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';
Expand Down Expand Up @@ -811,4 +846,3 @@ rt_err_t rt_custom_object_destroy(rt_object_t obj)
#endif

/** @} group_object_management */

79 changes: 79 additions & 0 deletions src/utest/object_tc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/

/**
Expand Down Expand Up @@ -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
Copy link

Copilot AI Jan 6, 2026

Choose a reason for hiding this comment

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

English: The strict_assert_hook function and related strict_assert_capture structure lack documentation comments. These are internal test utilities that should be documented to explain their purpose in capturing assert calls during testing. Add comments explaining that this mechanism is used to test that the strict name checks trigger assertions as expected.

中文:strict_assert_hook 函数和相关的 strict_assert_capture 结构缺少文档注释。这些是内部测试工具,应该记录以解释它们在测试期间捕获断言调用的目的。添加注释说明此机制用于测试严格名称检查按预期触发断言。

Copilot uses AI. Check for mistakes.
#endif /* defined(RT_USING_STRICT_NAME_CHECKS) && defined(RT_DEBUGING_ASSERT) */

static void test_object_name_handling(void)
{
struct rt_object static_obj;
Expand Down Expand Up @@ -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)
Copy link

Copilot AI Jan 6, 2026

Choose a reason for hiding this comment

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

English: The new test function test_object_strict_name_checks lacks a documentation comment (docstring) to explain its purpose and behavior. According to RT-Thread coding standards, functions should have clear documentation. Consider adding a comment block above this function explaining that it tests the strict name checking feature including duplicate name detection and length overflow validation.

中文:新的测试函数 test_object_strict_name_checks 缺少文档注释(docstring)来解释其目的和行为。根据 RT-Thread 编码标准,函数应该有清晰的文档。考虑在此函数上方添加注释块,说明它测试严格名称检查功能,包括重复名称检测和长度溢出验证。

Suggested change
#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 uses AI. Check for mistakes.
static void test_object_strict_name_checks(void)
{
struct rt_object base_obj;
struct rt_object duplicate_obj;
struct rt_object overflow_obj;
char duplicate_name[TEST_RT_NAME_MAX];
char overflow_name[TEST_RT_NAME_MAX + 8];
rt_size_t base_len;
void (*prev_hook)(const char *, const char *, rt_size_t) = rt_assert_hook;

uassert_true(generate_unique_name(duplicate_name, sizeof(duplicate_name), "strict", RT_Object_Class_Thread) == RT_EOK);
rt_object_init(&base_obj, RT_Object_Class_Thread, duplicate_name);

rt_assert_set_hook(strict_assert_hook);

strict_assert_capture_state.hit_count = 0;
strict_assert_capture_state.expr = RT_NULL;
strict_assert_capture_state.func = RT_NULL;
strict_assert_capture_state.line = 0;
strict_assert_capture_state.armed = RT_TRUE;
rt_object_init(&duplicate_obj, RT_Object_Class_Thread, duplicate_name);
uassert_true(strict_assert_capture_state.hit_count >= 1);
uassert_not_null(strict_assert_capture_state.expr);
uassert_str_equal(strict_assert_capture_state.expr, "duplicate == RT_NULL");
strict_assert_capture_state.armed = RT_FALSE;
rt_object_detach(&duplicate_obj);

uassert_true(generate_unique_name(overflow_name, sizeof(overflow_name), "strict", RT_Object_Class_Thread) == RT_EOK);
base_len = rt_strlen(overflow_name);
rt_memset(overflow_name + base_len, 'x', sizeof(overflow_name) - base_len - 1);
overflow_name[sizeof(overflow_name) - 1] = '\0';
uassert_true(rt_strlen(overflow_name) > TEST_RT_NAME_MAX - 1);

strict_assert_capture_state.hit_count = 0;
strict_assert_capture_state.expr = RT_NULL;
strict_assert_capture_state.func = RT_NULL;
strict_assert_capture_state.line = 0;
strict_assert_capture_state.armed = RT_TRUE;
rt_object_init(&overflow_obj, RT_Object_Class_Thread, overflow_name);
uassert_true(strict_assert_capture_state.hit_count >= 1);
uassert_not_null(strict_assert_capture_state.expr);
uassert_str_equal(strict_assert_capture_state.expr, "obj_name_len <= RT_NAME_MAX - 1");
strict_assert_capture_state.armed = RT_FALSE;
rt_object_detach(&overflow_obj);
Comment on lines +464 to +487
Copy link

Copilot AI Jan 6, 2026

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_objoverflow_obj,即使它们的初始化失败(触发了断言)。这可能导致分离未初始化的对象。考虑在调用 detach 之前检查对象是否成功初始化,或验证 rt_object_detach 是否安全处理部分初始化的对象。

Copilot uses AI. Check for mistakes.

rt_assert_set_hook(prev_hook);
rt_object_detach(&base_obj);
}
#endif /* defined(RT_USING_STRICT_NAME_CHECKS) && defined(RT_DEBUGING_ASSERT) */

#ifdef RT_USING_HEAP
static rt_err_t custom_destroy_cb(void *data)
{
Expand Down Expand Up @@ -516,6 +592,9 @@ static void test_object_suite(void)
UTEST_UNIT_RUN(test_object_error_paths);
#ifdef RT_USING_HEAP
UTEST_UNIT_RUN(test_custom_object_lifecycle);
#endif
#if defined(RT_USING_STRICT_NAME_CHECKS) && defined(RT_DEBUGING_ASSERT)
UTEST_UNIT_RUN(test_object_strict_name_checks);
#endif
UTEST_UNIT_RUN(test_object_pressure);
}
Expand Down