Skip to content

Fix masking of irrelevant bits in address groups.#6556

Merged
laanwj merged 2 commits into
bitcoin:masterfrom
morcos:patch_netBits
Aug 20, 2015
Merged

Fix masking of irrelevant bits in address groups.#6556
laanwj merged 2 commits into
bitcoin:masterfrom
morcos:patch_netBits

Conversation

@morcos

@morcos morcos commented Aug 14, 2015

Copy link
Copy Markdown
Contributor

If I'm reading this right, I believe the intent is to keep the high order nBits and mask away the others with 1's. But before this fix it was keeping the high order (8 - nBits). I don't think there was any effect, because the only time this was used was in the case of he.net /36 IPv6 groups so nBits was 4.

@sipa

sipa commented Aug 15, 2015

Copy link
Copy Markdown
Member

Nice catch. Changing the | into an & seems simpler, though.

@dcousens

Copy link
Copy Markdown
Contributor

Agreed, the & would be much clearer here and is more typical for a masking operation of this type.

@morcos

morcos commented Aug 17, 2015

Copy link
Copy Markdown
Contributor Author

I thought about & but I wasn't confident it was ok to change the previous behavior.
I also couldn't see how to make it as short?
vchRet.push_back(GetByte(15 - nStartByte) & (((1 << nBits) - 1) << (8 - nBits)));
or
vchRet.push_back(GetByte(15 - nStartByte) & ~((1 << (8-nBits)) - 1));

@sipa

sipa commented Aug 17, 2015

Copy link
Copy Markdown
Member

Good point, we shouldn't change the existing behaviour.

ACK

@jonasschnelli

Copy link
Copy Markdown
Contributor

utACK.
Would be nice if there would be test coverage in netbase_tests.cpp for this.

@laanwj

laanwj commented Aug 19, 2015

Copy link
Copy Markdown
Member

utACK.
There was a similar error in the netmask parsing.
Agree @jonasschnelli that unit tests for this function would be nice.

@morcos

morcos commented Aug 19, 2015

Copy link
Copy Markdown
Contributor Author

Ok writing unit test. I have a question about IsLocal(), since those addresses are also !IsRoutable() they will be lumped in the NET_UNROUTABLE group instead of having their own group. The check for IsLocal() on line 932 is meaningless. Is that expected? Changing that would obviously change existing behavior.

@laanwj

laanwj commented Aug 20, 2015

Copy link
Copy Markdown
Member

Yes, that check seems to be redundant. But let's leave removing it for another pull.

Thanks for adding tests.

@laanwj laanwj merged commit 1123cdb into bitcoin:master Aug 20, 2015
laanwj added a commit that referenced this pull request Aug 20, 2015
1123cdb add unit test for CNetAddr::GetGroup. (Alex Morcos)
bba3db1 Fix masking of irrelevant bits in address groups. (Alex Morcos)
@laanwj laanwj added the Bug label Aug 20, 2015
furszy added a commit to PIVX-Project/PIVX that referenced this pull request May 21, 2020
5d7887e add unit test for CNetAddr::GetGroup. (Alex Morcos)
f0e0aca Fix masking of irrelevant bits in address groups. (Alex Morcos)

Pull request description:

  Backport of bitcoin#6556

  Original description:

  > If I'm reading this right, I believe the intent is to keep the high order nBits and mask away the others with 1's. But before this fix it was keeping the high order (8 - nBits). I don't think there was any effect, because the only time this was used was in the case of he.net /36 IPv6 groups so nBits was 4.

ACKs for top commit:
  random-zebra:
    utACK 5d7887e
  furszy:
    utACK 5d7887e

Tree-SHA512: 8cc095561918c838b46dd7b957ab5a7acc8788fa568b760157f026e012bada4d6dc5b7f6c4b2dfc58dc3f05382237bb265c61cc4c76175aff9341dafeea567e8
@bitcoin bitcoin locked as resolved and limited conversation to collaborators Sep 8, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants