public inbox for passt-dev@passt.top
 help / color / mirror / code / Atom feed
* [PATCH] tcp_vu: Fix assert/error-check order in tcp_vu_sock_recv()
@ 2026-11-09 22:13 Jon Maloy
  2026-11-10  9:29 ` Stefano Brivio
  0 siblings, 1 reply; 3+ messages in thread
From: Jon Maloy @ 2026-11-09 22:13 UTC (permalink / raw)
  To: sbrivio, david, jmaloy, passt-dev

iov_tail_clone() returns a ssize_t. However, the assert comparing
it against (ARRAY_SIZE(iov_msg) - j), which yields size_t, comes
before the negative-value check. Hence, when iov_tail_clone() fails
and returns -1, the signed-to-unsigned promotion turns -1 into
SIZE_MAX, and causes the assert to fire.

We fix this by moving the error check before the assert, while casting
the counter and the index inside the assert to size_t.

Signed-off-by: Jon Maloy <jmaloy@redhat.com>
---
 tcp_vu.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/tcp_vu.c b/tcp_vu.c
index 868a35b9..c052d8e2 100644
--- a/tcp_vu.c
+++ b/tcp_vu.c
@@ -291,9 +291,9 @@ static ssize_t tcp_vu_sock_recv(const struct ctx *c, struct vu_virtq *vq,
 
 		cnt = iov_tail_clone(&iov_msg[j], ARRAY_SIZE(iov_msg) - j,
 				     &data);
-		assert(cnt < ARRAY_SIZE(iov_msg) - j);
 		if (cnt < 0)
 			die("Missing entries in iov_msg");
+		assert((size_t)cnt < ARRAY_SIZE(iov_msg) - (size_t)j);
 
 		j += cnt;
 	}
-- 
2.52.0


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] tcp_vu: Fix assert/error-check order in tcp_vu_sock_recv()
  2026-11-09 22:13 [PATCH] tcp_vu: Fix assert/error-check order in tcp_vu_sock_recv() Jon Maloy
@ 2026-11-10  9:29 ` Stefano Brivio
  2026-11-10  9:47   ` Laurent Vivier
  0 siblings, 1 reply; 3+ messages in thread
From: Stefano Brivio @ 2026-11-10  9:29 UTC (permalink / raw)
  To: Jon Maloy; +Cc: david, passt-dev, contact, Laurent Vivier

[Cc: Johannes, Laurent]

Thanks for the patch and the explanation, but shouldn't this be:

On Mon,  9 Nov 2026 17:13:42 -0500
Jon Maloy <jmaloy@redhat.com> wrote:

From: Johannes Bartunek <contact@jbartunek.de>

and:

> iov_tail_clone() returns a ssize_t. However, the assert comparing
> it against (ARRAY_SIZE(iov_msg) - j), which yields size_t, comes
> before the negative-value check. Hence, when iov_tail_clone() fails
> and returns -1, the signed-to-unsigned promotion turns -1 into
> SIZE_MAX, and causes the assert to fire.
> 
> We fix this by moving the error check before the assert, while casting
> the counter and the index inside the assert to size_t.

Reported-by: Johannes Bartunek <contact@jbartunek.de>
Suggested-by: Laurent Vivier <lvivier@redhat.com>

?

> Signed-off-by: Jon Maloy <jmaloy@redhat.com>
> ---
>  tcp_vu.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/tcp_vu.c b/tcp_vu.c
> index 868a35b9..c052d8e2 100644
> --- a/tcp_vu.c
> +++ b/tcp_vu.c
> @@ -291,9 +291,9 @@ static ssize_t tcp_vu_sock_recv(const struct ctx *c, struct vu_virtq *vq,
>  
>  		cnt = iov_tail_clone(&iov_msg[j], ARRAY_SIZE(iov_msg) - j,
>  				     &data);
> -		assert(cnt < ARRAY_SIZE(iov_msg) - j);
>  		if (cnt < 0)
>  			die("Missing entries in iov_msg");
> +		assert((size_t)cnt < ARRAY_SIZE(iov_msg) - (size_t)j);
>  
>  		j += cnt;
>  	}

-- 
Stefano


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] tcp_vu: Fix assert/error-check order in tcp_vu_sock_recv()
  2026-11-10  9:29 ` Stefano Brivio
@ 2026-11-10  9:47   ` Laurent Vivier
  0 siblings, 0 replies; 3+ messages in thread
From: Laurent Vivier @ 2026-11-10  9:47 UTC (permalink / raw)
  To: Stefano Brivio, Jon Maloy; +Cc: david, passt-dev, contact

On 11/10/26 10:29, Stefano Brivio wrote:
> [Cc: Johannes, Laurent]
> 
> Thanks for the patch and the explanation, but shouldn't this be:
> 
> On Mon,  9 Nov 2026 17:13:42 -0500
> Jon Maloy <jmaloy@redhat.com> wrote:
> 
> From: Johannes Bartunek <contact@jbartunek.de>
> 
> and:
> 
>> iov_tail_clone() returns a ssize_t. However, the assert comparing
>> it against (ARRAY_SIZE(iov_msg) - j), which yields size_t, comes
>> before the negative-value check. Hence, when iov_tail_clone() fails
>> and returns -1, the signed-to-unsigned promotion turns -1 into
>> SIZE_MAX, and causes the assert to fire.
>>
>> We fix this by moving the error check before the assert, while casting
>> the counter and the index inside the assert to size_t.
> 
> Reported-by: Johannes Bartunek <contact@jbartunek.de>
> Suggested-by: Laurent Vivier <lvivier@redhat.com>

Not exactly...

> 
> ?
> 
>> Signed-off-by: Jon Maloy <jmaloy@redhat.com>
>> ---
>>   tcp_vu.c | 2 +-
>>   1 file changed, 1 insertion(+), 1 deletion(-)
>>
>> diff --git a/tcp_vu.c b/tcp_vu.c
>> index 868a35b9..c052d8e2 100644
>> --- a/tcp_vu.c
>> +++ b/tcp_vu.c
>> @@ -291,9 +291,9 @@ static ssize_t tcp_vu_sock_recv(const struct ctx *c, struct vu_virtq *vq,
>>   
>>   		cnt = iov_tail_clone(&iov_msg[j], ARRAY_SIZE(iov_msg) - j,
>>   				     &data);
>> -		assert(cnt < ARRAY_SIZE(iov_msg) - j);
>>   		if (cnt < 0)
>>   			die("Missing entries in iov_msg");
>> +		assert((size_t)cnt < ARRAY_SIZE(iov_msg) - (size_t)j);

The real fix is:

assert((size_t)cnt <= ARRAY_SIZE(iov_msg) - (size_t)j);

>>   
>>   		j += cnt;
>>   	}
> 

Thanks,
Laurent


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-11-10  9:47 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-09 22:13 [PATCH] tcp_vu: Fix assert/error-check order in tcp_vu_sock_recv() Jon Maloy
2026-11-10  9:29 ` Stefano Brivio
2026-11-10  9:47   ` Laurent Vivier

Code repositories for project(s) associated with this public inbox

	https://passt.top/passt

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for IMAP folder(s).