Skip to content

Conversation

@Barry-Xu-2018
Copy link
Collaborator

Description

node_name, node_namespace and topic_type may be nullptr. This will result in initializing a std::string with nullptr, which will cause an exception during construction. This issue is mentioned here ros2/rosbag2#2293.

explicit TopicEndpointInfo(const rcl_topic_endpoint_info_t & info)
: node_name_(info.node_name),
node_namespace_(info.node_namespace),
topic_type_(info.topic_type),

Is this user-facing behavior change?

No

Did you use Generative AI?

No

Additional Information

N/A

Copilot AI review requested due to automatic review settings January 7, 2026 07:01
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR improves the robustness of the TopicEndpointInfo constructor by adding null pointer checks for the node_name, node_namespace, and topic_type fields from the rcl_topic_endpoint_info_t structure. Previously, passing nullptr values would result in undefined behavior when initializing std::string members, causing exceptions during construction as reported in ros2/rosbag2#2293.

  • Added ternary operator checks to safely handle nullptr values
  • Empty strings are used as fallback values when nullptr is encountered
  • Prevents exceptions during TopicEndpointInfo construction with invalid input

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 60 to 62
: node_name_(info.node_name ? info.node_name : ""),
node_namespace_(info.node_namespace ? info.node_namespace : ""),
topic_type_(info.topic_type ? info.topic_type : ""),
Copy link

Copilot AI Jan 7, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The null pointer checks added to protect against nullptr values for node_name, node_namespace, and topic_type should be covered by tests. Consider adding a test case that creates a TopicEndpointInfo with an rcl_topic_endpoint_info_t structure containing nullptr values for these fields to verify the fix handles this edge case correctly.

Copilot uses AI. Check for mistakes.
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently, TopicEndpointInfo has not been tested in any of the test code. Besides, this modification is simple. Is it necessary to add a new test file to test such a simple change?

@ahcorde
Copy link
Contributor

ahcorde commented Jan 7, 2026

Pulls: #3013
Gist: https://gist.githubusercontent.com/ahcorde/32c178de7fe164c7ee3d8dae39634e55/raw/4bbec3e9a4f5f1a0215d4c3f8fcf9cbd5df52e18/ros2.repos
BUILD args: --packages-above-and-dependencies rclcpp
TEST args: --packages-above rclcpp
ROS Distro: rolling
Job: ci_launcher
ci_launcher ran: https://ci.ros2.org/job/ci_launcher/17873

  • Linux Build Status
  • Linux-aarch64 Build Status
  • Linux-rhel Build Status
  • Windows Build Status

Copy link
Collaborator

@jmachowinski jmachowinski left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not a fan of this patch. Giving in an nullptr is clearly unintended behavior. Instead of silently ignoring this we should rather actively throw an exception.

@Barry-Xu-2018
Copy link
Collaborator Author

@jmachowinski

Thank you for your comments.
The original exception message wasn't helpful for troubleshooting. Wouldn't it be better to change it to the following?

  RCLCPP_PUBLIC
  explicit TopicEndpointInfo(const rcl_topic_endpoint_info_t & info)
  : endpoint_type_(static_cast<rclcpp::EndpointType>(info.endpoint_type)),
    qos_profile_({info.qos_profile.history, info.qos_profile.depth}, info.qos_profile),
    topic_type_hash_(info.topic_type_hash)
  {
    if (!info.node_name || !info.node_namespace || !info.topic_type) {
      throw std::invalid_argument("Constructor TopicEndpointInfo with invalid topic endpoint info");
    }
    node_name_ = info.node_name;
    node_namespace_ = info.node_namespace;
    topic_type_ = info.topic_type;
    std::copy(info.endpoint_gid, info.endpoint_gid + RMW_GID_STORAGE_SIZE, endpoint_gid_.begin());
  }

@jmachowinski
Copy link
Collaborator

@Barry-Xu-2018 your proposal looks exactly like what I had in mind.

@ahcorde
Copy link
Contributor

ahcorde commented Jan 9, 2026

Pulls: #3013
Gist: https://gist.githubusercontent.com/ahcorde/0a33636e165d1a1ee1263016c14194a8/raw/4bbec3e9a4f5f1a0215d4c3f8fcf9cbd5df52e18/ros2.repos
BUILD args: --packages-above-and-dependencies rclcpp
TEST args: --packages-above rclcpp
ROS Distro: rolling
Job: ci_launcher
ci_launcher ran: https://ci.ros2.org/job/ci_launcher/17899

  • Linux Build Status
  • Linux-aarch64 Build Status
  • Linux-rhel Build Status
  • Windows Build Status

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants