@@ -35,6 +35,18 @@ extern "C"
3535
3636static rclcpp_lifecycle__msg__Transition msg ;
3737
38+ bool concatenate (const char * * prefix , const char * * suffix , char * * result )
39+ {
40+ size_t prefix_size = strlen (* prefix );
41+ size_t suffix_size = strlen (* suffix );
42+ if ((prefix_size + suffix_size ) >= 255 )
43+ return false;
44+ * result = malloc ((prefix_size + suffix_size )* sizeof (char ));
45+ memcpy (* result , * prefix , prefix_size );
46+ memcpy (* result + prefix_size , * suffix , suffix_size );
47+ return true;
48+ }
49+
3850// get zero initialized state machine here
3951rcl_state_machine_t
4052rcl_get_zero_initialized_state_machine ()
@@ -72,16 +84,14 @@ rcl_state_machine_init(rcl_state_machine_t* state_machine, const char* node_name
7284 // Build topic, topic suffix hardcoded for now
7385 // and limited in length of 255
7486 const char * topic_suffix = "lifecycle_manager__state_changes__" ;
75- if (strlen (node_name )+ strlen (topic_suffix ) >= 255 )
76- {
87+ char * topic_name ;
88+ if (concatenate (& topic_suffix , & node_name , & topic_name ) != true)
89+ {
7790 fprintf (stderr , "%s:%u, Topic name exceeds maximum size of 255\n" ,
7891 __FILE__ , __LINE__ );
7992 state_machine = NULL ;
8093 return RCL_RET_ERROR ;
81- }
82- char topic_name [255 ];
83- strcpy (topic_name , topic_suffix );
84- strcat (topic_name , node_name );
94+ }
8595
8696 const rosidl_message_type_support_t * ts = ROSIDL_GET_TYPE_SUPPORT (
8797 rclcpp_lifecycle , msg , Transition );
@@ -92,24 +102,24 @@ rcl_state_machine_init(rcl_state_machine_t* state_machine, const char* node_name
92102 if (ret != RCL_RET_OK )
93103 {
94104 state_machine = NULL ;
105+ free (topic_name );
95106 return ret ;
96107 }
108+ free (topic_name );
97109 }
98110
99111 { // initialize service
100112 // Build topic, topic suffix hardcoded for now
101113 // and limited in length of 255
102114 const char * topic_suffix = "lifecycle_manager__get_current_state__" ;
103- if (strlen (node_name )+ strlen (topic_suffix ) >= 255 )
104- {
105- fprintf (stderr , "%s:%u, Service name exceeds maximum size of 255\n" ,
115+ char * topic_name ;
116+ if (concatenate (& topic_suffix , & node_name , & topic_name ) != true)
117+ {
118+ fprintf (stderr , "%s:%u, Topic name exceeds maximum size of 255\n" ,
106119 __FILE__ , __LINE__ );
107120 state_machine = NULL ;
108121 return RCL_RET_ERROR ;
109122 }
110- char topic_name [255 ];
111- strcpy (topic_name , topic_suffix );
112- strcat (topic_name , node_name );
113123
114124 const rosidl_service_type_support_t * ts = ROSIDL_GET_TYPE_SUPPORT (
115125 rclcpp_lifecycle , srv , GetState );
@@ -119,9 +129,12 @@ rcl_state_machine_init(rcl_state_machine_t* state_machine, const char* node_name
119129 if (ret != RCL_RET_OK )
120130 {
121131 state_machine = NULL ;
132+ free (topic_name );
122133 return ret ;
123134 }
135+ free (topic_name );
124136 }
137+
125138 if (default_states )
126139 {
127140 rcl_init_default_state_machine (state_machine );
0 commit comments