On Wed, Mar 3, 2010 at 2:57 PM, Dave Allan <dallan(a)redhat.com> wrote:
Although I use goto a lot, I generally try to avoid multiple labels
within a
function, just because I think it gets out of hand really quickly. Although
it's a slightly more invasive patch, would you refactor the code to look
something like what I've attached? I haven't even compile tested it as I'm
running late, but that's the idea.
Is there a piece of code in libvirt that exemplifies the preferred
error handling style? (
http://libvirt.org/hacking.html doesn't cover
this issue, as far as I can tell.) Just in the very small part of
libvirt I've hacked on recently I've found a variety of styles,
including
- pair every allocation with a goto label that frees the allocation
and all the earlier ones, and goto the appropriate label on error
- don't use goto at all, and on error, do the necessary frees and
return -1, with each error case having to do one more free
- a combination of the above, with each error case doing the necessary
frees, but using goto out more or less as an alias for return -1
- none of the above, not bothering to free anything when an allocation
fails (see udevSetupSystemDev for an example)
There are probably arguments to be made for each of these styles, but
it would be helpful to know which of them is preferred when writing
new code or refactoring existing code.
That said, I'll gladly refactor my patch towards the preferred style.
--Ed