Fixed endless loop in receive function in case of a incomplete frame

(limit = 3 timeouts)
This commit is contained in:
Stefan Wahren
2012-05-29 19:15:52 +02:00
parent dd97141f21
commit ad4d307b57
2 changed files with 29 additions and 3 deletions

View File

@ -165,7 +165,7 @@ int
mbus_tcp_recv_frame(mbus_tcp_handle *handle, mbus_frame *frame)
{
char buff[PACKET_BUFF_SIZE];
int len, remaining, nread;
int len, remaining, nread, timeouts;
if (handle == NULL || frame == NULL)
{
@ -180,6 +180,7 @@ mbus_tcp_recv_frame(mbus_tcp_handle *handle, mbus_frame *frame)
//
remaining = 1; // start by reading 1 byte
len = 0;
timeouts = 0;
do {
@ -188,6 +189,18 @@ mbus_tcp_recv_frame(mbus_tcp_handle *handle, mbus_frame *frame)
mbus_error_str_set("M-Bus tcp transport layer failed to read data.");
return -1;
}
if (nread == 0)
{
timeouts++;
if (timeouts >= 3)
{
// abort to avoid endless loop
fprintf(stderr, "%s: Timeout\n", __PRETTY_FUNCTION__);
break;
}
}
len += nread;