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

matrix: fix slice to slice assignment to do deep copy #22574

Merged
merged 2 commits into from
Dec 21, 2023

Conversation

MaEtUgR
Copy link
Member

@MaEtUgR MaEtUgR commented Dec 20, 2023

Solved Problem

@bresch warned me about this bug report yesterday: #22565
We started debugging and were puzzled why the assignment of a.xy() = b.xy() with two Vector3fs did not call Slice::operator=() which implements a deep copy of the submatrix in b, in this case the first two elements.

@jwidauer kindly spent some time analyzing the case. He found out by separating the steps and explicitly deleting constructors and the detailed output of clang that the autogenerated implicit default copy constructor of Slice<Type, P, Q, M, N> is the preferred template match over Slice<Type, P, Q, M, N> &operator=(const Slice<Type, P, Q, MM, NN>) when the original unsliced size of matrix b (MM and NN) exactly matches the size of matrix a (M and N). As a result the pointer in a.x() was replaced with a thin copy of b.xy() and none of the elements were copied to the original matrix a.

Fixes #22565

Solution

Explicitly define Slice<Type, P, Q, M, N> &operator=(const Slice<Type, P, Q, M, N>) with exactly matching original sizes M and N and make it call the original deep copy implementation. The compiler then gives a warning if we don't also explicitly declare the default copy constructor without the assignment operator to do what it originally did.

Changelog Entry

  • Evaluate the end-user effect and update the changelog entry
Bugfix: Position lock fix for non-default MPC_POS_MODE 0 (Direct velocity stick mapping)

Test coverage

The added test now does what it should.

  • I'm evaluating what the effect is and verifying that things are working correctly compared to before. E.g.

Position lock in Position mode
Before:

After:

Fake position fusion:
I have no idea how to verify this. @bresch could you have a quick look at the change indicator?

Context

In
https://github.com/PX4/PX4-Autopilot/pull/20440/files#diff-6a93912921c62df6bced857c3dea68c3c275d25faa3f6c78866eafedc4e5612fR113
and
https://github.com/PX4/PX4-Autopilot/pull/20021/files#diff-61244855f5e70ce3cf45228c9915530a01583ea7906fb25b93547c8ff839ad74R513
we started using assignments of a slice to a slice a.xy() = b.xy() with two Vector3fs. We expected this to assign the elements of slice b to the elements of slice a just like how it works when doing a.xy() = Vector2f(). Turns out the later case is unit tested, the first one not.

To fix usage of a.xy() = b.xy() which should copy
the first two elements over into a and not act on a copy of a.
@MaEtUgR MaEtUgR force-pushed the fix-matrix-slice-to-slice-assignment branch from 16bddd1 to a232b93 Compare December 20, 2023 13:44
@MaEtUgR MaEtUgR marked this pull request as ready for review December 20, 2023 15:45
Copy link
Member

@bresch bresch left a comment

Choose a reason for hiding this comment

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

Thanks for the fix!

@MaEtUgR MaEtUgR merged commit 74549e2 into main Dec 21, 2023
3 checks passed
@MaEtUgR MaEtUgR deleted the fix-matrix-slice-to-slice-assignment branch December 21, 2023 10:42
@DronecodeBot
Copy link

This pull request has been mentioned on Discussion Forum for PX4, Pixhawk, QGroundControl, MAVSDK, MAVLink. There might be relevant details there:

https://discuss.px4.io/t/px4-sync-q-a-sep-11-2024/40625/1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: ✅ Done
Development

Successfully merging this pull request may close these issues.

Vector asigment with vec.xy() = vec.xy() does not work [Bug]
3 participants