Skip to content

Commit ee61ff0

Browse files
Let get_ordered_interfaces throw if input vector size does not fit (#2528)
1 parent 8b98fbf commit ee61ff0

File tree

4 files changed

+22
-2
lines changed

4 files changed

+22
-2
lines changed

controller_interface/include/controller_interface/helpers.hpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
#include <algorithm>
1919
#include <functional>
20+
#include <memory>
21+
#include <stdexcept>
2022
#include <string>
2123
#include <vector>
2224

@@ -37,15 +39,24 @@ namespace controller_interface
3739
* If joint names are used for ordering, \p interface_type specifies valid interface.
3840
* If full interface names are used for ordering, \p interface_type should be empty string ("").
3941
* \param[in] interface_type used for ordering interfaces with respect to joint names.
40-
* \param[out] ordered_interfaces vector with ordered interfaces.
42+
* \param[out] ordered_interfaces vector with ordered interfaces. Has to have the same capacity as
43+
* \p ordered_names size. Throws otherwise.
44+
* \throws std::range_error if the capacity of ordered_interfaces is less than the size of
45+
* ordered_names.
4146
* \return true if all interfaces or joints in \p ordered_names are found, otherwise false.
4247
*/
4348
template <typename T>
4449
bool get_ordered_interfaces(
4550
std::vector<T> & unordered_interfaces, const std::vector<std::string> & ordered_names,
4651
const std::string & interface_type, std::vector<std::reference_wrapper<T>> & ordered_interfaces)
4752
{
48-
ordered_interfaces.reserve(ordered_names.size());
53+
if (ordered_interfaces.capacity() < ordered_names.size())
54+
{
55+
throw std::range_error(
56+
"Capacity of ordered_interfaces (" + std::to_string(ordered_interfaces.capacity()) +
57+
") has to be equal or higher as size of ordered_names (" +
58+
std::to_string(ordered_names.size()) + ") for realtime reasons.");
59+
}
4960
for (const auto & name : ordered_names)
5061
{
5162
for (auto & interface : unordered_interfaces)

controller_interface/include/semantic_components/gps_sensor.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ class GPSSensor : public SemanticComponentInterface<sensor_msgs::msg::NavSatFix>
5252
interface_names_.emplace_back(name + "/" + "latitude_covariance");
5353
interface_names_.emplace_back(name + "/" + "longitude_covariance");
5454
interface_names_.emplace_back(name + "/" + "altitude_covariance");
55+
state_interfaces_.reserve(state_interfaces_.capacity() + 3);
5556
}
5657
}
5758

controller_interface/include/semantic_components/semantic_component_interface.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,10 @@ class SemanticComponentInterface
104104
*/
105105
bool get_values_as_message(MessageReturnType & /* message */) { return false; }
106106

107+
// delete copy constructor, because
108+
// copy will change capacity of member variables
109+
SemanticComponentInterface(const SemanticComponentInterface &) = delete;
110+
107111
protected:
108112
std::string name_;
109113
std::vector<std::string> interface_names_;

doc/migration.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ Migration Guides: Jazzy to Kilted
55

66
This list summarizes important changes between Jazzy (previous) and Kilted (current) releases, where changes to user code might be necessary.
77

8+
controller_interface
9+
********************
10+
* ``get_ordered_interfaces`` now throws if the size of the output vector does not match the size of the input names vector (`#2528 <https://github.com/ros-controls/ros2_control/pull/2528>`__).
11+
812
hardware_interface
913
******************
1014

0 commit comments

Comments
 (0)