Skip to content

fix(autoware_trajectory): make the get_index function safe #568

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

yhisaki
Copy link
Contributor

@yhisaki yhisaki commented Jul 10, 2025

Description

The get_index function now returns values between 0 and size -1.

Related links

Parent Issue:

  • Link

How was this PR tested?

Notes for reviewers

None.

Interface changes

None.

Effects on system behavior

None.

@yhisaki yhisaki requested review from kosuke55, soblin, takayuki5168 and a team as code owners July 10, 2025 06:40
Copy link

github-actions bot commented Jul 10, 2025

Thank you for contributing to the Autoware project!

🚧 If your pull request is in progress, switch it to draft mode.

Please ensure:

@yhisaki yhisaki added the run:build-and-test-differential Mark to enable build-and-test-differential workflow. (used-by-ci) label Jul 10, 2025
return std::distance(bases_.begin(), std::lower_bound(bases_.begin(), bases_.end(), s, comp)) -
1;
const int32_t idx =
std::distance(bases_.begin(), std::lower_bound(bases_.begin(), bases_.end(), s, comp)) - 1;
Copy link
Contributor

@soblin soblin Jul 10, 2025

Choose a reason for hiding this comment

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

This still gives 0 - 1 = 18446744073709551615 as int32_t, which causes narrow-conversion from uint64_t to int32_t.

static_cast<int64_t or int32_t>(std::distance(bases_.begin(), std::lower_bound(bases_.begin(), bases_.end(), s, comp))) still causes narrowing-conversion, so there is no choice but to check if std::distance(bases_.begin(), std::lower_bound(bases_.begin(), bases_.end(), s, comp)) == 0 and return 0, otherwise subtract 1.

Copy link
Contributor

@sasakisasaki sasakisasaki left a comment

Choose a reason for hiding this comment

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

Perhaps we also need to fix this method too? 👀

Eigen::Index SplineInterpolation::get_index(const double & key) const
{
const auto it = std::lower_bound(base_keys_.begin(), base_keys_.end(), key);
return std::clamp(
static_cast<int>(std::distance(base_keys_.begin(), it)) - 1, 0,
static_cast<int>(base_keys_.size()) - 2);
}

yhisaki and others added 2 commits July 14, 2025 20:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
run:build-and-test-differential Mark to enable build-and-test-differential workflow. (used-by-ci)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants