Re: Critical crypto bug leaves Linux, hundreds of apps open ...
by tptacek on Tuesday March 04, 2014 @03:05PM
(#7342706)
Using zero for errors means you can express error checking directly with C expressions that appear boolean. It's not as safe or rigorous, but is more readable.
Re: Critical crypto bug leaves Linux, hundreds of apps open ...
by userbinator on Tuesday March 04, 2014 @07:14PM
(#7344259)
Using nonzero for errors means you can do the same thing. This is common in things like this:
if(somefunc()) goto fail;
Additionally, if the return is a fail/pass status, there is only one success (0), and you can use all the nonzero values for error codes.
Re: Critical crypto bug leaves Linux, hundreds of apps open ...
by prewett on Wednesday March 05, 2014 @07:54AM
(#7346639)
Yes, you can, but that doesn't read very well to my eyes, given most function names. if(open()), if(read()), if (CreateWindowExW()), all suggest success to me.
if (!open(...)) goto fail;
if (!read(...)) goto fail;
if (!close(...)) goto fail;
reads a lot nicer to me. Although, the reality is that you need to assign to an error variable, which makes it not as nice and clean.
Re: Critical crypto bug leaves Linux, hundreds of apps open ...
by cjensen on Tuesday March 04, 2014 @03:25PM
(#7342859)
No. CreateFile returns INVALID_HANDLE_VALUE which is -1. Compare this to CreateSemaphore which also returns a HANDLE, but returns NULL[1] on failure.
Returning int(0) as a failure is often done in C if the return value is a boolean since C lacks a true boolean type.
[1] NULL should never be used in actual code because it is ambiguous.
Re: Critical crypto bug leaves Linux, hundreds of apps open ...
by userbinator on Tuesday March 04, 2014 @07:18PM
(#7344285)
Those are functions that return more possible success values than failure values. The three common conventions seem to be:
Nonzero: error code, zero: success
Nonzero: one of many possible success values, zero: error (e.g. malloc())
Negative: error code, positive: one of many possible success values
And each has its tradeoffs.
I really don't understand why anyone would return an int and have 0 indicate failure. OpenSSL does this too, and it makes no sense to me. I don't know how far this goes back, but clearly we have a long tradition of programs returning 0 to the OS to indicate no error.