linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] CodingStyle: add some more error handling guidelines
@ 2016-08-22 13:57 Michael S. Tsirkin
  2016-08-22 14:16 ` Jonathan Corbet
                   ` (2 more replies)
  0 siblings, 3 replies; 28+ messages in thread
From: Michael S. Tsirkin @ 2016-08-22 13:57 UTC (permalink / raw)
  To: linux-kernel
  Cc: Dan Carpenter, Julia Lawall, Jonathan Corbet, Jason Wang,
	linux-doc, virtualization

commit commit ea04036032edda6f771c1381d03832d2ed0f6c31 ("CodingStyle:
add some more error handling guidelines") suggests never naming goto
labels after the goto location - that is the error that is handled.

But it's actually pretty common and IMHO it's a reasonable style
provided each error gets its own label, and each label comes after the
matching cleanup:

                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;

Provides some symmetry and makes it easy to add more cases as code
calling goto does not need to worry about how is the error handled.

Cc: Dan Carpenter <dan.carpenter@oracle.com>
Cc: Julia Lawall <julia.lawall@lip6.fr>
Cc: Jonathan Corbet <corbet@lwn.net>
---
 tools/virtio/ringtest/main.h |  4 +++-
 Documentation/CodingStyle    | 44 ++++++++++++++++++++++++++++++++++++++++++--
 2 files changed, 45 insertions(+), 3 deletions(-)

diff --git a/tools/virtio/ringtest/main.h b/tools/virtio/ringtest/main.h
index 16917ac..e4d76c3 100644
--- a/tools/virtio/ringtest/main.h
+++ b/tools/virtio/ringtest/main.h
@@ -80,7 +80,9 @@ extern unsigned ring_size;
 
 /* Is there a portable way to do this? */
 #if defined(__x86_64__) || defined(__i386__)
-#define cpu_relax() asm ("rep; nop" ::: "memory")
+#define cpu_relax() do { \
+	asm ("rep; nop" ::: "memory"); \
+} while (0)
 #else
 #define cpu_relax() assert(0)
 #endif
diff --git a/Documentation/CodingStyle b/Documentation/CodingStyle
index a096836..af2b5e9 100644
--- a/Documentation/CodingStyle
+++ b/Documentation/CodingStyle
@@ -397,8 +397,7 @@ cleanup needed then just return directly.
 
 Choose label names which say what the goto does or why the goto exists.  An
 example of a good name could be "out_buffer:" if the goto frees "buffer".  Avoid
-using GW-BASIC names like "err1:" and "err2:".  Also don't name them after the
-goto location like "err_kmalloc_failed:"
+using GW-BASIC names like "err1:" and "err2:".
 
 The rationale for using gotos is:
 
@@ -440,6 +439,47 @@ A common type of bug to be aware of is "one err bugs" which look like this:
 The bug in this code is that on some exit paths "foo" is NULL.  Normally the
 fix for this is to split it up into two error labels "err_bar:" and "err_foo:".
 
+Note that labels normally come before the appropriate cleanups:
+
+		foo = kmalloc(SIZE, GFP_KERNEL); 
+		if (!foo)
+			goto out;
+
+		foo->bar = kmalloc(SIZE, GFP_KERNEL); 
+		if (!foo->bar)
+			goto out_foo;
+		...
+		if (err)
+			goto out_bar;
+
+	out_bar:
+		kfree(foo->bar);
+
+	out_foo:
+		kfree(foo);
+
+	out:
+		return ret;
+
+If labels are named after the goto location (or error that was detected), they
+come after the matching cleanup code:
+
+		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;
 
 		Chapter 8: Commenting
 
-- 
MST

^ permalink raw reply related	[flat|nested] 28+ messages in thread
* Re: [PATCH v2] fs-fat: Less function calls in fat_fill_super() after error detection
@ 2014-12-02  7:37 Julia Lawall
  2014-12-02  8:59 ` [patch] CodingStyle: add some more error handling guidelines Dan Carpenter
  0 siblings, 1 reply; 28+ messages in thread
From: Julia Lawall @ 2014-12-02  7:37 UTC (permalink / raw)
  To: Dan Carpenter
  Cc: SF Markus Elfring, OGAWA Hirofumi, linux-kernel, kernel-janitors,
	trivial, Coccinelle



On Tue, 2 Dec 2014, Dan Carpenter wrote:

> On Mon, Dec 01, 2014 at 10:22:38PM +0100, SF Markus Elfring wrote:
> > >> Which names would be better acceptable for you?
> > > 
> > > You named it after the goto location but the label name should be based
> > > on the label location to say what the goto does.
> > 
> > I find it easier occasionally to name a label similarly to the jump target.
> 
> That is a useless thing to do.
> 
> > It seems that there are a few variations used for the affected identifiers.
> 
> There is a lot of crap code in the kernel, yes.

Does the label naming strategy appear in the conding style documentation 
anywhere?  There are so many variants that just from looking at the code, 
it is hard to guess what is the best strategy.  For example, out1, out2, 
etc are pretty uninformative, but they are concise and easy to spell 
correctly.

julia

^ permalink raw reply	[flat|nested] 28+ messages in thread

end of thread, other threads:[~2016-08-23 14:09 UTC | newest]

Thread overview: 28+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-08-22 13:57 [PATCH] CodingStyle: add some more error handling guidelines Michael S. Tsirkin
2016-08-22 14:16 ` Jonathan Corbet
2016-08-22 14:53   ` Michael S. Tsirkin
2016-08-22 18:31     ` Dan Carpenter
2016-08-22 18:39       ` Michael S. Tsirkin
2016-08-22 18:50     ` Dan Carpenter
2016-08-22 19:31       ` Michael S. Tsirkin
2016-08-22 14:23 ` Dan Carpenter
2016-08-23 11:03 ` Bjørn Mork
2016-08-23 11:58   ` Dan Carpenter
2016-08-23 12:46     ` Bjørn Mork
2016-08-23 14:05       ` Dan Carpenter
  -- strict thread matches above, loose matches on Subject: below --
2014-12-02  7:37 [PATCH v2] fs-fat: Less function calls in fat_fill_super() after error detection Julia Lawall
2014-12-02  8:59 ` [patch] CodingStyle: add some more error handling guidelines Dan Carpenter
2014-12-02  9:09   ` Julia Lawall
2014-12-02 13:56     ` Jonathan Corbet
2014-12-03 12:31   ` SF Markus Elfring
2014-12-03 12:39     ` Arend van Spriel
2014-12-03 12:51       ` SF Markus Elfring
2014-12-03 12:45     ` Dan Carpenter
2014-12-03 12:52       ` Julia Lawall
2014-12-03 13:15         ` Dan Carpenter
2014-12-03 13:00       ` SF Markus Elfring
2014-12-03 13:20         ` Dan Carpenter
2014-12-03 13:24           ` SF Markus Elfring
2014-12-03 14:08             ` Arend van Spriel
2014-12-03 16:00               ` SF Markus Elfring
2014-12-03 19:13                 ` Arend van Spriel
2014-12-03 23:11                   ` SF Markus Elfring

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).