[ccan] help with gracefully dealing with alloc failure in a recursive function

Daniel Burke dan.p.burke at gmail.com
Fri Oct 7 08:54:08 EST 2011


I'm wondering what a commonly acceptable method of handling this failure
would be, my Google-Fu's not giving me answers I like, so   I'm turning to
the collective wisdom of this list. I suspect my knowledge of other
languages is poisoning my thought process.

So parsing XML in a recursive function, with a structure that contains the
relevant state of the task. My initial plan is to add a variable to the
structure named "failed", and if an alloc fails I set it, and then test this
after every function call that can fail, trying to bail out to the head
function ASAP, where I call the free function on the partial tree I've
created so far.

This puts a lot of ugly checking code in what is presently on the clean side
of what I typically write. Most other languages I'd raise an exception and
deal with the failure once.

I've a few existing Linux Kernel style Goto-Exceptions to keep all the error
code together, and not spread throughout the meat of the functions, however
my understanding is that it's a Bad Thing (tm) to goto across functions, as
depending on compiler/flags there's going to have to be some stack
twiddling, and while my inner assembly programmer says just store SI in the
data structure, every other bone in my body is telling me this is a capitol
offense.

Should I bite the bullet and turn my pretty 1 page function into a 3 page
function with lots of checking, or is there a clever/easy way to quickly
bail?


regards,

dan
--
"Within C++, there is a much smaller and cleaner language struggling to get
out"
--Bjarne Stroustrup
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.ozlabs.org/pipermail/ccan/attachments/20111007/588c051c/attachment.html>


More information about the ccan mailing list