Bjørn Mork
2016-Aug-23 11:03 UTC
[PATCH] CodingStyle: add some more error handling guidelines
"Michael S. Tsirkin" <mst at redhat.com> writes:> foo = kmalloc(SIZE, GFP_KERNEL); > if (!foo) > goto err_foo; > > foo->bar = kmalloc(SIZE, GFP_KERNEL); > if (!foo->bar) > goto err_bar; > ... > > kfree(foo->bar); > err_bar: > > kfree(foo); > err_foo: > > return ret;I believe the CodingStyle already contain far too much personal style to be useful as real style guide. FWIW, I prefer a single error label, at the "cost" of additional tests in the error path: foo = kmalloc(SIZE, GFP_KERNEL); if (!foo) goto err; foo->bar = kmalloc(SIZE, GFP_KERNEL); if (!foo->bar) goto err; ... if (ret) goto err; return 0; err: if (foo) kfree(foo->bar); kfree(foo); return ret; The advantage is that I don't have to manage X different labels, ensuring that they have the order is correct if some part of the function is refactored etc. That tends to get too complicated for my simple brain. And since the error path is rarely tested, complicated equals buggy. My sample will of course trigger all those nice "optimizing the error path" patches, but I ignore those anyway so that's not a big deal. Bj?rn
Bjørn Mork
2016-Aug-23 12:46 UTC
[PATCH] CodingStyle: add some more error handling guidelines
Dan Carpenter <dan.carpenter at oracle.com> writes:> Hike up the mountain, then if you get stuck hike back down using the > exact same path.OK, I understand what you say. I just can't resist objecting to that example ;) In my experience, finding the exact same path back after hiking up a mountain is really hard. Especially if you are in enough trouble already to get stuck. Up and down tend to look completely different. Looking back all the time while you go up might help, but finding your way back by simply taking the reverse path is definitely not easy. Bj?rn
Apparently Analagous Threads
- [PATCH] CodingStyle: add some more error handling guidelines
- [PATCH] CodingStyle: add some more error handling guidelines
- [PATCH] CodingStyle: add some more error handling guidelines
- [PATCH] CodingStyle: add some more error handling guidelines
- [PATCH] CodingStyle: add some more error handling guidelines