@@ -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