Skip to contentSkip to conversation

axis-fifo: use size_t for write size calculations

PATCH Superseded Lucas Jeffrey 2 participants 3 messages Jul 31 – Aug 1 Staging

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 axis-fifo: use size_t for write size calculations

The write length is provided as size_t, but words_to_write and the return value storage used narrower types. Use size_t for the word count and ssize_t for the return value to keep the types consistent with the write interface.

This avoids relying on implicit size limits when handling write sizes.

---
 drivers/staging/axis-fifo/axis-fifo.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/staging/axis-fifo/axis-fifo.c +3 -3
index 3aa2aa870ea9..d2b30b7b6845 100644
@@ -214,9 +214,9 @@ static ssize_t axis_fifo_write(struct file *f, const char __user *buf,
 			       size_t len, loff_t *off)
 {
 	struct axis_fifo *fifo = f->private_data;
-	unsigned int words_to_write;
+	size_t words_to_write;
 	u32 *txbuf;
-	int ret;
+	ssize_t ret;
 
 	words_to_write = len / sizeof(u32);
 
@@ -257,7 +257,7 @@ static ssize_t axis_fifo_write(struct file *f, const char __user *buf,
 		goto end_unlock;
 	}
 
-	for (int i = 0; i < words_to_write; ++i)
+	for (size_t i = 0; i < words_to_write; ++i)
 		iowrite32(txbuf[i], fifo->base_addr + XLLF_TDFD_OFFSET);
 
 	iowrite32(len, fifo->base_addr + XLLF_TLR_OFFSET);
Signature · 1 line
2.43.0
#1 lore raw
Quoted 9 lines · Lucas Jeffrey

The write length is provided as size_t, but words_to_write and the return value storage used narrower types. Use size_t for the word count and ssize_t for the return value to keep the types consistent with the write interface.

This avoids relying on implicit size limits when handling write sizes.

---

This doesn't affect real life because len is capped in rw_verify_area().

The commit message needs to make it clear that this is just a clean up and not a fix. If we pretended that the len value in read/write functions was an range from 0-ULONG_MAX then we would have a ton of integer overflow bugs etc.

regards,
dan carpenter

#2 lore raw

PATCH v2 1/1 staging: axis-fifo: use size_t for write size calculations

In reply to Lucas Jeffrey

The write length is provided as size_t, but words_to_write and the return value storage used narrower types (int and unsigned int). Use size_t for the word count and ssize_t for the return value to keep the types consistent with the write interface.

This is purely a cleanup. The VFS already bounds the write length before the driver is called, so this does not change runtime behavior.

---
v2:
- Clarify that this is a cleanup.
- Mention that VFS already bounds write sizes.

 drivers/staging/axis-fifo/axis-fifo.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/staging/axis-fifo/axis-fifo.c +3 -3
index 3aa2aa870ea9..d2b30b7b6845 100644
@@ -214,9 +214,9 @@ static ssize_t axis_fifo_write(struct file *f, const char __user *buf,
 			       size_t len, loff_t *off)
 {
 	struct axis_fifo *fifo = f->private_data;
-	unsigned int words_to_write;
+	size_t words_to_write;
 	u32 *txbuf;
-	int ret;
+	ssize_t ret;
 
 	words_to_write = len / sizeof(u32);
 
@@ -257,7 +257,7 @@ static ssize_t axis_fifo_write(struct file *f, const char __user *buf,
 		goto end_unlock;
 	}
 
-	for (int i = 0; i < words_to_write; ++i)
+	for (size_t i = 0; i < words_to_write; ++i)
 		iowrite32(txbuf[i], fifo->base_addr + XLLF_TDFD_OFFSET);
 
 	iowrite32(len, fifo->base_addr + XLLF_TLR_OFFSET);
Signature · 1 line
2.43.0