Skip to content

Commit 24e2c52

Browse files
committed
object: add strict name checks guard
1 parent d6b173b commit 24e2c52

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

src/Kconfig

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,15 @@ config RT_NAME_MAX
1010
Each kernel object, such as thread, timer, semaphore etc, has a name,
1111
the RT_NAME_MAX is the maximal size of this object name.
1212

13+
config RT_USING_STRICT_NAME_CHECKS
14+
bool "Enable strict name checks for kernel objects"
15+
default n
16+
help
17+
Enable duplicate-name detection and strict length checks for kernel
18+
object names. The kernel will refuse to register a new object when its
19+
name already exists in the same class or exceeds RT_NAME_MAX. This
20+
option helps detect configuration mistakes during development.
21+
1322
config RT_USING_ARCH_DATA_TYPE
1423
bool "Use the data types defined in ARCH_CPU"
1524
default n

src/object.c

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -396,10 +396,28 @@ void rt_object_init(struct rt_object *object,
396396
if (name)
397397
{
398398
obj_name_len = rt_strlen(name);
399-
if(obj_name_len > RT_NAME_MAX - 1)
399+
400+
#ifdef RT_USING_STRICT_NAME_CHECKS
401+
/* Strict name checks */
402+
{
403+
rt_object_t duplicate = rt_object_find(name, type);
404+
405+
if (duplicate)
406+
{
407+
LOG_E("Object name %s already exists in type %d.", name, type);
408+
RT_ASSERT(duplicate == RT_NULL);
409+
}
410+
}
411+
#endif /* RT_USING_STRICT_NAME_CHECKS */
412+
413+
if (obj_name_len > RT_NAME_MAX - 1)
400414
{
401415
LOG_E("Object name %s exceeds RT_NAME_MAX=%d, consider increasing RT_NAME_MAX.", name, RT_NAME_MAX);
416+
#ifdef RT_USING_STRICT_NAME_CHECKS
417+
RT_ASSERT(obj_name_len <= RT_NAME_MAX - 1);
418+
#endif /* RT_USING_STRICT_NAME_CHECKS */
402419
}
420+
403421
rt_strncpy(object->name, name, RT_NAME_MAX - 1);
404422
object->name[RT_NAME_MAX - 1] = '\0';
405423
}
@@ -510,10 +528,27 @@ rt_object_t rt_object_allocate(enum rt_object_class_type type, const char *name)
510528
#if RT_NAME_MAX > 0
511529
if (name)
512530
{
531+
532+
#ifdef RT_USING_STRICT_NAME_CHECKS
533+
/* Strict name checks */
534+
{
535+
rt_object_t duplicate = rt_object_find(name, type);
536+
537+
if (duplicate)
538+
{
539+
LOG_E("Object name %s already exists in type %d.", name, type);
540+
RT_ASSERT(duplicate == RT_NULL);
541+
}
542+
}
543+
#endif /* RT_USING_STRICT_NAME_CHECKS */
544+
513545
obj_name_len = rt_strlen(name);
514546
if(obj_name_len > RT_NAME_MAX - 1)
515547
{
516548
LOG_E("Object name %s exceeds RT_NAME_MAX=%d, consider increasing RT_NAME_MAX.", name, RT_NAME_MAX);
549+
#ifdef RT_USING_STRICT_NAME_CHECKS
550+
RT_ASSERT(obj_name_len <= RT_NAME_MAX - 1);
551+
#endif /* RT_USING_STRICT_NAME_CHECKS */
517552
}
518553
rt_strncpy(object->name, name, RT_NAME_MAX - 1);
519554
object->name[RT_NAME_MAX - 1] = '\0';
@@ -811,4 +846,3 @@ rt_err_t rt_custom_object_destroy(rt_object_t obj)
811846
#endif
812847

813848
/** @} group_object_management */
814-

0 commit comments

Comments
 (0)