Skip to contentSkip to conversation

mtd: mtdchar.c silence sparse warning

PATCH Superseded Harvey Harrison 3 participants 3 messages May 15, 2008 Memory technology devices (MTD)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 mtd: mtdchar.c silence sparse warning

The copy_to_user was casting away the address space to get the offset of the length member. Use offsetof() instead and add it to the void __user *argp.

drivers/mtd/mtdchar.c:527:23: warning: cast removes address space of expression drivers/mtd/mtdchar.c:527:23: warning: incorrect type in argument 1 (different address spaces)
drivers/mtd/mtdchar.c:527:23: expected void [noderef] <asn:1>*to
drivers/mtd/mtdchar.c:527:23: got unsigned int *<noident>

---
 drivers/mtd/mtdchar.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
drivers/mtd/mtdchar.c +1 -1
index 5d3ac51..3522d4a 100644
@@ -524,7 +524,7 @@ static int mtd_ioctl(struct inode *inode, struct file *file,
 		if (ops.oobretlen > 0xFFFFFFFFU)
 			ret = -EOVERFLOW;
 		retlen = ops.oobretlen;
-		if (copy_to_user(&((struct mtd_oob_buf *)argp)->length,
+		if (copy_to_user(argp + offsetof(struct mtd_oob_buf, length),
 				 &retlen, sizeof(buf.length)))
 			ret = -EFAULT;
 
Signature · 1 line
1.5.5.1.482.g0f174
#1 lore raw
Quoted 18 lines · On Wed, 14 May 2008 18:22:10 -0700 Harvey Harrison <[email protected]>…

The copy_to_user was casting away the address space to get the offset of the length member. Use offsetof() instead and add it to the void __user *argp.

drivers/mtd/mtdchar.c:527:23: warning: cast removes address space of expression drivers/mtd/mtdchar.c:527:23: warning: incorrect type in argument 1 (different address spaces)
drivers/mtd/mtdchar.c:527:23: expected void [noderef] <asn:1>*to
drivers/mtd/mtdchar.c:527:23: got unsigned int *<noident>

---
 drivers/mtd/mtdchar.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
drivers/mtd/mtdchar.c +0 -0
index 5d3ac51..3522d4a 100644

drivers/mtd/mtdchar.c static int mtd_ioctl(struct inode *inode, struct file *file, · line 529

Quoted 7 lines
drivers/mtd/mtdchar.c +1 -1
@@ -524,7 +524,7 @@ static int mtd_ioctl(struct inode *inode, struct file *file,
 		if (ops.oobretlen > 0xFFFFFFFFU)
 			ret = -EOVERFLOW;
 		retlen = ops.oobretlen;
-		if (copy_to_user(&((struct mtd_oob_buf *)argp)->length,
+		if (copy_to_user(argp + offsetof(struct mtd_oob_buf, length),
 				 &retlen, sizeof(buf.length)))
 			ret = -EFAULT;

Cleaner would be:

	struct mtd_oob_buf __user *user_mtd_oob_buf = argp;

	if (copy_to_user(&user_mtd_oob_buf->length, ...

?

(cleaner, faster and shorter would be put_user(), but that's off-topic).

#2 lore raw
Quoted 10 lines · Andrew Morton

drivers/mtd/mtdchar.c static int mtd_ioctl(struct inode *inode, struct file *file, · line 529

Quoted 4 lines
drivers/mtd/mtdchar.c +1 -1
-		if (copy_to_user(&((struct mtd_oob_buf *)argp)->length,
+		if (copy_to_user(argp + offsetof(struct mtd_oob_buf, length),
 				 &retlen, sizeof(buf.length)))
 			ret = -EFAULT;

Cleaner would be:

	struct mtd_oob_buf __user *user_mtd_oob_buf = argp;

	if (copy_to_user(&user_mtd_oob_buf->length, ...

Yup. BTW, &((type __user *)p)->field) would work just fine too, but yeah, local variable like that would be cleaner.