From bfe00f71b7056bb64b27a8d5f5bacefe0564c43e Mon Sep 17 00:00:00 2001 From: Chris Lalancette Date: Tue, 17 Sep 2024 05:34:14 -0400 Subject: [PATCH] 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 --- rcl/src/rcl/type_hash.c | 9 ++++++++- rcl/test/rcl/test_type_hash.cpp | 8 ++++++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/rcl/src/rcl/type_hash.c b/rcl/src/rcl/type_hash.c index f05dd9851..8c6b217d8 100644 --- a/rcl/src/rcl/type_hash.c +++ b/rcl/src/rcl/type_hash.c @@ -23,6 +23,8 @@ #include "rcutils/sha256.h" #include "type_description_interfaces/msg/type_description.h" +#include "./common.h" + static int yaml_write_handler(void * ext, uint8_t * buffer, size_t size) { rcutils_char_array_t * repr = (rcutils_char_array_t *)ext; @@ -249,8 +251,13 @@ rcl_calculate_type_hash( RCL_CHECK_ARGUMENT_FOR_NULL(output_hash, RCL_RET_INVALID_ARGUMENT); rcl_ret_t result = RCL_RET_OK; + rcl_allocator_t allocator = rcl_get_default_allocator(); rcutils_char_array_t msg_repr = rcutils_get_zero_initialized_char_array(); - msg_repr.allocator = rcl_get_default_allocator(); + rcutils_ret_t rcutils_result = rcutils_char_array_init(&msg_repr, 0, &allocator); + if (rcutils_result != RCL_RET_OK) { + // rcutils_char_array_init already set the error + return rcl_convert_rcutils_ret_to_rcl_ret(rcutils_result); + } output_hash->version = 1; result = rcl_type_description_to_hashable_json(type_description, &msg_repr); diff --git a/rcl/test/rcl/test_type_hash.cpp b/rcl/test/rcl/test_type_hash.cpp index 58f19f76c..b7a0c970e 100644 --- a/rcl/test/rcl/test_type_hash.cpp +++ b/rcl/test/rcl/test_type_hash.cpp @@ -84,7 +84,9 @@ TEST(TestTypeVersionHash, field_type_from_install) { { rcutils_sha256_ctx_t sha; rcutils_char_array_t msg_repr = rcutils_get_zero_initialized_char_array(); - msg_repr.allocator = rcl_get_default_allocator(); + rcl_allocator_t allocator = rcl_get_default_allocator(); + rcutils_ret_t rcutils_result = rcutils_char_array_init(&msg_repr, 0, &allocator); + ASSERT_EQ(rcutils_result, RCL_RET_OK); res = rcl_type_description_to_hashable_json(td_msg, &msg_repr); ASSERT_EQ(res, RCL_RET_OK); @@ -204,7 +206,9 @@ TEST(TestTypeVersionHash, nested_real_type) { { rcutils_sha256_ctx_t sha; rcutils_char_array_t msg_repr = rcutils_get_zero_initialized_char_array(); - msg_repr.allocator = rcl_get_default_allocator(); + rcl_allocator_t allocator = rcl_get_default_allocator(); + rcutils_ret_t rcutils_result = rcutils_char_array_init(&msg_repr, 0, &allocator); + ASSERT_EQ(rcutils_result, RCL_RET_OK); res = rcl_type_description_to_hashable_json(td_msg, &msg_repr); ASSERT_EQ(res, RCL_RET_OK);