Skip to contentSkip to conversation

usbip: tools: Fix read_usb_vudc_device() error path handling

PATCH Superseded GwanYeong Kim 3 participants 10 messages Oct 15, 2019 – Oct 21, 2019

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

PATCH 1/1 usbip: tools: Fix read_usb_vudc_device() error path handling

cannot be less than 0 - fread() returns 0 on error.

---
 tools/usb/usbip/libsrc/usbip_device_driver.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/usb/usbip/libsrc/usbip_device_driver.c +1 -1
index 051d7d3f443b..49760b98aabc 100644
@@ -79,7 +79,7 @@ int read_usb_vudc_device(struct udev_device *sdev, struct usbip_usb_device *dev)
 	if (!fd)
 		return -1;
 	ret = fread((char *) &descr, sizeof(descr), 1, fd);
-	if (ret < 0)
+	if (ret != sizeof(descr))
 		goto err;
 	fclose(fd);
 
Signature · 1 line
2.17.1
maintainer
#1 lore raw
Quoted 12 lines · GwanYeong Kim

cannot be less than 0 - fread() returns 0 on error.

---
  tools/usb/usbip/libsrc/usbip_device_driver.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
tools/usb/usbip/libsrc/usbip_device_driver.c +0 -0
index 051d7d3f443b..49760b98aabc 100644

tools/usb/usbip/libsrc/usbip_device_driver.c int read_usb_vudc_device(struct udev_device *sdev, struct usbip_usb_device *dev) · line 82

Quoted 4 lines
tools/usb/usbip/libsrc/usbip_device_driver.c +0 -1
@@ -79,7 +79,7 @@ int read_usb_vudc_device(struct udev_device *sdev, struct usbip_usb_device *dev)
  	if (!fd)
  		return -1;
  	ret = fread((char *) &descr, sizeof(descr), 1, fd);
-	if (ret < 0) > +	if (ret != sizeof(descr))

Are you sure this check is correct? fread() returns the number of elements read, # elements = 1 in this case.

fread() returns 0 when size or # of elements is 0 and in other error cases it will return < # of elements. I would think you want to check ret != 1 here.

thanks,
-- Shuah

#2 lore raw
Quoted 8 lines · On Tue, 15 Oct 2019 17:14:32 -0600 shuah <[email protected]> wrote:
Quoted 7 lines · GwanYeong Kim

cannot be less than 0 - fread() returns 0 on error.

---
  tools/usb/usbip/libsrc/usbip_device_driver.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 12 lines

Review

Quoted 9 lines
(quoted diff) +0 -0

b/tools/usb/usbip/libsrc/usbip_device_driver.c index 051d7d3f443b..49760b98aabc 100644 --- a/tools/usb/usbip/libsrc/usbip_device_driver.c +++ b/tools/usb/usbip/libsrc/usbip_device_driver.c @@ -79,7 +79,7 @@ int read_usb_vudc_device(struct udev_device *sdev, struct usbip_usb_device *dev) if (!fd) return -1;
ret = fread((char *) &descr, sizeof(descr), 1, fd);

(quoted diff) +0 -1
-	if (ret < 0) > +	if (ret != sizeof(descr))

Are you sure this check is correct? fread() returns the number of elements read, # elements = 1 in this case.

Thank you for pointing this out.
Sorry for my mistake.

Quoted 3 lines

fread() returns 0 when size or # of elements is 0 and in other error cases it will return < # of elements. I would think you want to check ret != 1 here.

You're right.
This should be changed to "ret != 1".

Should I send a new patch?

Regards,
GwanYeong Kim

maintainer
#3 lore raw
Quoted 27 lines · GwanYeong Kim
Quoted 8 lines · On Tue, 15 Oct 2019 17:14:32 -0600 shuah <[email protected]> wrote:
Quoted 7 lines · GwanYeong Kim

cannot be less than 0 - fread() returns 0 on error.

---
  tools/usb/usbip/libsrc/usbip_device_driver.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 12 lines

Review

Quoted 9 lines
(quoted diff) +0 -0

