Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bad rmw_time_t to nanoseconds conversion. #555

Merged
merged 4 commits into from
May 7, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions rclpy/src/rclpy_common/src/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,29 @@ rclpy_convert_to_py_names_and_types(rcl_names_and_types_t * names_and_types)
return pynames_and_types;
}

// TODO(hidmic): revisit after discussions in
// https://github.com/ros2/rmw/issues/210 and
// https://github.com/ros2/rmw/issues/215 have
// settled.
static
int64_t
_convert_rmw_time_to_ns(const rmw_time_t * duration)
{
int64_t partial = RCUTILS_S_TO_NS(duration->sec);
if (partial < 0LL || (uint64_t)partial < duration->sec) {
return INT64_MAX;
}
int64_t total = partial + duration->nsec;
if (total < 0LL || total < partial || (uint64_t)total < duration->nsec) {
return INT64_MAX;
}
return total;
}

static
PyObject *
_convert_rmw_time_to_py_duration(const rmw_time_t * duration)
{
uint64_t total_nanoseconds = RCUTILS_S_TO_NS(duration->sec) + duration->nsec;
PyObject * pyduration_module = NULL;
PyObject * pyduration_class = NULL;
PyObject * args = NULL;
Expand All @@ -163,7 +181,8 @@ _convert_rmw_time_to_py_duration(const rmw_time_t * duration)
if (!args) {
goto cleanup;
}
kwargs = Py_BuildValue("{sK}", "nanoseconds", total_nanoseconds);
int64_t total_nanoseconds = _convert_rmw_time_to_ns(duration);
kwargs = Py_BuildValue("{sL}", "nanoseconds", total_nanoseconds);
if (!kwargs) {
goto cleanup;
}
Expand Down