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

Fix C++ One Definition Rule violations and misc. UB #25394

Closed
wants to merge 6 commits into from

Conversation

tpwrules
Copy link
Contributor

@tpwrules tpwrules commented Oct 29, 2023

Fix various violations of the C++ One Definition Rule discovered during testing with LTO.

Fix a couple of easy UB cases noticed when doing the above.

Not necessarily an exhaustive list, but this does fix the complete list of violations diagnosed by GCC.

No code size or functional change.

@tpwrules
Copy link
Contributor Author

tpwrules commented Nov 8, 2023

It should also be possible to use C++ friendship if desired instead of public for the sample_regs struct.

@Ryanf55
Copy link
Collaborator

Ryanf55 commented Nov 14, 2023

GCC10 comes with the vagrant build. The build is still not passing even with these fixes because LTO isn't enabled. LTO can save size and increase performance by 10-20%.

libraries/AP_HAL_ChibiOS/CANFDIface.cpp Outdated Show resolved Hide resolved
@tridge tridge removed the DevCallEU label Nov 15, 2023
Two structs with the same name must have exactly the same definition, no
matter where they occur in the program.

Move each sample register struct definition into the associated class
definition so they are in a different namespace and no longer
identicially named, thus fixing this issue.
Allows update of CAN interface during driver initialization without
UB-inducing casting away of const.
Switch to const extern hal reference, then use mutability of can array
to store the CAN driver reference during driver init and eliminate UB.
Eliminates UB, now possible with mutable can array in AP_HAL.
Eliminates UB, now possible with mutable can array in AP_HAL.
Avoids UB-inducing assumption that UART drivers are consecutive in the
serial() function.
@tpwrules tpwrules changed the title Fix C++ One Definition Rule violations Fix C++ One Definition Rule violations and misc. UB Nov 15, 2023
@tpwrules
Copy link
Contributor Author

Feedback is to split this up.

mutable keyword is not liked, @peterbarker suggests a global mutable extern for this purpose which seems eminently reasonable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants