2011-03-24 14:27:19

by Sergey Senozhatsky

[permalink] [raw]
Subject: check device_create_file() return code in usb_create_sysfs_intf_files()

Hello,

I just noticed that usb_create_sysfs_intf_files() ignores device_create_file()
return code and sets intf->sysfs_files_created to 1, even if sysfs_add_file_mode()
returned -ENOMEM (or later sysfs_add_one() returned -EEXIST).

Shouldn't we check retval for 0 before setting intf->sysfs_files_created?

---

drivers/usb/core/sysfs.c | 6 ++++--
1 files changed, 4 insertions(+), 2 deletions(-)

diff --git a/drivers/usb/core/sysfs.c b/drivers/usb/core/sysfs.c
index 6781c36..5a02524 100644
--- a/drivers/usb/core/sysfs.c
+++ b/drivers/usb/core/sysfs.c
@@ -856,8 +856,10 @@ int usb_create_sysfs_intf_files(struct usb_interface *intf)
alt->string = usb_cache_string(udev, alt->desc.iInterface);
if (alt->string)
retval = device_create_file(&intf->dev, &dev_attr_interface);
- intf->sysfs_files_created = 1;
- return 0;
+ if (retval == 0)
+ intf->sysfs_files_created = 1;
+
+ return retval;
}

void usb_remove_sysfs_intf_files(struct usb_interface *intf)


2011-03-24 14:42:59

by Michal Nazarewicz

[permalink] [raw]
Subject: Re: check device_create_file() return code in usb_create_sysfs_intf_files()

On Thu, 24 Mar 2011 15:31:16 +0100, Sergey Senozhatsky wrote:
> diff --git a/drivers/usb/core/sysfs.c b/drivers/usb/core/sysfs.c
> index 6781c36..5a02524 100644
> --- a/drivers/usb/core/sysfs.c
> +++ b/drivers/usb/core/sysfs.c
> @@ -856,8 +856,10 @@ int usb_create_sysfs_intf_files(struct
> usb_interface *intf)
> alt->string = usb_cache_string(udev, alt->desc.iInterface);
> if (alt->string)
> retval = device_create_file(&intf->dev, &dev_attr_interface);
> - intf->sysfs_files_created = 1;
> - return 0;
> + if (retval == 0)
> + intf->sysfs_files_created = 1;
> +
> + return retval;
> }

retval may be uninitialised. You need to zero it at the beginning of the
function (or somewhere).

> void usb_remove_sysfs_intf_files(struct usb_interface *intf)
>
> --
> To unsubscribe from this list: send the line "unsubscribe linux-usb" in
> the body of a message to [email protected]
> More majordomo info at http://vger.kernel.org/majordomo-info.html


--
Best regards, _ _
.o. | Liege of Serenely Enlightened Majesty of o' \,=./ `o
..o | Computer Science, Michal "mina86" Nazarewicz (o o)
ooo +-----<email/xmpp: [email protected]>-----ooO--(_)--Ooo--

2011-03-24 14:52:13

by Sergey Senozhatsky

[permalink] [raw]
Subject: Re: check device_create_file() return code in usb_create_sysfs_intf_files()

On (03/24/11 15:42), Michal Nazarewicz wrote:
> >@@ -856,8 +856,10 @@ int usb_create_sysfs_intf_files(struct
> >usb_interface *intf)
> > alt->string = usb_cache_string(udev, alt->desc.iInterface);
> > if (alt->string)
> > retval = device_create_file(&intf->dev, &dev_attr_interface);
> >- intf->sysfs_files_created = 1;
> >- return 0;
> >+ if (retval == 0)
> >+ intf->sysfs_files_created = 1;
> >+
> >+ return retval;
> > }
>
> retval may be uninitialised. You need to zero it at the beginning of the
> function (or somewhere).
>

Oops... Yes, indeed. Thanks.

---

drivers/usb/core/sysfs.c | 8 +++++---
1 files changed, 5 insertions(+), 3 deletions(-)

