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

Fix potential NextPacketID endless loop, expand tests #169

Merged
merged 4 commits into from
Feb 10, 2023

Conversation

mochi-co
Copy link
Collaborator

Fixes potential endless loop scenario with clients.NextPacketID and updates related tests to cover this scenario.
cc @thedevop Thank you for your code sample - it really helped illustrate the issue!

Copy link
Collaborator

@thedevop thedevop left a comment

Choose a reason for hiding this comment

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

This works. However, there is a slightly optimized solution:
Change:

	i = atomic.LoadUint32(&cl.State.packetID)
	if i >= 65535 {
		i = 0
	}

	started := i + 1

to

	i = atomic.LoadUint32(&cl.State.packetID)
	started := i

With started := i + 1, started will be checked twice.

Example with started := i
Example with started := i + 1

@mochi-co
Copy link
Collaborator Author

@thedevop I believe this optimization will put the method into an infinite loop if all packet ids are allocated, as started can be 65535 but if overflowed && i == started { can never be reached.

@mochi-co
Copy link
Collaborator Author

Optimization was implemented by refactoring NextPacketID, and small changes were made to enhance ease of testing (introduction of unexported Capabilities.maximumPacketID field). Additionally, max uint16 values were replaced with math constants.

@mochi-co mochi-co merged commit e5716ca into master Feb 10, 2023
@mochi-co mochi-co deleted the fix-packet-id-overflow branch February 10, 2023 23:27
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.

2 participants