Fix freeaddrinfo(NULL) in _modbus_tcp_pi_connect#853
Open
NeriaIfrah wants to merge 1 commit into
Open
Conversation
When getaddrinfo() fails in _modbus_tcp_pi_connect, the error path calls freeaddrinfo(ai_list) unconditionally. POSIX leaves the contents of *res unspecified on getaddrinfo() failure, and freeaddrinfo(NULL) is undefined behaviour. On musl libc this causes a segfault, so a hostname that fails to resolve crashes the host application instead of returning -1 with errno=ECONNREFUSED as documented. This is the same defect that was fixed for the server-side mirror function modbus_tcp_pi_listen in commit 26dc8a5 ("Check if ai_list is null before freeing it (stephane#831)"). The client-side mirror has the byte-for-byte identical error-handling code and was overlooked. Apply the same guard pattern to _modbus_tcp_pi_connect to complete the fix on the client path. Affects Alpine, OpenWrt, and other musl-based deployments, which are core libmodbus targets. Closes #<NEW_ISSUE_NUMBER>
|
We require contributors to sign our Contributor License Agreement. In order for us to review and merge your code, please fill https://forms.gle/5635zjphDo5JEJQSA to get added. Your document will be manually checked by the maintainer. Be patient... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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
Completes the fix from #831 / 26dc8a5 by applying the same
NULL-guardto the client-side mirror function
_modbus_tcp_pi_connect, which hasidentical error-handling code and was overlooked when the server-side
function was patched.
Closes #852.
Root cause
_modbus_tcp_pi_connectinsrc/modbus-tcp.ccallsfreeaddrinfo(ai_list)unconditionally whengetaddrinfo()fails:POSIX leaves the contents of
*resunspecified ongetaddrinfo()failure, and
freeaddrinfo(NULL)is undefined behaviour. Thepractical breakdown:
freeaddrinfo(NULL)behaviourAlpine, OpenWrt, Void (musl variant), and most musl-based Buildroot/
Yocto images are therefore vulnerable. The trigger is not a malformed
packet — it's a hostname that fails to resolve, which is routine
operational reality on industrial gateways during DHCP/DNS warm-up,
transient DNS outages, or configuration typos.
See #852 for the full bug report, reproduction steps, and impact analysis.
Fix