diff --git a/drivers/usb/core/sysfs.c b/drivers/usb/core/sysfs.c
index 6781c36..d03c630 100644
--- a/drivers/usb/core/sysfs.c
+++ b/drivers/usb/core/sysfs.c
@@ -846,7 +846,7 @@ int usb_create_sysfs_intf_files(struct usb_interface *intf)
{
struct usb_device *udev = interface_to_usbdev(intf);
struct usb_host_interface *alt = intf->cur_altsetting;
- int retval;
+ int retval = -1;

if (intf->sysfs_files_created || intf->unregistering)
return 0;
@@ -856,8 +856,10 @@ int usb_create_sysfs_intf_files(struct usb_interface *intf)
alt->string = usb_cache_string(udev, alt->desc.iInterface);
if (alt->string)
retval = device_create_file(&intf->dev, &dev_attr_interface);
- intf->sysfs_files_created = 1;
- return 0;
+ if (retval == 0)
+ intf->sysfs_files_created = 1;
+
+ return retval;
}

void usb_remove_sysfs_intf_files(struct usb_interface *intf)

2011-03-24 14:55:13

by Alan Stern

[permalink] [raw]
Subject: Re: check device_create_file() return code in usb_create_sysfs_intf_files()

On Thu, 24 Mar 2011, Sergey Senozhatsky wrote:

> Hello,
>
> I just noticed that usb_create_sysfs_intf_files() ignores device_create_file()
> return code and sets intf->sysfs_files_created to 1, even if sysfs_add_file_mode()
> returned -ENOMEM (or later sysfs_add_one() returned -EEXIST).
>
> Shouldn't we check retval for 0 before setting intf->sysfs_files_created?

No. We want this routine to succeed even if the sysfs files can't be
created. The interface string attribute is more or less optional.

It would be okay to add a comment explaining this, so that other people
don't make the same mistake (which has already happened -- you're not
the first).

Alan Stern

2011-03-24 14:59:17

by Michal Nazarewicz

[permalink] [raw]
Subject: Re: check device_create_file() return code in usb_create_sysfs_intf_files()

On Thu, 24 Mar 2011 15:56:06 +0100, Sergey Senozhatsky wrote:
> diff --git a/drivers/usb/core/sysfs.c b/drivers/usb/core/sysfs.c
> index 6781c36..d03c630 100644
> --- a/drivers/usb/core/sysfs.c
> +++ b/drivers/usb/core/sysfs.c
> @@ -846,7 +846,7 @@ int usb_create_sysfs_intf_files(struct usb_interface
> *intf)
> {
> struct usb_device *udev = interface_to_usbdev(intf);
> struct usb_host_interface *alt = intf->cur_altsetting;
> - int retval;
> + int retval = -1;

That should be 0 or some other meaningful value. A literal -1 is definitely
not a good choice. This is just for future since Alan has explained this
patch is not needed.

> if (intf->sysfs_files_created || intf->unregistering)
> return 0;

--
Best regards, _ _
.o. | Liege of Serenely Enlightened Majesty of o' \,=./ `o
..o | Computer Science, Michal "mina86" Nazarewicz (o o)
ooo +-----<email/xmpp: [email protected]>-----ooO--(_)--Ooo--

2011-03-24 15:01:30

by Sergey Senozhatsky

[permalink] [raw]
Subject: Re: check device_create_file() return code in usb_create_sysfs_intf_files()

On (03/24/11 10:55), Alan Stern wrote:
> > I just noticed that usb_create_sysfs_intf_files() ignores device_create_file()
> > return code and sets intf->sysfs_files_created to 1, even if sysfs_add_file_mode()
> > returned -ENOMEM (or later sysfs_add_one() returned -EEXIST).
> >
> > Shouldn't we check retval for 0 before setting intf->sysfs_files_created?
>
> No. We want this routine to succeed even if the sysfs files can't be
> created. The interface string attribute is more or less optional.
>
> It would be okay to add a comment explaining this, so that other people
> don't make the same mistake (which has already happened -- you're not
> the first).
>

Thanks. Sorry for the noise then.
Well, in that case, I guess, `int retval;' can be removed, since it's
unused.


Best,
Sergey


Attachments:
(No filename) (801.00 B)
(No filename) (316.00 B)
Download all attachments