Re: fix: recvfrom() error handling
From: Carlos O'Donell <hidden>
Date: 2025-03-28 22:45:29
On 3/27/25 1:47 PM, Peter Radisson wrote:
Am 27.03.25 um 18:28 schrieb Alejandro Colomar:quoted
On Thu, Mar 27, 2025 at 05:35:21PM +0100, Peter Radisson wrote:quoted
V2: * removed 1 empty line * and changed wording to "content of" to make clear *addrlen is used.V3: * changed wording: less that null -> negativ
s/negativ/negative/g
quoted hunk ↗ jump to hunk
thx for fast reply, unfortunately i noticed an other problem with the page. I will report in an other mail.quoted
quoted
To replicate the problem: // intended use struct sockaddr_in sock_out; int slen=sizeof(sock_out); //socklen_t recv_len = recvfrom(s, buf, BUFLEN, 0, (struct sockaddr *) &sock_out, &slen); // error case struct sockaddr_in sock_out; int slen=1; recv_len = recvfrom(s, buf, BUFLEN, 0, (struct sockaddr *) &sock_out, &slen); funfact: this escapes the error handling in the linux kernel (no crash) set slen=-1 and you get EINVAL. hope that helps.From 9f464fde8dd168b71430ca29f631153e3e3fb2e5 Mon Sep 17 00:00:00 2001 From: Peter Radisson <--show-origin> Date: Thu, 27 Mar 2025 18:39:29 +0100 Subject: [PATCH] Be more verbose about recvfrom(2) error handling Signed-off-by: Peter Radisson <--show-origin> --- man2/recv.2 | 16 ++++++++++++++++ 1 file changed, 16 insertions(+)diff --git a/man2/recv.2 b/man2/recv.2 index 2659957a6..ba17d03a3 100644 --- a/man2/recv.2 +++ b/man2/recv.2@@ -293,6 +293,22 @@ The returned address is truncated if the bufferprovided is too small; in this case, .I addrlen will return a value greater than was supplied to the call. +If +.I src_addr +is NULL +.I addrlen +will be ignored. +If +.I src_addr +is not NULL and the content of +.I addrlen +is negativ the call will return with
s/negativ/negative/g
+.IR EINVAL . +If +.I addrlen +is less than sizeof struct sockaddr_in the src_addr will +not be modified.
My suggestion would be to place this as an entry under ERRORS for EINVAL. Adding all of this conditional text under recvfrom() seems overly complicated. We should document the success case and how it work for truncation.
+ .PP If the caller is not interested in the source address, .I src_addr -- 2.35.3
-- Cheers, Carlos.