drivers/staging/vme_user/vme.c | 2 +- drivers/staging/vme_user/vme.h | 4 ++-- drivers/staging/vme_user/vme_bridge.h | 2 +- drivers/staging/vme_user/vme_fake.c | 2 +- drivers/staging/vme_user/vme_tsi148.c | 4 ++-- drivers/staging/vme_user/vme_user.c | 2 +- 6 files changed, 8 insertions(+), 8 deletions(-)
Change the type used for VME slot numbers from int to u32 throughout vme
driver. This modification more accurately represents the nature of slot
numbers which are always non-negative.
The changes include
- Updating variable declarations
- Modifying function signatures and return types
This change imporves type safety, prevents potential issues with sign conversion.
Signed-off-by: Riyan Dhiman <riyandhiman14@gmail.com>
---
drivers/staging/vme_user/vme.c | 2 +-
drivers/staging/vme_user/vme.h | 4 ++--
drivers/staging/vme_user/vme_bridge.h | 2 +-
drivers/staging/vme_user/vme_fake.c | 2 +-
drivers/staging/vme_user/vme_tsi148.c | 4 ++--
drivers/staging/vme_user/vme_user.c | 2 +-
6 files changed, 8 insertions(+), 8 deletions(-)
diff --git a/drivers/staging/vme_user/vme.c b/drivers/staging/vme_user/vme.c
index 42304c9f83a2..aa3be2d7248d 100644
--- a/drivers/staging/vme_user/vme.c
+++ b/drivers/staging/vme_user/vme.c
@@ -1696,7 +1696,7 @@ EXPORT_SYMBOL(vme_lm_free);
* or the function is not supported. Hardware specific errors may also
* be returned.
*/
-int vme_slot_num(struct vme_dev *vdev)
+u32 vme_slot_num(struct vme_dev *vdev)
{
struct vme_bridge *bridge;
diff --git a/drivers/staging/vme_user/vme.h b/drivers/staging/vme_user/vme.h
index 7753e736f9fd..66388767b6c7 100644
--- a/drivers/staging/vme_user/vme.h
+++ b/drivers/staging/vme_user/vme.h
@@ -102,7 +102,7 @@ extern const struct bus_type vme_bus_type;
* @bridge_list: List of devices (per bridge)
*/
struct vme_dev {
- int num;
+ u32 num;
struct vme_bridge *bridge;
struct device dev;
struct list_head drv_list;
@@ -179,7 +179,7 @@ int vme_lm_attach(struct vme_resource *, int, void (*callback)(void *), void *);
int vme_lm_detach(struct vme_resource *, int);
void vme_lm_free(struct vme_resource *);
-int vme_slot_num(struct vme_dev *);
+u32 vme_slot_num(struct vme_dev *);
int vme_bus_num(struct vme_dev *);
int vme_register_driver(struct vme_driver *, unsigned int);
diff --git a/drivers/staging/vme_user/vme_bridge.h b/drivers/staging/vme_user/vme_bridge.h
index 9bdc41bb6602..6778447eadfb 100644
--- a/drivers/staging/vme_user/vme_bridge.h
+++ b/drivers/staging/vme_user/vme_bridge.h
@@ -160,7 +160,7 @@ struct vme_bridge {
int (*lm_detach)(struct vme_lm_resource *, int);
/* CR/CSR space functions */
- int (*slot_get)(struct vme_bridge *);
+ u32 (*slot_get)(struct vme_bridge *);
/* Bridge parent interface */
void *(*alloc_consistent)(struct device *dev, size_t size, dma_addr_t *dma);
diff --git a/drivers/staging/vme_user/vme_fake.c b/drivers/staging/vme_user/vme_fake.c
index 7f84d1c86f29..81601bfa4155 100644
--- a/drivers/staging/vme_user/vme_fake.c
+++ b/drivers/staging/vme_user/vme_fake.c
@@ -987,7 +987,7 @@ static int fake_lm_detach(struct vme_lm_resource *lm, int monitor)
/*
* Determine Geographical Addressing
*/
-static int fake_slot_get(struct vme_bridge *fake_bridge)
+static u32 fake_slot_get(struct vme_bridge *fake_bridge)
{
return geoid;
}
diff --git a/drivers/staging/vme_user/vme_tsi148.c b/drivers/staging/vme_user/vme_tsi148.c
index d81be8e4ceba..65237fb12466 100644
--- a/drivers/staging/vme_user/vme_tsi148.c
+++ b/drivers/staging/vme_user/vme_tsi148.c
@@ -2109,7 +2109,7 @@ static int tsi148_lm_detach(struct vme_lm_resource *lm, int monitor)
/*
* Determine Geographical Addressing
*/
-static int tsi148_slot_get(struct vme_bridge *tsi148_bridge)
+static u32 tsi148_slot_get(struct vme_bridge *tsi148_bridge)
{
u32 slot = 0;
struct tsi148_driver *bridge;
@@ -2123,7 +2123,7 @@ static int tsi148_slot_get(struct vme_bridge *tsi148_bridge)
slot = geoid;
}
- return (int)slot;
+ return slot;
}
static void *tsi148_alloc_consistent(struct device *parent, size_t size,
diff --git a/drivers/staging/vme_user/vme_user.c b/drivers/staging/vme_user/vme_user.c
index 5829a4141561..63f8efc19324 100644
--- a/drivers/staging/vme_user/vme_user.c
+++ b/drivers/staging/vme_user/vme_user.c
@@ -506,7 +506,7 @@ static int vme_user_match(struct vme_dev *vdev)
int i;
int cur_bus = vme_bus_num(vdev);
- int cur_slot = vme_slot_num(vdev);
+ u32 cur_slot = vme_slot_num(vdev);
for (i = 0; i < bus_num; i++)
if ((cur_bus == bus[i]) && (cur_slot == vdev->num))
--
2.46.0
On Sun, Aug 25, 2024 at 12:59:55PM +0530, Riyan Dhiman wrote:
> Change the type used for VME slot numbers from int to u32 throughout vme
> driver. This modification more accurately represents the nature of slot
> numbers which are always non-negative.
>
> The changes include
> - Updating variable declarations
> - Modifying function signatures and return types
>
> This change imporves type safety, prevents potential issues with sign conversion.
How type promotion works is that if we have if (a < b) { we first cast
everything to int. Then we look at the types of a and b and if one of them has
more than 31 positive bits, which ever has the most positive bits then we cast
both sides to that. The danger is that a negative value will be cast to a high
unsigned value.
In a way you could look at it like the unsigned types are what is making the
code more dangerous. If we didn't have unsigned types, nothing would change the
negatives into unsigned values. Sure we'd have to always check for negatives,
but you'd just get used to that and do it. This is how high level languages
like python work. They don't have any kind of nonsense about if you're
comparing a define and a number -5 and the define is defined as another define
and you have to dig through five different header files and then the define
eventually becomes a sizeof() and so that means -5 is now 18446744073709551611.
In python -5 is just -5.
Of course, there is a place for unsigned types in C but it's so subtle and
complicated to explain. I think people wish that there was a way to make C
safer when there really isn't. There is no easy answer like just declare
everything as u32. It's a false hope.
Here is a blog with more ranting.
https://staticthinking.wordpress.com/2023/07/25/wsign-compare-is-garbage/
regards,
dan carpenter
This patch introduces bugs. I wrote a blog entry about how to choose data types. https://staticthinking.wordpress.com/2022/06/01/unsigned-int-i-is-stupid/ regards, dan carpenter
On Sun, Aug 25, 2024 at 12:59:55PM +0530, Riyan Dhiman wrote: > Change the type used for VME slot numbers from int to u32 throughout vme > driver. This modification more accurately represents the nature of slot > numbers which are always non-negative. > > The changes include > - Updating variable declarations > - Modifying function signatures and return types > > This change imporves type safety, prevents potential issues with sign conversion. Signed integer may be used to encode both non-negative valid values and negative error code. Are you sure none of the changed functions ever return a negative error code? Best regards, Nam
© 2016 - 2025 Red Hat, Inc.