tech-misc archive

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

Re: __attribute__((warn_unused_result)) for NetBSD?



> Why do you think it's OK to ignore error checks on printf but not on
> write?  I'm having trouble seeing the relevant difference.

I think you know a difference. printf is often used for printing
something to a terminal or pipes. I consider this not very critical.
While sending data by write(2) to sockets or file descriptors is
very critical in almost all (all?) cases.

Though, I agree with you: this difference is not obvious.  Also I ask
you to not forget that we are talking about WARNING messages at
SOME LEVEL OF PARANOIA.

In a "perfect code" an exit value of printf(3) should also be checked.
And it is easy to do. See item 5 below.

> That is, if I call (say) write() in a circumstance where I don't
> consider it necessary to check the return value, I have only a few
> options:

> 1) Turn off unused-result warnings entirely, at least for that file.
> 2) Hack up the include file to remove the marking from write().
> 3) Don't use -Werror, and ignore the warning message.
> 4) Uglify the code with a (void) cast.

> I won't do (4) - I have major philosophical disagreement with

There is a better way, MUCH better way.

  5)
   static void xputenv (char *s)
   {
       if (putenv (s)){
           perror ("putenv(3) failed");
           exit (1);
       }
   }

This approach is used in thousands of programs out there. I also used
it in my open source paexec and runawk programs as well as in many
others. emalloc(3) and friends from libutil is another example. It was
also used by Stevens in his books and...

This way keeps your program clean/readable and make it stable. But I
think this is a different question.

-- 
Best regards, Aleksey Cheusov.


Home | Main Index | Thread Index | Old Index