b/tools/usb/usbip/libsrc/usbip_device_driver.c index 051d7d3f443b..49760b98aabc 100644 --- a/tools/usb/usbip/libsrc/usbip_device_driver.c +++ b/tools/usb/usbip/libsrc/usbip_device_driver.c @@ -79,7 +79,7 @@ int read_usb_vudc_device(struct udev_device *sdev, struct usbip_usb_device *dev) if (!fd) return -1;
ret = fread((char *) &descr, sizeof(descr), 1, fd);

(quoted diff) +0 -1
-	if (ret < 0) > +	if (ret != sizeof(descr))

Are you sure this check is correct? fread() returns the number of elements read, # elements = 1 in this case.

Thank you for pointing this out.
Sorry for my mistake.

Quoted 8 lines
Quoted 3 lines

fread() returns 0 when size or # of elements is 0 and in other error cases it will return < # of elements. I would think you want to check ret != 1 here.

You're right.
This should be changed to "ret != 1".

Should I send a new patch?

If you want to have it applied, yes.

thanks,

greg k-h

#4 lore raw

PATCH v2 1/1 usbip: tools: Fix read_usb_vudc_device() error path handling

cannot be less than 0 - fread() returns 0 on error.

---
 tools/usb/usbip/libsrc/usbip_device_driver.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
tools/usb/usbip/libsrc/usbip_device_driver.c +2 -2
index 051d7d3f443b..959bb29d0477 100644
@@ -69,7 +69,7 @@ int read_usb_vudc_device(struct udev_device *sdev, struct usbip_usb_device *dev)
 	FILE *fd = NULL;
 	struct udev_device *plat;
 	const char *speed;
-	int ret = 0;
+	size_t ret = 0;
 
 	plat = udev_device_get_parent(sdev);
 	path = udev_device_get_syspath(plat);
@@ -79,7 +79,7 @@ int read_usb_vudc_device(struct udev_device *sdev, struct usbip_usb_device *dev)
 	if (!fd)
 		return -1;
 	ret = fread((char *) &descr, sizeof(descr), 1, fd);
-	if (ret < 0)
+	if (ret != 1)
 		goto err;
 	fclose(fd);
 
Signature · 1 line
2.17.1
maintainer
#5 lore raw
Quoted 2 lines · GwanYeong Kim

cannot be less than 0 - fread() returns 0 on error.

This isn't really accurate right. fread() doesn't always return 0 in error. It could return < number of elements and set errno.

Please make changes to reflect that.

Quoted 10 lines
---
  tools/usb/usbip/libsrc/usbip_device_driver.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
tools/usb/usbip/libsrc/usbip_device_driver.c +0 -0
index 051d7d3f443b..959bb29d0477 100644

tools/usb/usbip/libsrc/usbip_device_driver.c int read_usb_vudc_device(struct udev_device *sdev, struct usbip_usb_device *dev) · line 72

Quoted 5 lines
tools/usb/usbip/libsrc/usbip_device_driver.c +1 -1
@@ -69,7 +69,7 @@ int read_usb_vudc_device(struct udev_device *sdev, struct usbip_usb_device *dev)
  	FILE *fd = NULL;
  	struct udev_device *plat;
  	const char *speed;
-	int ret = 0;
+	size_t ret = 0;

You don't need to initialize this.

tools/usb/usbip/libsrc/usbip_device_driver.c int read_usb_vudc_device(struct udev_device *sdev, struct usbip_usb_device *dev) · line 82

Quoted 9 lines
tools/usb/usbip/libsrc/usbip_device_driver.c +1 -1
  	plat = udev_device_get_parent(sdev);
  	path = udev_device_get_syspath(plat);
@@ -79,7 +79,7 @@ int read_usb_vudc_device(struct udev_device *sdev, struct usbip_usb_device *dev)
  	if (!fd)
  		return -1;
  	ret = fread((char *) &descr, sizeof(descr), 1, fd);
-	if (ret < 0)
+	if (ret != 1)

Why not print error message?

tools/usb/usbip/libsrc/usbip_device_driver.c int read_usb_vudc_device(struct udev_device *sdev, struct usbip_usb_device *dev) · line 85

Quoted 4 lines
tools/usb/usbip/libsrc/usbip_device_driver.c +0 -0
  		goto err;
  	fclose(fd);
 

