Skip to contentSkip to conversation

rbd: cast snap_count to size_t to avoid tautological comparison warning

PATCH In review Rosen Penev 3 participants 4 messages May 28

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 rbd: cast snap_count to size_t to avoid tautological comparison warning

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
---
 drivers/block/rbd.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/block/rbd.c +1 -1
index 94709466ad19..b4ba51db9a28 100644
@@ -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;
Signature · 1 line
2.54.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 7 lines · Rosen Penev

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

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;
Quoted 17 lines
---
  drivers/block/rbd.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
drivers/block/rbd.c +1 -1
index 94709466ad19..b4ba51db9a28 100644
@@ -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 lore raw
In reply to Rosen Penev
Quoted 4 lines · On Thu, 28 May 2026 13:21:51 -0700 Rosen Penev <[email protected]> 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

Quoted 20 lines
Assisted-by Opencode:Big-pickle
---
 drivers/block/rbd.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/block/rbd.c +1 -1
index 94709466ad19..b4ba51db9a28 100644
@@ -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;
#3 lore raw
Quoted 8 lines · David Laight
Quoted 4 lines · On Thu, 28 May 2026 13:21:51 -0700 Rosen Penev <[email protected]> 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.

Quoted 4 lines

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

Quoted 25 lines

-- David

Quoted 20 lines
Assisted-by Opencode:Big-pickle
---
  drivers/block/rbd.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
drivers/block/rbd.c +1 -1
index 94709466ad19..b4ba51db9a28 100644
@@ -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;