Patchwork [09/15] usb-linux.c: fix warning with _FORTIFY_SOURCE

login
register
mail settings
Submitter Kirill A. Shutemov
Date Jan. 2, 2010, 3:45 a.m.
Message ID <1262403933-26881-9-git-send-email-kirill@shutemov.name>
Download mbox | patch
Permalink /patch/41999/
State New
Headers show

Comments

Kirill A. Shutemov - Jan. 2, 2010, 3:45 a.m.
CC    usb-linux.o
cc1: warnings being treated as errors
usb-linux.c: In function 'usb_host_read_file':
usb-linux.c:1204: error: ignoring return value of 'fgets', declared with attribute warn_unused_result
make: *** [usb-linux.o] Error 1

Signed-off-by: Kirill A. Shutemov <kirill@shutemov.name>
---
 usb-linux.c |    8 ++++++--
 1 files changed, 6 insertions(+), 2 deletions(-)
Kirill A. Shutemov - Jan. 5, 2010, 4:07 p.m.
On Tue, Jan 5, 2010 at 4:42 PM, Juan Quintela <quintela@trasno.org> wrote:
> "Kirill A. Shutemov" <kirill@shutemov.name> wrote:
>>   CC    usb-linux.o
>> cc1: warnings being treated as errors
>> usb-linux.c: In function 'usb_host_read_file':
>> usb-linux.c:1204: error: ignoring return value of 'fgets', declared with attribute warn_unused_result
>> make: *** [usb-linux.o] Error 1
>>
>> Signed-off-by: Kirill A. Shutemov <kirill@shutemov.name>
>> ---
>>  usb-linux.c |    8 ++++++--
>>  1 files changed, 6 insertions(+), 2 deletions(-)
>>
>> diff --git a/usb-linux.c b/usb-linux.c
>> index 88728e9..8673474 100644
>> --- a/usb-linux.c
>> +++ b/usb-linux.c
>> @@ -1201,9 +1201,13 @@ static int usb_host_read_file(char *line, size_t line_size, const char *device_f
>>               device_file);
>>      f = fopen(filename, "r");
>>      if (f) {
>> -        fgets(line, line_size, f);
>> +        if (fgets(line, line_size, f)) {
>> +            ret = 1;
>> +        } else {
>> +            ret = 0;
>> +        }
>> +
> This if is equivalent to:
>
> ret = !!fgets(line, line_size, f);
>
> No need for the if at all :)

It's not very readable.
Probably better to use something like:

ret = (fgets(line, line_size, f) != NULL);
Michael S. Tsirkin - Jan. 5, 2010, 4:36 p.m.
On Tue, Jan 05, 2010 at 06:07:35PM +0200, Kirill A. Shutemov wrote:
> On Tue, Jan 5, 2010 at 4:42 PM, Juan Quintela <quintela@trasno.org> wrote:
> > "Kirill A. Shutemov" <kirill@shutemov.name> wrote:
> >>   CC    usb-linux.o
> >> cc1: warnings being treated as errors
> >> usb-linux.c: In function 'usb_host_read_file':
> >> usb-linux.c:1204: error: ignoring return value of 'fgets', declared with attribute warn_unused_result
> >> make: *** [usb-linux.o] Error 1
> >>
> >> Signed-off-by: Kirill A. Shutemov <kirill@shutemov.name>
> >> ---
> >>  usb-linux.c |    8 ++++++--
> >>  1 files changed, 6 insertions(+), 2 deletions(-)
> >>
> >> diff --git a/usb-linux.c b/usb-linux.c
> >> index 88728e9..8673474 100644
> >> --- a/usb-linux.c
> >> +++ b/usb-linux.c
> >> @@ -1201,9 +1201,13 @@ static int usb_host_read_file(char *line, size_t line_size, const char *device_f
> >>               device_file);
> >>      f = fopen(filename, "r");
> >>      if (f) {
> >> -        fgets(line, line_size, f);
> >> +        if (fgets(line, line_size, f)) {
> >> +            ret = 1;
> >> +        } else {
> >> +            ret = 0;
> >> +        }
> >> +
> > This if is equivalent to:
> >
> > ret = !!fgets(line, line_size, f);
> >
> > No need for the if at all :)
> 
> It's not very readable.
> Probably better to use something like:
> 
> ret = (fgets(line, line_size, f) != NULL);
> 

Might be matter of taste. E.g. I think !! is more readable than != NULL.
And () around != are not needed.  It's better to make code brief IMO,
a lof of boilerplate hides bugs.

Nothing to get hung about though.

Patch

diff --git a/usb-linux.c b/usb-linux.c
index 88728e9..8673474 100644
--- a/usb-linux.c
+++ b/usb-linux.c
@@ -1201,9 +1201,13 @@  static int usb_host_read_file(char *line, size_t line_size, const char *device_f
              device_file);
     f = fopen(filename, "r");
     if (f) {
-        fgets(line, line_size, f);
+        if (fgets(line, line_size, f)) {
+            ret = 1;
+        } else {
+            ret = 0;
+        }
+
         fclose(f);
-        ret = 1;
 #if 0
     } else {
         if (mon)