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 redundant packet type check in handler with assert. #434

Merged
merged 1 commit into from
Jan 27, 2017

Conversation

yurivict
Copy link
Member

@yurivict yurivict commented Jan 15, 2017

This change is Reviewable

@iphydf
Copy link
Member

iphydf commented Jan 15, 2017

Review status: 0 of 1 files reviewed at latest revision, 1 unresolved discussion.


toxcore/DHT.c, line 2538 at r1 (raw file):

    DHT *dht = (DHT *)object;

    if (packet[0] == NET_PACKET_CRYPTO) {

How about:

if (packet[0] != NET_PACKET_CRYPTO) {
    assert(!"unexpected packet kind");
    return 1;
}

The check is redundant, but removing it makes the function's correctness non-local. It's a function precondition, and we currently don't have a good way to express those. Perhaps add a // TODO comment saying that function preconditions should be expressed better (e.g. with an assert-like macro we define for that purpose).


Comments from Reviewable

@iphydf
Copy link
Member

iphydf commented Jan 15, 2017

Reviewed 1 of 1 files at r1.
Review status: all files reviewed at latest revision, 1 unresolved discussion.


Comments from Reviewable

@yurivict
Copy link
Member Author

This is a very shallow check, it merely checks that the handler is plugged at the right slot, which it obviously is. Also no other handlers perform such check. I suggest it is deleted.

@iphydf
Copy link
Member

iphydf commented Jan 16, 2017

Review status: all files reviewed at latest revision, 1 unresolved discussion.


toxcore/DHT.c, line 2538 at r1 (raw file):

Previously, iphydf wrote…

How about:

if (packet[0] != NET_PACKET_CRYPTO) {
    assert(!"unexpected packet kind");
    return 1;
}

The check is redundant, but removing it makes the function's correctness non-local. It's a function precondition, and we currently don't have a good way to express those. Perhaps add a // TODO comment saying that function preconditions should be expressed better (e.g. with an assert-like macro we define for that purpose).

Can you just turn it into an assert then? "which it obviously is" is not a good criterion to remove a check. The fact that it's not checked elsewhere probably means we should check it there as well. It is a function precondition, because it won't work correctly if the handler was plugged into the wrong slot. The problem with not checking it at all is that if somehow we end up changing some code that ends up putting it into the wrong slot, it might fail silently doing some incorrect thing.


Comments from Reviewable

@yurivict
Copy link
Member Author

assert(packet[0] == NET_PACKET_CRYPTO); might be better.

@iphydf
Copy link
Member

iphydf commented Jan 16, 2017

Yes, that's what I meant.


Review status: all files reviewed at latest revision, 1 unresolved discussion.


Comments from Reviewable

@iphydf
Copy link
Member

iphydf commented Jan 16, 2017

:lgtm_strong:


Review status: 0 of 1 files reviewed at latest revision, all discussions resolved.


Comments from Reviewable

@iphydf
Copy link
Member

iphydf commented Jan 16, 2017

Reviewed 1 of 1 files at r2.
Review status: all files reviewed at latest revision, all discussions resolved.


Comments from Reviewable

@iphydf iphydf self-assigned this Jan 16, 2017
@iphydf iphydf modified the milestone: v0.1.5 Jan 16, 2017
@iphydf
Copy link
Member

iphydf commented Jan 18, 2017

Can you enable the checkbox that lets collaborators push to your PR branch? That way, I can rebase it on master and then merge after CI completes.

@iphydf iphydf assigned piling and unassigned piling Jan 18, 2017
@iphydf
Copy link
Member

iphydf commented Jan 19, 2017

Please rebase on master.

@piling
Copy link

piling commented Jan 19, 2017

:lgtm_strong:


Reviewed 1 of 1 files at r2.
Review status: all files reviewed at latest revision, all discussions resolved, some commit checks failed.


Comments from Reviewable

@iphydf iphydf modified the milestones: v0.1.5, v0.1.6 Jan 19, 2017
@iphydf
Copy link
Member

iphydf commented Jan 20, 2017

@yurivict can you rebase this on master and enable the checkbox (on the right) to allow us to rebase it again if necessary?

@iphydf
Copy link
Member

iphydf commented Jan 20, 2017

@yurivict also squash the two commits in one, and give it an appropriate title.

@iphydf iphydf changed the title Removed the redundant check of the packet's first byte in the handler. Replace redundant packet type check in handler with assert. Jan 22, 2017
@iphydf
Copy link
Member

iphydf commented Jan 25, 2017

@yurivict I'd like to push this before tomorrow. Will you have time to either click the checkbox on the right, or squash and rebase these commits on master?

@yurivict
Copy link
Member Author

I am sorry I have been busy lately to engage in this. Where is the button you talk about?

@dvor
Copy link
Member

dvor commented Jan 26, 2017

@yurivict I guess he is talking about this checkmark

i

@yurivict
Copy link
Member Author

It doesn't show such button on my screen:
github-no-button

@yurivict
Copy link
Member Author

It says "this pull request can be merged by project collaborators". Why don't you just do this?

@dvor
Copy link
Member

dvor commented Jan 26, 2017

@yurivict we want to update pull request with required changes before merging it.

Try scrolling to the top of the page. There should be block as on my screenshot where you will find needed checkmark.

@yurivict
Copy link
Member Author

Ok, found it.

@iphydf iphydf force-pushed the redundant-packet-byte-checks branch from f0f57a5 to 2f62fa3 Compare January 26, 2017 22:29
@iphydf
Copy link
Member

iphydf commented Jan 27, 2017

"This pull request can be merged" is a lie. If I try, it actually doesn't work. That's a github bug.


Reviewed 1 of 1 files at r3.
Review status: all files reviewed at latest revision, all discussions resolved, some commit checks failed.


Comments from Reviewable

@iphydf iphydf merged commit 2f62fa3 into TokTok:master Jan 27, 2017
This pull request was closed.
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.

4 participants