Skip to contentSkip to conversation

staging: vc04_services: shut up out-of-range warning

PATCH In review Arnd Bergmann 3 participants 4 messages Sep 27, 2021

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

PATCH 1/1 staging: vc04_services: shut up out-of-range warning

From: Arnd Bergmann <[email protected]>

The comparison against SIZE_MAX produces a harmless warning on 64-bit architectures:

drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.c:185:16: error: result of comparison of constant 419244183493398898 with expression of type 'unsigned int' is always false [-Werror,-Wtautological-constant-out-of-range-compare]
if (num_pages > (SIZE_MAX - sizeof(struct pagelist) -
~~~~~~~~~ ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Shut up that warning by adding a cast to a longer type.

Fixes ca641bae6da9 ("staging: vc04_services: prevent integer overflow in create_pagelist()")
---
 drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.c +1 -1
index b25369a13452..967f10b9582a 100644
@@ -182,7 +182,7 @@ create_pagelist(char *buf, char __user *ubuf,
 		offset = (uintptr_t)ubuf & (PAGE_SIZE - 1);
 	num_pages = DIV_ROUND_UP(count + offset, PAGE_SIZE);
 
-	if (num_pages > (SIZE_MAX - sizeof(struct pagelist) -
+	if ((size_t)num_pages > (SIZE_MAX - sizeof(struct pagelist) -
 			 sizeof(struct vchiq_pagelist_info)) /
 			(sizeof(u32) + sizeof(pages[0]) +
 			 sizeof(struct scatterlist)))
Signature · 1 line
2.29.2
#1 lore raw
Quoted 22 lines · Arnd Bergmann

From: Arnd Bergmann <[email protected]>

The comparison against SIZE_MAX produces a harmless warning on 64-bit architectures:

drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.c:185:16: error: result of comparison of constant 419244183493398898 with expression of type 'unsigned int' is always false [-Werror,-Wtautological-constant-out-of-range-compare]
if (num_pages > (SIZE_MAX - sizeof(struct pagelist) -
~~~~~~~~~ ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Shut up that warning by adding a cast to a longer type.

Fixes ca641bae6da9 ("staging: vc04_services: prevent integer overflow in create_pagelist()")
---
 drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.c +0 -0
index b25369a13452..967f10b9582a 100644

drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.c create_pagelist(char *buf, char __user *ubuf, · line 188

Quoted 8 lines
drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.c +1 -1
@@ -182,7 +182,7 @@ create_pagelist(char *buf, char __user *ubuf,
 		offset = (uintptr_t)ubuf & (PAGE_SIZE - 1);
 	num_pages = DIV_ROUND_UP(count + offset, PAGE_SIZE);
 
-	if (num_pages > (SIZE_MAX - sizeof(struct pagelist) -
+	if ((size_t)num_pages > (SIZE_MAX - sizeof(struct pagelist) -
 			 sizeof(struct vchiq_pagelist_info)) /
 			(sizeof(u32) + sizeof(pages[0]) +
 			 sizeof(struct scatterlist)))

The temptation would be to declare "num_pages" as size_t instead of adding this cost. But then something will complain about the "pagelistinfo->num_pages = num_pages;" assignment because "pagelistinfo->num_pages" is a u32.

The next temptation is to change the SIZE_MAX to UINT_MAX. I didn't do that originally because I can't test this and I was trying not to break things... We probably still don't want to break things, but maybe there is someone who is more familiar with this who knows if UINT_MAX is okay?

regards,
dan carpenter

#2 lore raw

Hi Dan,

Quoted 37 lines · Dan Carpenter
Quoted 22 lines · Arnd Bergmann

From: Arnd Bergmann <[email protected]>

The comparison against SIZE_MAX produces a harmless warning on 64-bit architectures:

drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.c:185:16: error: result of comparison of constant 419244183493398898 with expression of type 'unsigned int' is always false [-Werror,-Wtautological-constant-out-of-range-compare]
if (num_pages > (SIZE_MAX - sizeof(struct pagelist) -
~~~~~~~~~ ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Shut up that warning by adding a cast to a longer type.

Fixes ca641bae6da9 ("staging: vc04_services: prevent integer overflow in create_pagelist()")
---
  drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.c +0 -0
index b25369a13452..967f10b9582a 100644

drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.c create_pagelist(char *buf, char __user *ubuf, · line 188

Quoted 8 lines
drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.c +1 -1
@@ -182,7 +182,7 @@ create_pagelist(char *buf, char __user *ubuf,
  		offset = (uintptr_t)ubuf & (PAGE_SIZE - 1);
  	num_pages = DIV_ROUND_UP(count + offset, PAGE_SIZE);
 
-	if (num_pages > (SIZE_MAX - sizeof(struct pagelist) -
+	if ((size_t)num_pages > (SIZE_MAX - sizeof(struct pagelist) -
  			 sizeof(struct vchiq_pagelist_info)) /
  			(sizeof(u32) + sizeof(pages[0]) +
  			 sizeof(struct scatterlist)))

The temptation would be to declare "num_pages" as size_t instead of adding this cost. But then something will complain about the "pagelistinfo->num_pages = num_pages;" assignment because "pagelistinfo->num_pages" is a u32.

Quoted 5 lines

The next temptation is to change the SIZE_MAX to UINT_MAX. I didn't do that originally because I can't test this and I was trying not to break things... We probably still don't want to break things, but maybe there is someone who is more familiar with this who knows if UINT_MAX is okay?

The VPU can't address more than 1GB directly, so UINT_MAX is more than sufficient.

Phil

#3 lore raw
Quoted 15 lines · Phil Elwell
Quoted 14 lines · Dan Carpenter

drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.c create_pagelist(char *buf, char __user *ubuf, · line 188

Quoted 7 lines · Arnd Bergmann

From: Arnd Bergmann <[email protected]>

drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.c +1 -1
-    if (num_pages > (SIZE_MAX - sizeof(struct pagelist) -
+    if ((size_t)num_pages > (SIZE_MAX - sizeof(struct pagelist) -
                      sizeof(struct vchiq_pagelist_info)) /
                     (sizeof(u32) + sizeof(pages[0]) +
                      sizeof(struct scatterlist)))

The temptation would be to declare "num_pages" as size_t instead of adding this cost. But then something will complain about the "pagelistinfo->num_pages = num_pages;" assignment because "pagelistinfo->num_pages" is a u32.

Quoted 7 lines
Quoted 5 lines

The next temptation is to change the SIZE_MAX to UINT_MAX. I didn't do that originally because I can't test this and I was trying not to break things... We probably still don't want to break things, but maybe there is someone who is more familiar with this who knows if UINT_MAX is okay?

The VPU can't address more than 1GB directly, so UINT_MAX is more than sufficient.

Is there a macro that already defines that 1GB size, or maybe an even smaller value that makes sense as an upper bound?

Arnd