lkml.org 
[lkml]   [2004]   [Nov]   [2]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
/
Date
From
SubjectRe: question on common error-handling idiom
On Tue, 2 Nov 2004, Chris Friesen wrote:

>
> There's something I've been wondering about for a while. There is a lot of
> code in linux that looks something like this:
>
>
> err = -ERRORCODE
> if (error condition)
> goto out;
>
>
> While nice to read, it would seem that it might be more efficient to do the
> following:
>
> if (error condition) {
> err = -ERRORCODE;
> goto out;
> }
>
>
> Is there any particular reason why the former is preferred? Is the compiler
> smart enough to optimize away the additional write in the non-error path?
>
There are some places that do

err = -SOMEERROR;
if (some_error)
goto out;
if (some_other_error)
goto out;
if (another_error)
goto out;

In that case, where there are several different conditions that need
testing, but they all need to return the same error, setting the error
just once seems the best approach.

but for the places that do

err = -SOMEERROR;
if (condition)
goto out;

err = -OTHERERROR;
if (condition)
goto out;

I would tend to agree with you that moving the setting of the error inside
the if() would make sense.

Let's see what other people think :)


--
Jesper Juhl

-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/

\
 
 \ /
  Last update: 2005-03-22 14:07    [W:0.096 / U:0.176 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site