thanks,
-- Shuah

#6 lore raw
Quoted 9 lines · On Wed, 16 Oct 2019 20:33:39 -0600 shuah <[email protected]> wrote:
Quoted 2 lines · GwanYeong Kim

cannot be less than 0 - fread() returns 0 on error.

This isn't really accurate right. fread() doesn't always return 0 in error. It could return < number of elements and set errno.

Please make changes to reflect that.

Will reflect this.

Quoted 6 lines
Quoted 6 lines
---
  tools/usb/usbip/libsrc/usbip_device_driver.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
Quoted 12 lines

tools/usb/usbip/libsrc/usbip_device_driver.c int read_usb_vudc_device(struct udev_device *sdev, struct usbip_usb_device *dev) · line 72

Quoted 10 lines
(quoted diff) +0 -0

b/tools/usb/usbip/libsrc/usbip_device_driver.c index 051d7d3f443b..959bb29d0477 100644 --- a/tools/usb/usbip/libsrc/usbip_device_driver.c +++ b/tools/usb/usbip/libsrc/usbip_device_driver.c @@ -69,7 +69,7 @@ int read_usb_vudc_device(struct udev_device *sdev, struct usbip_usb_device *dev) FILE *fd = NULL; struct udev_device *plat;
const char *speed;

tools/usb/usbip/libsrc/usbip_device_driver.c +1 -1
-	int ret = 0;
+	size_t ret = 0;

You don't need to initialize this.

Exactly, because "ret" variable receives a "fread()" return value,

Quoted 12 lines

