stephane / libmodbus

A Modbus library for Linux, Mac OS, FreeBSD and Windows
http://libmodbus.org
GNU Lesser General Public License v2.1
3.29k stars 1.71k forks source link

With TCP, `modbus_connect()` returns -1 on timeout but does not set `errno` #736

Open kyllingstad opened 5 months ago

kyllingstad commented 5 months ago

libmodbus version

3f9f17e228f70ce2fcdc1988cf7563fc1bf78eec (current master)

OS and/or distribution

Windows (but I believe the problem also exists on *NIX)

Environment

N/A

Description

With TCP, modbus_connect() returns -1 on timeout, but does not set errno.

The reason seems to be that the timeout is implemented via select(), where a timeout is not considered an error. The offending code is in lines 295–299 of modbus-tcp.c, which look like this:

        rc = select(sockfd + 1, NULL, &wset, NULL, &tv);
        if (rc <= 0) {
            /* Timeout or fail */
            return -1;
        }

Expected behavior or suggestion

errno == ETIMEDOUT