Re: Is it OK to have compile warnings

2013-10-23 Thread Russ Allbery
Gert Wollny  writes:

>> To directly answer your question, since I think everyone so far has told
>> you that you should fix it, this answer from S/O [1]
>> 
> [...]
>> [1]
>> http://stackoverflow.com/questions/3614691/casting-to-void-doesnt-remove-warn-unused-result-error/3615000#3615000

> One should note that the question on S/O was about opening "/dev/null"
> which is unlikely to fail. 

Less unlikely than one might think.  I've seen this fail in several
situations, usually because the program is being run in a partial chroot,
but on one occasion because someone deleted the device.  In the latter
case, /dev/null later became a regular file because some program was
opening it with O_CREAT (aie), and that caused all sorts of truly bizarre
problems.

It's sometimes the case that errors really don't need checking, but more
rarely than people think.  Good software needs error handling mechanisms
for unusual failure conditions.  If you really think that this should
never fail, check the return status with assert().  That way, if the
impossible happens, at least you terminate the process rather than
continuing under false assumptions.

The only exception I've run into for this particular warning in my own
code was in monitoring software that was speaking the IMAP protocol.  It
sent a "tag logout" command to the server before closing the socket, but
it truly, absolutely did not care if that write failed, since the correct
action to take if the write failed was to... close the socket.  Sending
the logout command was just being polite to the server.  In that case, I
used the following construct:

/* Only for clean shutdown, don't care about failure. */
if (socket_write(sd, "tag logout\r\n", 12) < 0) {}
socket_close(sd);

which gcc is happy with.  (The socket_* calls are, on Linux, just macros
that expand to write and close.  They exist for compatibility with
Windows, where different functions have to be used when working with
sockets.)

-- 
Russ Allbery (r...@debian.org)   


-- 
To UNSUBSCRIBE, email to debian-mentors-requ...@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmas...@lists.debian.org
Archive: http://lists.debian.org/87wql4m0b3@windlord.stanford.edu



Re: Is it OK to have compile warnings

2013-10-23 Thread Patrick Baggett
Oh yeah, definitely fix it if at all possible ;)


On Wed, Oct 23, 2013 at 10:59 AM, Gert Wollny  wrote:

> > To directly answer your question, since I think everyone so far has told
> > you that you should fix it, this answer from S/O [1]
> >
> [...]
> > [1]
> >
> http://stackoverflow.com/questions/3614691/casting-to-void-doesnt-remove-warn-unused-result-error/3615000#3615000
>
> One should note that the question on S/O was about opening "/dev/null"
> which is unlikely to fail.
>
> The "write" that was mentioned in the original post is quite a different
> story.
>
> best regards,
> Gert
>
>
>


Re: Is it OK to have compile warnings

2013-10-23 Thread Gert Wollny
> To directly answer your question, since I think everyone so far has told
> you that you should fix it, this answer from S/O [1]
> 
[...]
> [1]
> http://stackoverflow.com/questions/3614691/casting-to-void-doesnt-remove-warn-unused-result-error/3615000#3615000

One should note that the question on S/O was about opening "/dev/null"
which is unlikely to fail. 

The "write" that was mentioned in the original post is quite a different
story.  
 
best regards,
Gert 




signature.asc
Description: This is a digitally signed message part


Re: Is it OK to have compile warnings

2013-10-23 Thread Patrick Baggett
On Tue, Oct 22, 2013 at 8:41 PM, Tong Sun  wrote:

> Hi,
>
> Is it OK to have compile warnings when building a package? If not,
>
> how can I fix the following:
>
> dial.c: In function 'main':
> dial.c:273:8: warning: ignoring return value of 'write', declared with
> attribute warn_unused_result [-Wunused-result]
>write(fd, buf, bufidx);
>
>
To directly answer your question, since I think everyone so far has told
you that you should fix it, this answer from S/O [1]

 #define ignore_result(x) ({ typeof(x) z = x; (void)sizeof z; })

then:

ignore_result( func(...) )


[1]
http://stackoverflow.com/questions/3614691/casting-to-void-doesnt-remove-warn-unused-result-error/3615000#3615000

I got a bunch of such ignoring-return-value warnings.
>
> Thanks
>
>
>
> --
> To UNSUBSCRIBE, email to debian-mentors-requ...@lists.debian.org
> with a subject of "unsubscribe". Trouble? Contact
> listmas...@lists.debian.org
> Archive:
> http://lists.debian.org/1382492499.68644.yahoomail...@web161904.mail.bf1.yahoo.com
>
>


Re: Is it OK to have compile warnings

2013-10-22 Thread Octavio Alvarez

On 22/10/13 18:41, Tong Sun wrote:

Hi,

Is it OK to have compile warnings when building a package?


Although sometimes the compiler is just noisy, I don't like ignoring 
compiler warnings. They may be a hint of an omission, a bug, or in the 
worst case, a security issue.


I prefer to shut the compiler up by fixing the code.


If not,

how can I fix the following:

dial.c: In function 'main':
dial.c:273:8: warning: ignoring return value of 'write', declared with 
attribute warn_unused_result [-Wunused-result]
write(fd, buf, bufidx);

I got a bunch of such ignoring-return-value warnings.


Quoting the specification for write() [1]:

"Upon successful completion, write() [...] shall return the number of 
bytes actually written to the file associated with fildes. This number 
shall never be greater than nbyte. Otherwise, -1 shall be returned and 
errno set to indicate the error."


The compiler warning you are getting means that the code isn't 
validating the write() return value.


If your write fails you will never catch the error and your application 
might crash. Depending on the type of application this can lead to 
security issues.


[1] http://pubs.opengroup.org/onlinepubs/009695399/functions/write.html

Best regards.


--
To UNSUBSCRIBE, email to debian-mentors-requ...@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmas...@lists.debian.org
Archive: http://lists.debian.org/52673e20.4020...@alvarezp.ods.org



Re: Is it OK to have compile warnings

2013-10-22 Thread Paul Wise
On Wed, Oct 23, 2013 at 9:41 AM, Tong Sun wrote:

> Is it OK to have compile warnings when building a package? If not,

It is fairly normal to have compile warnings since GCC gets stricter over time.

> how can I fix the following:
>
> dial.c: In function 'main':
> dial.c:273:8: warning: ignoring return value of 'write', declared with 
> attribute warn_unused_result [-Wunused-result]
>write(fd, buf, bufidx);
>
> I got a bunch of such ignoring-return-value warnings.

Report a bug upstream and ask them to fix it. Bonus points for reading
the C code, changing it to check the return value of write() as
appropriate and send upstream a patch.

-- 
bye,
pabs

http://wiki.debian.org/PaulWise


-- 
To UNSUBSCRIBE, email to debian-mentors-requ...@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmas...@lists.debian.org
Archive: 
http://lists.debian.org/caktje6extwzsbprpk_l-8d9ux_5cchdxpk1+cfmfutzluqy...@mail.gmail.com



Is it OK to have compile warnings

2013-10-22 Thread Tong Sun
Hi, 

Is it OK to have compile warnings when building a package? If not,

how can I fix the following:

dial.c: In function 'main':
dial.c:273:8: warning: ignoring return value of 'write', declared with 
attribute warn_unused_result [-Wunused-result]
   write(fd, buf, bufidx);

I got a bunch of such ignoring-return-value warnings. 

Thanks



--
To UNSUBSCRIBE, email to debian-mentors-requ...@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmas...@lists.debian.org
Archive: 
http://lists.debian.org/1382492499.68644.yahoomail...@web161904.mail.bf1.yahoo.com