Skip to contentSkip to conversation

usb-storage: Check whether divisor is non-zero before division

PATCH In review Shichao Lai 2 participants 4 messages May 23, 2024

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

PATCH v2 1/1 usb-storage: Check whether divisor is non-zero before division

Since uzonesize may be zero, so judgements for non-zero are nessesary in both place.

Changes since v1:
- Add one more check in alauda_write_lba().
- Move check ahead of loop in alauda_read_data().

---
 drivers/usb/storage/alauda.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/usb/storage/alauda.c +4 -0
index 115f05a6201a..17c73acd3b02 100644
@@ -813,6 +813,8 @@ static int alauda_write_lba(struct us_data *us, u16 lba,
 	unsigned char ecc[3];
 	int i, result;
 	unsigned int uzonesize = MEDIA_INFO(us).uzonesize;
+	if (!uzonesize)
+		return USB_STOR_TRANSPORT_ERROR;
 	unsigned int zonesize = MEDIA_INFO(us).zonesize;
 	unsigned int pagesize = MEDIA_INFO(us).pagesize;
 	unsigned int blocksize = MEDIA_INFO(us).blocksize;
@@ -921,6 +923,8 @@ static int alauda_read_data(struct us_data *us, unsigned long address,
 	unsigned int blocksize = MEDIA_INFO(us).blocksize;
 	unsigned int pagesize = MEDIA_INFO(us).pagesize;
 	unsigned int uzonesize = MEDIA_INFO(us).uzonesize;
+	if (!uzonesize)
+		return USB_STOR_TRANSPORT_ERROR;
 	struct scatterlist *sg;
 	int result;
 
Signature · 1 line
2.34.1
maintainer
#1 lore raw
Quoted 6 lines · Shichao Lai

Since uzonesize may be zero, so judgements for non-zero are nessesary in both place.

Changes since v1:
- Add one more check in alauda_write_lba().
- Move check ahead of loop in alauda_read_data().

Nit, this changes list should go below the --- line, as the documentation asks for.

Quoted 13 lines
---
 drivers/usb/storage/alauda.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/usb/storage/alauda.c +0 -0
index 115f05a6201a..17c73acd3b02 100644

drivers/usb/storage/alauda.c static int alauda_write_lba(struct us_data *us, u16 lba, · line 817

Quoted 5 lines
drivers/usb/storage/alauda.c +2 -0
@@ -813,6 +813,8 @@ static int alauda_write_lba(struct us_data *us, u16 lba,
 	unsigned char ecc[3];
 	int i, result;
 	unsigned int uzonesize = MEDIA_INFO(us).uzonesize;
+	if (!uzonesize)
+		return USB_STOR_TRANSPORT_ERROR;

Check after the list of variables please, not in the middle of them. I think checkpatch will complain about this, right?

drivers/usb/storage/alauda.c static int alauda_read_data(struct us_data *us, unsigned long address, · line 927

Quoted 9 lines
drivers/usb/storage/alauda.c +2 -0
 	unsigned int zonesize = MEDIA_INFO(us).zonesize;
 	unsigned int pagesize = MEDIA_INFO(us).pagesize;
 	unsigned int blocksize = MEDIA_INFO(us).blocksize;
@@ -921,6 +923,8 @@ static int alauda_read_data(struct us_data *us, unsigned long address,
 	unsigned int blocksize = MEDIA_INFO(us).blocksize;
 	unsigned int pagesize = MEDIA_INFO(us).pagesize;
 	unsigned int uzonesize = MEDIA_INFO(us).uzonesize;
+	if (!uzonesize)
+		return USB_STOR_TRANSPORT_ERROR;

Same here, at the end of the variable list please.

thanks,

greg k-h

#2 lore raw
Quoted 11 lines · Greg KH
Quoted 6 lines · Shichao Lai

Since uzonesize may be zero, so judgements for non-zero are necessary in both place.

Changes since v1:
- Add one more check in alauda_write_lba().
- Move check ahead of loop in alauda_read_data().

Nit, this changes list should go below the --- line, as the documentation asks for.

Sorry for my inexperience. I have read the document and found some examples but I am still a little confused about this.
I guess this is what you mean?

Since uzonesize may be zero ... (context)

---
Changes since v1:
- Add one more check in alauda_write_lba().
- Move check ahead of loop in alauda_read_data().

 drivers/usb/storage/alauda.c | 4 ++++
 1 file changed, 4 insertions(+)

...

Quoted 2 lines

Check after the list of variables please, not in the middle of them. I think checkpatch will complain about this, right?

In fact this script doesn't warn about these problems, but I will adjust my code style later.
But the check in alauda_write_lba() is due to some variable like lba_offset and zone will perform modulo and divide operations, which may throw divide errors when uzonesize is 0.
So I think I prefer to adjust the order of the variable list later. Changes like this.

```c
unsigned int uzonesize = MEDIA_INFO(us).uzonesize;
unsigned int zonesize = MEDIA_INFO(us).zonesize;
unsigned int pagesize = MEDIA_INFO(us).pagesize;
unsigned int blocksize = MEDIA_INFO(us).blocksize;
unsigned int new_pba_offset;
if (!uzonesize)
    return USB_STOR_TRANSPORT_ERROR;
unsigned int lba_offset = lba % uzonesize;
unsigned int zone = lba / uzonesize;
```

If it's ok, I will post the patch v3 soon.

maintainer
#3 lore raw
Quoted 19 lines · shichao lai
Quoted 11 lines · Greg KH
Quoted 6 lines · Shichao Lai

Since uzonesize may be zero, so judgements for non-zero are necessary in both place.

Changes since v1:
- Add one more check in alauda_write_lba().
- Move check ahead of loop in alauda_read_data().

Nit, this changes list should go below the --- line, as the documentation asks for.

Sorry for my inexperience. I have read the document and found some examples but I am still a little confused about this.
I guess this is what you mean?

Since uzonesize may be zero ... (context)

Quoted 7 lines

---
Changes since v1:
- Add one more check in alauda_write_lba().
- Move check ahead of loop in alauda_read_data().

Yes.

Quoted 28 lines
 drivers/usb/storage/alauda.c | 4 ++++
 1 file changed, 4 insertions(+)

...

Quoted 2 lines

Check after the list of variables please, not in the middle of them. I think checkpatch will complain about this, right?

In fact this script doesn't warn about these problems, but I will adjust my code style later.
But the check in alauda_write_lba() is due to some variable like lba_offset and zone will perform modulo and divide operations, which may throw divide errors when uzonesize is 0.
So I think I prefer to adjust the order of the variable list later. Changes like this.

```c
unsigned int uzonesize = MEDIA_INFO(us).uzonesize;
unsigned int zonesize = MEDIA_INFO(us).zonesize;
unsigned int pagesize = MEDIA_INFO(us).pagesize;
unsigned int blocksize = MEDIA_INFO(us).blocksize;
unsigned int new_pba_offset;
if (!uzonesize)
    return USB_STOR_TRANSPORT_ERROR;
unsigned int lba_offset = lba % uzonesize;
unsigned int zone = lba / uzonesize;
```
Quoted 1 line

If it's ok, I will post the patch v3 soon.

That works, thanks!

greg k-h