Calls to modbus slave functionality can result in resource leaks

Asked by Daniel Adams

I have begun using the slave side of libmodbus and have been reading the code to figure out the intended usage. I have noticed that in the function modbus_init_listen_tcp accepts an incoming socket connection on the listening socket new_connection which it then saves to mbparams->fd. This socket new_socket is then returned back to the user as a return code. From the API it is not apparent that this is the returned value and could potentially result in a resource leak.

I'm not sure what the intention of returning the listening socket back to user is. They can't call any API functions which could reuse this listening socket since subsequent calls to modbus_init_listen_tcp result in an attempt of a new socket being created which would fail since the original socket (address and port) is still in use.

Also I have noticed that this function calls exit(1) in several locations. I feel that it is a bad assumption that a user would want the process to exit if the socket can't create, bind or listen to incoming connections.

I wasn't sure if all of these were considered bugs, so I've posted it as a question instead. I am using the 2.0.3 source.

I can submit an official bug report with suggested patches if these are considered bugs

Thanks.

Question information

Language:
English Edit question
Status:
Solved
For:
libmodbus Edit question
Assignee:
No assignee Edit question
Last query:
Last reply:
Revision history for this message
Stéphane Raimbault (sra) said :
#1

Please, could you review the source code from trunk (not 2.0.3)? That contains many changes on this point.

Revision history for this message
Launchpad Janitor (janitor) said :
#2

This question was expired because it remained in the 'Needs information' state without activity for the last 15 days.

Revision history for this message
Stéphane Raimbault (sra) said :
#3

> From the API it is not apparent that this is the returned value and could potentially result in a resource leak.

A socket is just an int, at this point it's not a leak!
This socket is useful to accept several connections by server.

exit() functions are gone in latest version (github).