Skip to content

rclcpp::Time(int64_t nanoseconds, ...) should check for negative time #2507

@sharminramli

Description

@sharminramli

Bug report

Required Info:

  • Operating System:
    • Ubuntu 22.04
  • Installation type:
    • binaries (apt)
  • Version or commit hash:
    • ros-iron-rclcpp/jammy,now 21.0.5-1jammy.20240213.155845 amd64
  • DDS implementation:
    • default
  • Client library (if applicable):
    • rclcpp

Steps to reproduce issue

Create a dummy node with the following:

    RCLCPP_INFO_STREAM(rclcpp::get_logger(""), "Negative rclcpp::Time with int64_t ns");
    const auto negative_ns = rclcpp::Time(-1);
    RCLCPP_INFO_STREAM(rclcpp::get_logger(""), "Negative rclcpp::Time with int32_t s, uint32_t ns");
    const auto negative_s_ns = rclcpp::Time(-1, 0);

Expected behavior

Both negative_ns and negative_s_ns should throw.

Actual behavior

negative_ns does not throw, only negative_s_ns does:

[INFO] [1713275443.003549819] []: Negative rclcpp::Time with int64_t ns
[INFO] [1713275443.003577744] []: Negative rclcpp::Time with int32_t s, uint32_t ns
terminate called after throwing an instance of 'std::runtime_error'
  what():  cannot store a negative time point in rclcpp::Time
[ros2run]: Aborted

Additional information


Feature request

Feature description

Adding the <0 check

if (seconds < 0) {

to
Time::Time(int64_t nanoseconds, rcl_clock_type_t clock_type)

I would be happy to implement this change if this is indeed a missing feature.

Implementation considerations

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions