From afddb46c510ce000ddaa758f2979b62dfb5d22e6 Mon Sep 17 00:00:00 2001 From: iosabi Date: Sun, 4 Apr 2021 12:45:54 +0200 Subject: [PATCH] lwip_sock: Fix computation of last_offset on partial reads. lwip receives network buffers that are made available to lwip_sock_tcp calling `netconn_recv_tcp_pbuf`. The size of these buffers depends on the size of the network packets. An application calling `sock_tcp_read` can pass any arbitrary buffer size to read (copy) from these internal network buffers, which may be smaller. lwip_sock_tcp keeps around the last network buffer (`struct pbuf last_buf`) and the offset into this buffer already consumed by the application (`last_offset`). However, when multiple application reads from the same `pbuf` buffer occur, the `last_offset` must be updated incrementing it. The code had a bug that would work only when `last_offset` was either 0 (no previous partial read) or when the current read was consuming all the remaining data (when `buf_len == copylen`). This patch fixes the issue an allows multiple reads from the same buffer. --- pkg/lwip/contrib/sock/tcp/lwip_sock_tcp.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/lwip/contrib/sock/tcp/lwip_sock_tcp.c b/pkg/lwip/contrib/sock/tcp/lwip_sock_tcp.c index 03236664b03c..36a4146e656a 100644 --- a/pkg/lwip/contrib/sock/tcp/lwip_sock_tcp.c +++ b/pkg/lwip/contrib/sock/tcp/lwip_sock_tcp.c @@ -347,7 +347,7 @@ ssize_t sock_tcp_read(sock_tcp_t *sock, void *data, size_t max_len, if (buf_len > copylen) { /* there is still data in the buffer */ sock->last_buf = buf; - sock->last_offset = copylen; + sock->last_offset += copylen; } else { sock->last_buf = NULL;