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

Cast to void #121

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Cast to void #121

wants to merge 1 commit into from

Conversation

nenad
Copy link

@nenad nenad commented Aug 5, 2019

Prevent warnings from popping up in GCC >= 8.1

In file included from utp_api.cpp:26:
utp_internal.h: In constructor ‘UTPSocketKey::UTPSocketKey(const PackedSockAddr&, uint32)’:
utp_internal.h:79:32: warning: ‘void* memset(void*, int, size_t)’ clearing an object of non-trivial type ‘struct UTPSocketKey’; use assignment instead [-Wclass-memaccess]
   79 |   memset(this, 0, sizeof(*this));
      |                                ^
utp_internal.h:74:8: note: ‘struct UTPSocketKey’ declared here
   74 | struct UTPSocketKey {
      |        ^~~~~~~~~~~~

Resolved like in this issue: oneapi-src/oneTBB#54

Prevent warnings from popping up in GCC >= 8.1
@@ -76,7 +76,7 @@ struct UTPSocketKey {
uint32 recv_id; // "conn_seed", "conn_id"

UTPSocketKey(const PackedSockAddr& _addr, uint32 _recv_id) {
memset(this, 0, sizeof(*this));
memset(static_cast<void*>(this), 0, sizeof(*this));
Copy link
Contributor

Choose a reason for hiding this comment

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

How about removing this line to resolve the problem? This structure contains two members, both of which are initialized in this constructor in statements immediately after this statement, so the memset call here seems unnecessary. I would hope that code isn't depending that any inter-member space in this structure be initialized to zeroes. Could even change the following assignments to be within an initialization list.

Copy link
Author

@nenad nenad Aug 5, 2019

Choose a reason for hiding this comment

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

I am not experienced in C or C++, so I started reading around memset and usage in zeroing out structures. Seems like this is done intentionally, so that we don't leak information about the structure through the network. Since it's a bit controversial, I'd prefer if we don't remove it.

Copy link
Contributor

Choose a reason for hiding this comment

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

It's been a number of years now, but if I recall correctly this line was added to try to work around a bug in the hash table implementation which was later fixed by Art. It may not be needed now, but you'll probably want to do a bunch of QA testing first to confirm that.

Copy link
Contributor

Choose a reason for hiding this comment

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

If this structure is passed in its entirety via the network, I could see clearing it out to avoid disclosure. Either for this reason, or for the case that this was done to avoid a hash table bug, I'd like to see a comment that explains this inserted before the memset() call if the memset() line is retained, since this would be non-obvious valuable information that could easily be lost.

Copy link
Author

Choose a reason for hiding this comment

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

@rmcdonald / @mct Could you ping someone who worked on this to add the comment about the reason?

Copy link
Contributor

Choose a reason for hiding this comment

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

my recollection is that the hash table implementation used in uTorrent (not sure if it's still used in libutp) hashes the storage bytes of the key, and compares them with memcmp(). In other words, the padding bits are used by the hash table (or at least "were", it may have changed). I would think that's at least the reason for this call to memset().

As an alternative fix for this, the hash table could have an assert like this:

static_assert(std::has_unique_object_representations_v<Key>, "Key type may not have padding bits")

Then it would be safe to remove the memset() here (afaict).

Copy link
Author

Choose a reason for hiding this comment

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

I believe these are some places in the code where we have hints (as far as I could understand) that there might be some reasoning behind the memset.

https://github.com/bittorrent/libutp/blob/master/utp_hash.h#L43-L75
https://github.com/bittorrent/libutp/blob/master/utp_hash.cpp#L113-L118

I don't know how to test this properly and would appreciate any help.

@nenad
Copy link
Author

nenad commented Aug 12, 2019

Apologies for breaking the silence 🙊. Is the PR good enough to be merged as it is without removing the line?

@nenad
Copy link
Author

nenad commented Sep 16, 2019

Hi again! I was going through the list of my PRs and I noticed this one hasn't been active for quite some time. Personally, I find warnings in my build logs not pretty, and this PR addresses that issue. Would it be able to get this merged, or should I close it and leave the warnings?

@Jorropo
Copy link

Jorropo commented Nov 3, 2019

Hi, what is the status of this PR pls ?
I'm using libutp in golang and that (a small okay but still one) problem, I'm making a lib with it and gcc warning are not a good figure to end user even if there is 5 lib layer between libutp and the end user.

@arvidn
Copy link
Contributor

arvidn commented Nov 3, 2019

I would approve a PR that removes the memset and adds the static_assert I suggested earlier. nut I don’t have privileges to merge

@anacrolix
Copy link
Contributor

Any update on this? Would anyone be willing to merge this?

@arvidn
Copy link
Contributor

arvidn commented Jun 15, 2020

I do not think this proposed patch is a good solution to the GCC warning. The warning is right, this patch just silences it instead of fixing the code.

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.

6 participants