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

Change SelectedEntities to return a const ref #571

Merged
merged 2 commits into from
Jan 21, 2021

Conversation

brawner
Copy link
Contributor

@brawner brawner commented Jan 19, 2021

Instead of returning a copy of the member variable, this PR changes this getter to return a const ref. In Scene3D.cc, a couple of places call the method without retaining ownership of the copied vector. This was specifically an issue here: https://github.com/ignitionrobotics/ign-gazebo/blob/bccea9087dd4355fc6d273d41a191d5c16563175/src/gui/plugins/scene3d/Scene3D.cc#L1846

In this line, because the vector is a copy, the back() ref operator returns a reference to an immediately deleted object, which will cause a memory read error and a subsequent segfault (in the gazebo gui).

I don't think this should break API too bad. But if someone was actually mutating the copy that was returned, then they could potentially be affected by this. I don't think it can be backported, since it breaks API and ABI.

Signed-off-by: Stephen Brawner brawner@gmail.com

Signed-off-by: Stephen Brawner <brawner@gmail.com>
@brawner brawner requested a review from azeey January 19, 2021 23:32
@github-actions github-actions bot added the 🏢 edifice Ignition Edifice label Jan 19, 2021
@codecov
Copy link

codecov bot commented Jan 20, 2021

Codecov Report

Merging #571 (3897667) into main (fc913c8) will increase coverage by 0.04%.
The diff coverage is n/a.

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #571      +/-   ##
==========================================
+ Coverage   77.56%   77.60%   +0.04%     
==========================================
  Files         211      211              
  Lines       11579    11582       +3     
==========================================
+ Hits         8981     8988       +7     
+ Misses       2598     2594       -4     
Impacted Files Coverage Δ
src/systems/user_commands/UserCommands.cc 77.07% <0.00%> (-0.80%) ⬇️
src/systems/log/LogRecord.cc 82.35% <0.00%> (-0.08%) ⬇️
src/Conversions.cc 82.19% <0.00%> (ø)
src/systems/imu/Imu.hh 100.00% <0.00%> (ø)
src/systems/contact/Contact.hh 100.00% <0.00%> (ø)
src/systems/physics/Physics.hh 100.00% <0.00%> (ø)
src/systems/thermal/Thermal.hh 100.00% <0.00%> (ø)
src/systems/altimeter/Altimeter.hh 100.00% <0.00%> (ø)
src/systems/breadcrumbs/Breadcrumbs.hh 100.00% <0.00%> (ø)
src/systems/air_pressure/AirPressure.hh 100.00% <0.00%> (ø)
... and 16 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update fc913c8...3897667. Read the comment docs.

Copy link
Contributor

@chapulina chapulina left a comment

Choose a reason for hiding this comment

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

LGTM. Could you add a note to the migration guide before merging?

Signed-off-by: Stephen Brawner <brawner@gmail.com>
@@ -138,7 +138,7 @@ inline namespace IGNITION_GAZEBO_VERSION_NAMESPACE {

/// \brief Get the entities currently selected, in order of selection.
/// \return Vector of currently selected entities
public: std::vector<Entity> SelectedEntities() const;
public: const std::vector<Entity> &SelectedEntities() const;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@chapulina Does ignition style suggest that the & be next to the type or the method here, do you know? I just switched it to this after seeing some other getters that return references above.

Copy link
Contributor

@chapulina chapulina Jan 21, 2021

Choose a reason for hiding this comment

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

It goes next to the variable, see the style guide. This is something carried over from Gazebo classic and we have no linters to catch it yet.

@@ -138,7 +138,7 @@ inline namespace IGNITION_GAZEBO_VERSION_NAMESPACE {

/// \brief Get the entities currently selected, in order of selection.
/// \return Vector of currently selected entities
public: std::vector<Entity> SelectedEntities() const;
public: const std::vector<Entity> &SelectedEntities() const;
Copy link
Contributor

@chapulina chapulina Jan 21, 2021

Choose a reason for hiding this comment

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

It goes next to the variable, see the style guide. This is something carried over from Gazebo classic and we have no linters to catch it yet.

@chapulina chapulina merged commit 6f62b6f into main Jan 21, 2021
@chapulina chapulina deleted the brawner/selected-entities-const-ref branch January 21, 2021 02:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🏢 edifice Ignition Edifice
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants