From 41326c3f4276b9712f947da72feb9242642bf971 Mon Sep 17 00:00:00 2001 From: rim Date: Mon, 29 Apr 2024 17:08:12 +0300 Subject: [PATCH] socket: skt_tcp_stat_text() always return used buf size. --- src/net/socket.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/net/socket.c b/src/net/socket.c index 5e4a5d6..1a72317 100644 --- a/src/net/socket.c +++ b/src/net/socket.c @@ -846,8 +846,10 @@ skt_tcp_stat_text(uintptr_t skt, const char *tabs, optlen = sizeof(info); memset(&info, 0x00, sizeof(info)); - if (0 != getsockopt((int)skt, IPPROTO_TCP, TCP_INFO, &info, &optlen)) + if (0 != getsockopt((int)skt, IPPROTO_TCP, TCP_INFO, &info, &optlen)) { + (*buf_size_ret) = 0; return (errno); + } if (10 < info.tcpi_state) { info.tcpi_state = 11; /* UNKNOWN */ } @@ -858,8 +860,10 @@ skt_tcp_stat_text(uintptr_t skt, const char *tabs, continue; topts_used += strlcpy((topts + topts_used), tcpi_options_flags_str[i], (sizeof(topts) - topts_used)); - if ((sizeof(topts) - 2) < topts_used) + if ((sizeof(topts) - 2) < topts_used) { + (*buf_size_ret) = topts_used; return (ENOSPC); + } topts[topts_used ++] = ' '; } /* Remove trailing space and make sure that 0x00 at the end of string. */ @@ -956,8 +960,10 @@ skt_tcp_stat_text(uintptr_t skt, const char *tabs, ); #endif /* Linux specific code. */ - if (0 > rc) /* Error. */ + if (0 > rc) { /* Error. */ + (*buf_size_ret) = 0; return (EFAULT); + } (*buf_size_ret) = (size_t)rc; if (buf_size <= (size_t)rc) /* Truncated. */ return (ENOSPC);