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

CanBusMotionControl: corrected compilation warnings + potential bugs #761

Merged
merged 2 commits into from
Aug 3, 2021
Merged

CanBusMotionControl: corrected compilation warnings + potential bugs #761

merged 2 commits into from
Aug 3, 2021

Conversation

marcoaccame
Copy link
Contributor

Purpose of the PR

This PR addresses what in issue #754 and fixes:

  • compilation warnings
  • potential bugs
    • CanBusMotionControl::setControlModeRaw() and similar methods did not return false when they receive an invalid control mode.
    • in some c-string manipulation there was the danger to write out of memory in case the string is very long.

Details of the changes

There are two changes:

  • fix of the behaviour / use of function CanBusMotionControl::from_modevocab_to_modeint() used inside ::setControlModeRaw() to transform a yarp::conf::vocab32_t into a icubCanProto_controlmode_t.
  • fix of the creation of a string to be printed with yDebug() inside method void CanBusMotionControl::run()

In the first case, the return value of from_modevocab_to_modeint() was compared vs VOCAB_CM_UNKNOWN which was never returned because the function truncated its value (the warning was just saying that).
Solved by returning icubCanProto_controlmode_unknownError instead.

In the second case, some sprintf() did not have check vs writing out of memory in the case of particularly long strings. Solved by using a std::string which gets the memory it needs.

Tests

The changes are trivial and for this reason I did not test them on the robot.

potential bugs:
- setControlModeRaw() did not return false when it received an invalid control mode
- in some c-string manipulation there was teh danger to write out of memory
@pattacini pattacini linked an issue Jul 29, 2021 that may be closed by this pull request
Copy link
Member

@pattacini pattacini left a comment

Choose a reason for hiding this comment

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

Thanks a lot @marcoaccame 👍🏻

Copy link
Member

@traversaro traversaro left a comment

Choose a reason for hiding this comment

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

Ok for me. One thing that is worth noting is that probably that code did not use any std::string or similar to avoid the use of dynamic memory allocation (that is a quite non-determistic operation and expensive, from the real time point of view) in a frequently invoked loop. If however you do not think this is a problem in this case, I am ok with the PR.

@marcoaccame
Copy link
Contributor Author

Ok for me. One thing that is worth noting is that probably that code did not use any std::string or similar to avoid the use of dynamic memory allocation. ....

Good point @traversaro.

The change to avoid the use of heap would be trivial: move the string out of the scope of the loop and reserve for it a reasonable chunk of memory. Any other operation on the string would not involve heap operations.

std::string mystring;
mystring.reserve(512);

@pattacini
Copy link
Member

The change to avoid the use of heap would be trivial: move the string out of the scope of the loop and reserve for it a reasonable chunk of memory. Any other operation on the string would not involve heap operations.

Agreed 👍🏻
@marcoaccame could you proceed with the change?

@marcoaccame
Copy link
Contributor Author

@marcoaccame could you proceed with the change?

Hi @pattacini, I have just completed the change.

@pattacini
Copy link
Member

Just waiting the CI and then merging.

@pattacini pattacini merged commit 426c2a5 into robotology:devel Aug 3, 2021
@pattacini
Copy link
Member

This fix landed in devel but we're going to churn out 2021.08 soon.

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

Successfully merging this pull request may close these issues.

CanBusMotionControl warnings
3 participants