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

getMinValue and getMaxValue crash the program #1117

Closed
TauTheLepton opened this issue Oct 24, 2023 · 2 comments
Closed

getMinValue and getMaxValue crash the program #1117

TauTheLepton opened this issue Oct 24, 2023 · 2 comments
Assignees
Labels
bug Something isn't working

Comments

@TauTheLepton
Copy link
Contributor

Describe the bug
The function getMaxValue and getMinValue have a bug. When called with an empty vector they crash the program.
This is caused by the lines below.

return *std::max_element(values.begin(), values.end());

return *std::min_element(values.begin(), values.end());

std::min_element and std::max_element, when called with two identical iterators (which happens when we call begin() and end() on an empty vector), return this iterator. De-referencing an end() iterator causes the program to crash.
This case should be considered e.g. with an early return that returns nan when the vector is empty, or throwing an exception.

Context:
I am adding unit tests for the geometry package and wanted to add tests for the aforementioned functions, but calling them with an empty vector causes the whole program to crash.

To Reproduce
Steps to reproduce the behavior:

  1. Edit any existing source code file or add a new one
  2. Call getMinValue or getMaxValue with an empty vector as argument
  3. Compile and execute
  4. See error

Expected behavior
Some controlled handling of the empty vector situation like return of nan or throwing an exception, but not program crash.

Screenshots
If applicable, add screenshots to help explain your problem.

Desktop (please complete the following information):

  • OS: [e.g. iOS]: Ubuntu
  • Browser [e.g. chrome, safari]: Firefox
  • Version [e.g. 22]: 22
  • ROS 2 version: Humble
  • DDS: CycloneDDS
@hakuturu583
Copy link
Collaborator

Thanks, please use THROW_SIMULATION_ERROR macro when the length of the points are zero.
And, if the length of the points are one, return the first points.

@hakuturu583 hakuturu583 self-assigned this Oct 26, 2023
@hakuturu583 hakuturu583 added the bug Something isn't working label Oct 26, 2023
@hakuturu583
Copy link
Collaborator

#1139 was merged

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants