libmodbus icon indicating copy to clipboard operation
libmodbus copied to clipboard

Prevent segfault in TCP connect when using MUSL

Open micrictor opened this issue 2 months ago • 1 comments

Passing null to freeaddrinfo is undefined behaviour. While glibc and uClibc both handle it safely, musl does not, so a bad nodename or servname causes a segfault.

This change ensures that the ai_list is non-null before trying to free it in the connect function. The check already exists for listen

In musl and glibc this condition will never be met, meaning freeaddrinfo will never actually be ran. There's no possibility for errors to be returned after memory allocation occurs in those implementations either, so there's no remaining risk of memory leaks.

In uClibc, there are some code paths where errors occur after memory is allocated, and this still guarantees it will get freed.

micrictor avatar Nov 09 '25 18:11 micrictor

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...

cla-bot[bot] avatar Nov 09 '25 18:11 cla-bot[bot]