Prevent Panic in DNS Socket by Truncating Server List #986
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
This PR addresses an issue in the Socket constructor where passing a server list longer than
DNS_MAX_SERVER_COUNT
would cause a panic. The number of DNS servers available is often dictated by the network configuration, especially when using DHCP, and is not something the developer can directly control. As such, the previous behaviour of panicking when more servers are provided than allowed feels like a landmine.The code now safely truncates the server list to ensure it does not exceed the maximum allowed number of servers, preventing any runtime panics.
Changes
Why This Fix?
This fix is essential for environments where the number of servers may dynamically change or where input server lists might inadvertently exceed the allowed limit. The developer cannot know ahead of time how many DNS servers might be available to a user, and although setting
DNS_MAX_SERVER_COUNT
to a higher value limits the likelihood of this happening, it can still happen. The previous behaviour would result in a panic, whereas the new implementation gracefully handles the case by truncating the server list.