[PATCH v2 06/19] devfs: Fix incorrect error check for cdev->ops->lseek()

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Cdev->ops->lseek() will either return a negative error code on failure
or requested position on success. In order to properly check for
errors we need to test if the return value is negative, not just that
it's not -1.

Returning ret - cdev->offset, doesn't appear to be correct either,
even if ret is -1, since on failure this will lead us to return (-1 -
cdev->offset). Simplify that part by just returning 'pos', which is
what we'd end up returning on success in original code as well.

Third, make sure to return -ENOSYS, when no .lseek() callback is
provided.

Signed-off-by: Andrey Smirnov <andrew.smirnov@xxxxxxxxx>
---
 fs/devfs.c | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)

diff --git a/fs/devfs.c b/fs/devfs.c
index 81ae2c25a..ae5e6475b 100644
--- a/fs/devfs.c
+++ b/fs/devfs.c
@@ -60,15 +60,19 @@ static int devfs_write(struct device_d *_dev, FILE *f, const void *buf, size_t s
 static loff_t devfs_lseek(struct device_d *_dev, FILE *f, loff_t pos)
 {
 	struct cdev *cdev = f->priv;
-	loff_t ret = -1;
+	loff_t ret;
 
-	if (cdev->ops->lseek)
+	if (cdev->ops->lseek) {
 		ret = cdev->ops->lseek(cdev, pos + cdev->offset);
+		if (ret < 0)
+			return ret;
+	} else {
+		return -ENOSYS;
+	}
 
-	if (ret != -1)
-		f->pos = pos;
+	f->pos = pos;
 
-	return ret - cdev->offset;
+	return pos;
 }
 
 static int devfs_erase(struct device_d *_dev, FILE *f, loff_t count, loff_t offset)
-- 
2.20.1


_______________________________________________
barebox mailing list
barebox@xxxxxxxxxxxxxxxxxxx
http://lists.infradead.org/mailman/listinfo/barebox



[Index of Archives]     [Linux Embedded]     [Linux USB Devel]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]     [XFree86]

  Powered by Linux