Skip to content

type_description_hash_for_topic did not correctly handle type hash conflicts for version==0 #2264

@zhihaoshang

Description

@zhihaoshang

Description

The function type_description_hash_for_topic is used to infer the type description hash of a topic based on multiple TopicEndpointInfo. When the type hashes provided by the endpoints are inconsistent, the function should return an empty string.

In the current implementation, all type hashes with version == 0 are directly ignored, without distinguishing whether they are valid but different explicit values. This can cause the function to still return a valid type hash even when there is an actual conflict.

Expected Behavior

The function should return an empty string.

Actual Behavior

The function ignored the latter, returned the former directly, and did not detect any conflict.

To Reproduce

Test Case

#include <gmock/gmock.h>
#include <string>
#include "rosbag2_transport/recorder.hpp"
using namespace ::testing;  // NOLINT

rosidl_type_hash_t all_zeros() {
  rosidl_type_hash_t type_hash;
  type_hash.version = 0;
  for (int i = 0; i < ROSIDL_TYPE_HASH_SIZE; ++i) {
    type_hash.value[i] = 0;
  }
  return type_hash;
}

rosidl_type_hash_t valid_v1_type_hash() {
  rosidl_type_hash_t type_hash;
  type_hash.version = 1;
  for (int i = 0; i < ROSIDL_TYPE_HASH_SIZE; ++i) {
    type_hash.value[i] = i;
  }
  return type_hash;
}

rclcpp::TopicEndpointInfo make_info(rosidl_type_hash_t hash) {
  rcl_topic_endpoint_info_t info;
  info.topic_type_hash = hash;
  info.topic_type = "topic_type";
  info.node_name = "node_name";
  info.node_namespace = "node_namespace";
  info.endpoint_type = RMW_ENDPOINT_INVALID;
  info.qos_profile = rmw_qos_profile_default;
  return rclcpp::TopicEndpointInfo(info);
}

TEST(TestTopicTypeHashExtended, test) {
  std::vector<rclcpp::TopicEndpointInfo> endpoints {
    make_info(rosidl_get_zero_initialized_type_hash()), 
    make_info(valid_v1_type_hash()), 
    make_info(all_zeros())
  };
  std::string result = rosbag2_transport::type_description_hash_for_topic(endpoints);
  EXPECT_EQ("", result);
}

Output

[ RUN      ] TestTopicTypeHashExtended.test
/home/shangzh/rosbag2_ws/rosbag2/rosbag2_transport/test/rosbag2_transport/test_type_description_hash.cpp:42: Failure
Expected equality of these values:
  ""
  result
    Which is: "RIHS01_000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f"

[  FAILED  ] TestTopicTypeHashExtended.test (0 ms)
[----------] 1 test from TestTopicTypeHashExtended (0 ms total)

[----------] Global test environment tear-down
[==========] 1 test from 1 test suite ran. (0 ms total)
[  PASSED  ] 0 tests.
[  FAILED  ] 1 test, listed below:
[  FAILED  ] TestTopicTypeHashExtended.test

 1 FAILED TEST

System (please complete the following information)

OS: ubuntu 24.04
ROS 2 Distro: ros 2 jazzy
Install Method: source
Version: ros 2 jazzy
build options: --mixin asan-gcc

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions