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

Replace memcpy and memcmp with assignement and compare operators #119

Closed
wants to merge 3 commits into from

Conversation

tangram67
Copy link

Replace memcpy and memcmp with assignement and compare operators in class AmsNetId.

@@ -245,15 +245,26 @@ struct AmsNetId {
/** NetId, consisting of 6 digits. */
uint8_t b[6];

#ifdef __cplusplus
Copy link

Choose a reason for hiding this comment

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

This is a public header, which should be usable in pure C projects. Without this guard a C compiler will complain.

}
}
return false;
static const AmsNetId empty {};
Copy link

Choose a reason for hiding this comment

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

Mixed usage of tab and spaces for indentation. Please replace all instances of tabs (there are other lines too) with 4 spaces.

Copy link
Member

Choose a reason for hiding this comment

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

AdsLib/Router.h Outdated
@@ -26,6 +26,8 @@
#include "AdsDef.h"

struct Router {
virtual ~Router() {};
Copy link

Choose a reason for hiding this comment

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

I don't think there is a need to explicitly add a destructor here.

Copy link
Author

Choose a reason for hiding this comment

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

Missing destructor causes warnings in Eclipse, but I will remove it.

}


bool AmsNetId::operator == (const AmsNetId& rhs) const
Copy link

Choose a reason for hiding this comment

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

Suggested change
bool AmsNetId::operator == (const AmsNetId& rhs) const
bool AmsNetId::operator==(const AmsNetId& rhs) const

... also for the other operators

@tangram67
Copy link
Author

Hi pogojotz.

I followed your suggestions and updated my pull request.

@pbruenn
Copy link
Member

pbruenn commented Oct 9, 2020

This merge request is hard to read, please squash your updates, if they are fixes only.
Besides I don't get the purpose of this change. In general you should elaborate that purpose in the commit messages.

@tangram67
Copy link
Author

tangram67 commented Oct 9, 2020

The reason for my pull request was to avoid using memcpy on class pointers, see changes in file AmsRouter.cpp (line 116):

memcpy(&pAddr->netId, &localAddr, sizeof(localAddr));

My pull request "Replace memcpy and memcmp with assignement and compare operators" is suggesting a fix by using assignment operators to copy the class/struct member "uint8_t b[6];" content.

@pbruenn
Copy link
Member

pbruenn commented Oct 9, 2020

I still don't see the reason or benefit. You are adding more than 40 lines of code to replace one call of memcpy with an assignment? AmsRouter::localAddr is a struct AmsNetId, which itself is just an alias for an uint8_t[6]. It has no vtable so sizeof(struct AmsNetId) is just the same as sizeof(uint8_t[6]).
What am I missing?

@tangram67
Copy link
Author

I believe the code will/might work, as long as the C-style struct does contain native C variable types like the given uint8_t[6] and as long as both objects are persistent. By copying the whole struct (class) content by using memcpy on the object pointers, also the C++ member function pointers are copied from one object to the other. This leads to the situation that the copied object is accessing (or lets say is working) with the member methods of the original object.
The assignment operator from my PR avoids this situation by copying the data content of the struct only. For me, this looks like a cleaner solution.

@pbruenn
Copy link
Member

pbruenn commented Oct 9, 2020

I think clean depends on the view. I my view it's a big patch (4 files changed, 70 insertions(+), 14 deletions(-)) fixing a theoretical issue. AmsNetId is part of the ADS C API so I am pretty sure it won't get any vtables.
I appreciate your effort and the virtual destructor for the Router interface might be correct. But I am not yet convinced by this merge request

@tangram67
Copy link
Author

Hmm, hard to comment...
There is a common sense that only POD data objects are allowed to be copied by memcpy. Memcopying class objects is a risky business, the result depends from case to case. I think, that relying on indifferent prerequisites is never a good design choice.
But you're free to do so and therefore I will close the request for now.

@tangram67 tangram67 closed this Oct 9, 2020
@gummif gummif mentioned this pull request Jan 20, 2021
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.

3 participants