Skip to content

Commit bfe00f7

Browse files
authored
Properly initialize the char array used in type hash calculations. (#1182)
Previously, we were zero initializing it and only setting up one of its fields. But that doesn't totally properly initialize it; we really should call rcutils_char_array_init to make sure everything is initialized. Do that in the live source, as well as in the test for it. Signed-off-by: Chris Lalancette <[email protected]>
1 parent 8c4a704 commit bfe00f7

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

rcl/src/rcl/type_hash.c

+8-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
#include "rcutils/sha256.h"
2424
#include "type_description_interfaces/msg/type_description.h"
2525

26+
#include "./common.h"
27+
2628
static int yaml_write_handler(void * ext, uint8_t * buffer, size_t size)
2729
{
2830
rcutils_char_array_t * repr = (rcutils_char_array_t *)ext;
@@ -249,8 +251,13 @@ rcl_calculate_type_hash(
249251
RCL_CHECK_ARGUMENT_FOR_NULL(output_hash, RCL_RET_INVALID_ARGUMENT);
250252

251253
rcl_ret_t result = RCL_RET_OK;
254+
rcl_allocator_t allocator = rcl_get_default_allocator();
252255
rcutils_char_array_t msg_repr = rcutils_get_zero_initialized_char_array();
253-
msg_repr.allocator = rcl_get_default_allocator();
256+
rcutils_ret_t rcutils_result = rcutils_char_array_init(&msg_repr, 0, &allocator);
257+
if (rcutils_result != RCL_RET_OK) {
258+
// rcutils_char_array_init already set the error
259+
return rcl_convert_rcutils_ret_to_rcl_ret(rcutils_result);
260+
}
254261

255262
output_hash->version = 1;
256263
result = rcl_type_description_to_hashable_json(type_description, &msg_repr);

rcl/test/rcl/test_type_hash.cpp

+6-2
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,9 @@ TEST(TestTypeVersionHash, field_type_from_install) {
8484
{
8585
rcutils_sha256_ctx_t sha;
8686
rcutils_char_array_t msg_repr = rcutils_get_zero_initialized_char_array();
87-
msg_repr.allocator = rcl_get_default_allocator();
87+
rcl_allocator_t allocator = rcl_get_default_allocator();
88+
rcutils_ret_t rcutils_result = rcutils_char_array_init(&msg_repr, 0, &allocator);
89+
ASSERT_EQ(rcutils_result, RCL_RET_OK);
8890

8991
res = rcl_type_description_to_hashable_json(td_msg, &msg_repr);
9092
ASSERT_EQ(res, RCL_RET_OK);
@@ -204,7 +206,9 @@ TEST(TestTypeVersionHash, nested_real_type) {
204206
{
205207
rcutils_sha256_ctx_t sha;
206208
rcutils_char_array_t msg_repr = rcutils_get_zero_initialized_char_array();
207-
msg_repr.allocator = rcl_get_default_allocator();
209+
rcl_allocator_t allocator = rcl_get_default_allocator();
210+
rcutils_ret_t rcutils_result = rcutils_char_array_init(&msg_repr, 0, &allocator);
211+
ASSERT_EQ(rcutils_result, RCL_RET_OK);
208212

209213
res = rcl_type_description_to_hashable_json(td_msg, &msg_repr);
210214
ASSERT_EQ(res, RCL_RET_OK);

0 commit comments

Comments
 (0)