tools/usb/usbip/libsrc/usbip_device_driver.c int read_usb_vudc_device(struct udev_device · line 82

Quoted 10 lines
  
  	plat = udev_device_get_parent(sdev);
  	path = udev_device_get_syspath(plat);
tools/usb/usbip/libsrc/usbip_device_driver.c +1 -1
@@ -79,7 +79,7 @@ int read_usb_vudc_device(struct udev_device
 *sdev, struct usbip_usb_device *dev) if (!fd)
  		return -1;
  	ret = fread((char *) &descr, sizeof(descr), 1, fd);
-	if (ret < 0)
+	if (ret != 1)

Why not print error message?

Sorry, I'll add a message.

How about this?

err("Cannot read vudc device descr file");

Quoted 8 lines

tools/usb/usbip/libsrc/usbip_device_driver.c int read_usb_vudc_device(struct udev_device *sdev, struct usbip_usb_device *dev) · line 85

Quoted 5 lines
tools/usb/usbip/libsrc/usbip_device_driver.c +0 -0
  		goto err;
  	fclose(fd);
 

thanks,
-- Shuah

maintainer
#7 lore raw
Quoted 50 lines · GwanYeong Kim
Quoted 9 lines · On Wed, 16 Oct 2019 20:33:39 -0600 shuah <[email protected]> wrote:
Quoted 2 lines · GwanYeong Kim

cannot be less than 0 - fread() returns 0 on error.

This isn't really accurate right. fread() doesn't always return 0 in error. It could return < number of elements and set errno.

Please make changes to reflect that.

Will reflect this.

Quoted 6 lines
Quoted 6 lines
---
   tools/usb/usbip/libsrc/usbip_device_driver.c | 4 ++--
   1 file changed, 2 insertions(+), 2 deletions(-)
Quoted 12 lines

tools/usb/usbip/libsrc/usbip_device_driver.c int read_usb_vudc_device(struct udev_device *sdev, struct usbip_usb_device *dev) · line 72

Quoted 10 lines
(quoted diff) +0 -0

b/tools/usb/usbip/libsrc/usbip_device_driver.c index 051d7d3f443b..959bb29d0477 100644 --- a/tools/usb/usbip/libsrc/usbip_device_driver.c +++ b/tools/usb/usbip/libsrc/usbip_device_driver.c @@ -69,7 +69,7 @@ int read_usb_vudc_device(struct udev_device *sdev, struct

usbip_usb_device *dev) FILE *fd = NULL; struct udev_device *plat;
   	const char *speed;
tools/usb/usbip/libsrc/usbip_device_driver.c +1 -1
-	int ret = 0;
+	size_t ret = 0;

You don't need to initialize this.

Exactly, because "ret" variable receives a "fread()" return value,

Quoted 12 lines

tools/usb/usbip/libsrc/usbip_device_driver.c int read_usb_vudc_device(struct udev_device · line 82

Quoted 10 lines
   
   	plat = udev_device_get_parent(sdev);
   	path = udev_device_get_syspath(plat);
tools/usb/usbip/libsrc/usbip_device_driver.c +1 -1
@@ -79,7 +79,7 @@ int read_usb_vudc_device(struct udev_device
 *sdev, struct usbip_usb_device *dev) if (!fd)
   		return -1;
   	ret = fread((char *) &descr, sizeof(descr), 1, fd);
-	if (ret < 0)
+	if (ret != 1)

Why not print error message?

Quoted 5 lines

Sorry, I'll add a message.

How about this?

err("Cannot read vudc device descr file");

Using strerror() with errno gives you more information. Add that to them essage you proposed.

thanks,
-- Shuah

#8 lore raw

PATCH v3 1/1 usbip: tools: Fix read_usb_vudc_device() error path handling

This isn't really accurate right. fread() doesn't always return 0 in error. It could return < number of elements and set errno.

---
 tools/usb/usbip/libsrc/usbip_device_driver.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
tools/usb/usbip/libsrc/usbip_device_driver.c +4 -2
index 051d7d3f443b..927a151fa9aa 100644
@@ -69,7 +69,7 @@ int read_usb_vudc_device(struct udev_device *sdev, struct usbip_usb_device *dev)
 	FILE *fd = NULL;
 	struct udev_device *plat;
 	const char *speed;
-	int ret = 0;
+	size_t ret;
 
 	plat = udev_device_get_parent(sdev);
 	path = udev_device_get_syspath(plat);
@@ -79,8 +79,10 @@ int read_usb_vudc_device(struct udev_device *sdev, struct usbip_usb_device *dev)
 	if (!fd)
 		return -1;
 	ret = fread((char *) &descr, sizeof(descr), 1, fd);
-	if (ret < 0)
+	if (ret != 1) {
+		err("Cannot read vudc device descr file: %s", strerror(errno));
 		goto err;
+	}
 	fclose(fd);
 
 	copy_descr_attr(dev, &descr, bDeviceClass);
Signature · 1 line
2.17.1
maintainer Acked-by Shuah Khan on v3 1/1
#9 lore raw
Quoted 25 lines · GwanYeong Kim

This isn't really accurate right. fread() doesn't always return 0 in error. It could return < number of elements and set errno.

---
  tools/usb/usbip/libsrc/usbip_device_driver.c | 6 ++++--
  1 file changed, 4 insertions(+), 2 deletions(-)
tools/usb/usbip/libsrc/usbip_device_driver.c +1 -1
index 051d7d3f443b..927a151fa9aa 100644
@@ -69,7 +69,7 @@ int read_usb_vudc_device(struct udev_device *sdev, struct usbip_usb_device *dev)
  	FILE *fd = NULL;
  	struct udev_device *plat;
  	const char *speed;
-	int ret = 0;
+	size_t ret;
 
  	plat = udev_device_get_parent(sdev);
  	path = udev_device_get_syspath(plat);
@@ -79,8 +79,10 @@ int read_usb_vudc_device(struct udev_device *sdev, struct usbip_usb_device *dev)
  	if (!fd)

tools/usb/usbip/libsrc/usbip_device_driver.c int read_usb_vudc_device(struct udev_device *sdev, struct usbip_usb_device *dev) · line 88

Quoted 10 lines
tools/usb/usbip/libsrc/usbip_device_driver.c +3 -1
  		return -1;
  	ret = fread((char *) &descr, sizeof(descr), 1, fd);
-	if (ret < 0)
+	if (ret != 1) {
+		err("Cannot read vudc device descr file: %s", strerror(errno));
  		goto err;
+	}
  	fclose(fd);
 
  	copy_descr_attr(dev, &descr, bDeviceClass);

Looks good.

thanks,
-- Shuah