Linux block layer
 help / color / mirror / Atom feed
* [PATCH] rbd: cast snap_count to size_t to avoid tautological comparison warning
@ 2026-05-28 20:21 Rosen Penev
  2026-05-28 20:45 ` Alex Elder
  2026-05-28 21:05 ` David Laight
  0 siblings, 2 replies; 4+ messages in thread
From: Rosen Penev @ 2026-05-28 20:21 UTC (permalink / raw)
  To: linux-block
  Cc: Ilya Dryomov, Dongsheng Yang, Jens Axboe, Nathan Chancellor,
	Nick Desaulniers, Bill Wendling, Justin Stitt,
	open list:RADOS BLOCK DEVICE (RBD), open list,
	open list:CLANG/LLVM BUILD SUPPORT:Keyword:b(?i:clang|llvm)b

snap_count is u32 but the comparison is against a SIZE_MAX-derived value
(~2^61 on 64-bit), which clang flags as always false with
-Wtautological-constant-out-of-range-compare. Cast to size_t so the
comparison is done in the correct width.

Assisted-by: Opencode:Big-pickle
Signed-off-by: Rosen Penev <rosenp@gmail.com>
---
 drivers/block/rbd.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/block/rbd.c b/drivers/block/rbd.c
index 94709466ad19..b4ba51db9a28 100644
--- a/drivers/block/rbd.c
+++ b/drivers/block/rbd.c
@@ -6079,7 +6079,7 @@ static int rbd_dev_v2_snap_context(struct rbd_device *rbd_dev,
 	 * make sure the computed size of the snapshot context we
 	 * allocate is representable in a size_t.
 	 */
-	if (snap_count > (SIZE_MAX - sizeof (struct ceph_snap_context))
+	if ((size_t)snap_count > (SIZE_MAX - sizeof (struct ceph_snap_context))
 				 / sizeof (u64)) {
 		ret = -EINVAL;
 		goto out;
-- 
2.54.0


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

* Re: [PATCH] rbd: cast snap_count to size_t to avoid tautological comparison warning
  2026-05-28 20:21 [PATCH] rbd: cast snap_count to size_t to avoid tautological comparison warning Rosen Penev
@ 2026-05-28 20:45 ` Alex Elder
  2026-05-28 21:05 ` David Laight
  1 sibling, 0 replies; 4+ messages in thread
From: Alex Elder @ 2026-05-28 20:45 UTC (permalink / raw)
  To: Rosen Penev, linux-block
  Cc: Ilya Dryomov, Dongsheng Yang, Jens Axboe, Nathan Chancellor,
	Nick Desaulniers, Bill Wendling, Justin Stitt,
	open list:RADOS BLOCK DEVICE (RBD), open list,
	open list:CLANG/LLVM BUILD SUPPORT:Keyword:b(?i:clang|llvm)b

On 5/28/26 3:21 PM, Rosen Penev wrote:
> snap_count is u32 but the comparison is against a SIZE_MAX-derived value
> (~2^61 on 64-bit), which clang flags as always false with
> -Wtautological-constant-out-of-range-compare. Cast to size_t so the
> comparison is done in the correct width.
> 
> Assisted-by: Opencode:Big-pickle
> Signed-off-by: Rosen Penev <rosenp@gmail.com>

This is a simple fix.

You might consider doing something similar for this code in
rbd_dev_ondisk_valid():

         snap_count = le32_to_cpu(ondisk->snap_count);
         size = SIZE_MAX - sizeof (struct ceph_snap_context);
         if (snap_count > size / sizeof (__le64))
                 return false;

Reviewed-by: Alex Elder <elder@riscstar.com>


> ---
>   drivers/block/rbd.c | 2 +-
>   1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/drivers/block/rbd.c b/drivers/block/rbd.c
> index 94709466ad19..b4ba51db9a28 100644
> --- a/drivers/block/rbd.c
> +++ b/drivers/block/rbd.c
> @@ -6079,7 +6079,7 @@ static int rbd_dev_v2_snap_context(struct rbd_device *rbd_dev,
>   	 * make sure the computed size of the snapshot context we
>   	 * allocate is representable in a size_t.
>   	 */
> -	if (snap_count > (SIZE_MAX - sizeof (struct ceph_snap_context))
> +	if ((size_t)snap_count > (SIZE_MAX - sizeof (struct ceph_snap_context))
>   				 / sizeof (u64)) {
>   		ret = -EINVAL;
>   		goto out;


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

* Re: [PATCH] rbd: cast snap_count to size_t to avoid tautological comparison warning
  2026-05-28 20:21 [PATCH] rbd: cast snap_count to size_t to avoid tautological comparison warning Rosen Penev
  2026-05-28 20:45 ` Alex Elder
@ 2026-05-28 21:05 ` David Laight
  2026-05-28 21:20   ` Alex Elder
  1 sibling, 1 reply; 4+ messages in thread
From: David Laight @ 2026-05-28 21:05 UTC (permalink / raw)
  To: Rosen Penev
  Cc: linux-block, Ilya Dryomov, Dongsheng Yang, Jens Axboe,
	Nathan Chancellor, Nick Desaulniers, Bill Wendling, Justin Stitt,
	open list:RADOS BLOCK DEVICE (RBD), open list,
	open list:CLANG/LLVM BUILD SUPPORT:Keyword:b(?i:clang|llvm)b

On Thu, 28 May 2026 13:21:51 -0700
Rosen Penev <rosenp@gmail.com> wrote:

> snap_count is u32 but the comparison is against a SIZE_MAX-derived value
> (~2^61 on 64-bit), which clang flags as always false with
> -Wtautological-constant-out-of-range-compare. Cast to size_t so the
> comparison is done in the correct width.

If that warning makes any sense then the cast shouldn't make any difference.

Why not check against RBD_MAX_SNAP_COUNT - the buffer isn't big enough
to hold any more than that.

-- David

> 
> Assisted-by: Opencode:Big-pickle
> Signed-off-by: Rosen Penev <rosenp@gmail.com>
> ---
>  drivers/block/rbd.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/drivers/block/rbd.c b/drivers/block/rbd.c
> index 94709466ad19..b4ba51db9a28 100644
> --- a/drivers/block/rbd.c
> +++ b/drivers/block/rbd.c
> @@ -6079,7 +6079,7 @@ static int rbd_dev_v2_snap_context(struct rbd_device *rbd_dev,
>  	 * make sure the computed size of the snapshot context we
>  	 * allocate is representable in a size_t.
>  	 */
> -	if (snap_count > (SIZE_MAX - sizeof (struct ceph_snap_context))
> +	if ((size_t)snap_count > (SIZE_MAX - sizeof (struct ceph_snap_context))
>  				 / sizeof (u64)) {
>  		ret = -EINVAL;
>  		goto out;


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

* Re: [PATCH] rbd: cast snap_count to size_t to avoid tautological comparison warning
  2026-05-28 21:05 ` David Laight
@ 2026-05-28 21:20   ` Alex Elder
  0 siblings, 0 replies; 4+ messages in thread
From: Alex Elder @ 2026-05-28 21:20 UTC (permalink / raw)
  To: David Laight, Rosen Penev
  Cc: linux-block, Ilya Dryomov, Dongsheng Yang, Jens Axboe,
	Nathan Chancellor, Nick Desaulniers, Bill Wendling, Justin Stitt,
	open list:RADOS BLOCK DEVICE (RBD), open list,
	open list:CLANG/LLVM BUILD SUPPORT:Keyword:b(?i:clang|llvm)b

On 5/28/26 4:05 PM, David Laight wrote:
> On Thu, 28 May 2026 13:21:51 -0700
> Rosen Penev <rosenp@gmail.com> wrote:
> 
>> snap_count is u32 but the comparison is against a SIZE_MAX-derived value
>> (~2^61 on 64-bit), which clang flags as always false with
>> -Wtautological-constant-out-of-range-compare. Cast to size_t so the
>> comparison is done in the correct width.
> 
> If that warning makes any sense then the cast shouldn't make any difference.
> 
> Why not check against RBD_MAX_SNAP_COUNT - the buffer isn't big enough
> to hold any more than that.

I like that better.  Please do that instead (despite my
Reviewed-by provided earlier).

					-Alex

> 
> -- David
> 
>>
>> Assisted-by: Opencode:Big-pickle
>> Signed-off-by: Rosen Penev <rosenp@gmail.com>
>> ---
>>   drivers/block/rbd.c | 2 +-
>>   1 file changed, 1 insertion(+), 1 deletion(-)
>>
>> diff --git a/drivers/block/rbd.c b/drivers/block/rbd.c
>> index 94709466ad19..b4ba51db9a28 100644
>> --- a/drivers/block/rbd.c
>> +++ b/drivers/block/rbd.c
>> @@ -6079,7 +6079,7 @@ static int rbd_dev_v2_snap_context(struct rbd_device *rbd_dev,
>>   	 * make sure the computed size of the snapshot context we
>>   	 * allocate is representable in a size_t.
>>   	 */
>> -	if (snap_count > (SIZE_MAX - sizeof (struct ceph_snap_context))
>> +	if ((size_t)snap_count > (SIZE_MAX - sizeof (struct ceph_snap_context))
>>   				 / sizeof (u64)) {
>>   		ret = -EINVAL;
>>   		goto out;
> 
> 


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

end of thread, other threads:[~2026-05-28 21:20 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-05-28 20:21 [PATCH] rbd: cast snap_count to size_t to avoid tautological comparison warning Rosen Penev
2026-05-28 20:45 ` Alex Elder
2026-05-28 21:05 ` David Laight
2026-05-28 21:20   ` Alex Elder

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox