From: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
To: Dan Carpenter <dan.carpenter@oracle.com>
Cc: Julia Lawall <julia.lawall@lip6.fr>,
Sakari Alius <sakari.ailus@iki.fi>,
wharms@bfs.de, linux-media@vger.kernel.org,
kernel-janitors@vger.kernel.org
Subject: Re: [patch] [media] uvcvideo: freeing an error pointer
Date: Wed, 30 Nov 2016 13:53:03 +0000 [thread overview]
Message-ID: <3099994.m2oKJeJMud@avalon> (raw)
In-Reply-To: <20161130123326.GH28558@mwanda>
Hi Dan,
On Wednesday 30 Nov 2016 15:33:26 Dan Carpenter wrote:
> On Mon, Nov 28, 2016 at 04:49:36PM +0200, Laurent Pinchart wrote:
> > On Monday 28 Nov 2016 14:54:58 Julia Lawall wrote:
> >> On Mon, 28 Nov 2016, Dan Carpenter wrote:
> >>> I understand the comparison, but I just think it's better if people
> >>> always keep track of what has been allocated and what has not. I
> >>> tried so hard to get Markus to stop sending those hundreds of patches
> >>> where he's like "this function has a sanity check so we can pass
> >>> pointers that weren't allocated"... It's garbage code.
> >>>
> >>> But I understand that other people don't agree.
> >>
> >> In my opinion, it is good for code understanding to only do what is
> >> useful to do. It's not a hard and fast rule, but I think it is
> >> something to take into account.
> >
> > On the other hand it complicates the error handling code by increasing the
> > number of goto labels, and it then becomes pretty easy when reworking code
> > to goto the wrong label. This is even more of an issue when the rework
> > doesn't touch the error handling code, in which case the reviewers can
> > easily miss the issue if they don't open the original source file to
> > check the goto labels.
>
> It's really not. I've looked at a lot of error handling in the past
> five years and sent hundreds of fixes for error paths, more than any
> other kernel developer during that time. Although it seems obvious in
> retrospect, it took me years to realize this but the canonical way of
> doing error handling is the least error prone.
>
> Counting the labels is the wrong way to measure complexity. That's like
> counting the number of functions. Code with lots of functions is not
> necessarily more complicated than if it's just one big function.
>
> Part of the key to unwinding correctly is using good label names. It
> should say what the label does. Some people use come-from labels names
> like "goto kmalloc_failed". Those are totally useless. It's like
> naming your functions "called_from_foo()". If there is only one goto
> then come-from label names are useless and if there are more than one
> goto which go to the same label then it's useless *and* misleading.
Yes, label naming is (or at least should be) largely agreed upon, they should
name the unwinding action, not the cause of the failure.
> Functions should be written so you can read them from top to bottom
> without scrolling back and forth.
>
> a = alloc();
> if (!a)
> return -ENOMEM;
>
> b = alloc();
> if (!b) {
> ret = -ENOMEM;
> goto free_a;
> }
But then you get the following patch (which, apart from being totally made up,
probably shows what I've watched yesterday evening).
@@ ... @@
return -ENOMEM;
}
+ ret = check_time_vortex();
+ if (ret < 0)
+ goto power_off_tardis;
+
matt_smith = alloc_regeneration();
if (math_smith->status != OK) {
ret = -E_NEEDS_FISH_FINGERS_AND_CUSTARD;
From that code only you can't tell whether the jump label is the right one. If
a single jump label is used with an unwinding code block that supports non-
allocated resources, you don't have to ask yourself any question.
> That code tells a complete story without any scrolling. It's future
> proof too. You can tell the goto is correct just from the name. But
> when it's:
>
> a = alloc();
> if (!a)
> goto out;
> b = alloc();
> goto out;
>
> That code doesn't have enough information to be understandable on it's
> own. It's way more bug prone than the first sample.
--
Regards,
Laurent Pinchart
WARNING: multiple messages have this Message-ID (diff)
From: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
To: Dan Carpenter <dan.carpenter@oracle.com>
Cc: Julia Lawall <julia.lawall@lip6.fr>,
Sakari Alius <sakari.ailus@iki.fi>,
wharms@bfs.de, linux-media@vger.kernel.org,
kernel-janitors@vger.kernel.org
Subject: Re: [patch] [media] uvcvideo: freeing an error pointer
Date: Wed, 30 Nov 2016 15:53:03 +0200 [thread overview]
Message-ID: <3099994.m2oKJeJMud@avalon> (raw)
In-Reply-To: <20161130123326.GH28558@mwanda>
Hi Dan,
On Wednesday 30 Nov 2016 15:33:26 Dan Carpenter wrote:
> On Mon, Nov 28, 2016 at 04:49:36PM +0200, Laurent Pinchart wrote:
> > On Monday 28 Nov 2016 14:54:58 Julia Lawall wrote:
> >> On Mon, 28 Nov 2016, Dan Carpenter wrote:
> >>> I understand the comparison, but I just think it's better if people
> >>> always keep track of what has been allocated and what has not. I
> >>> tried so hard to get Markus to stop sending those hundreds of patches
> >>> where he's like "this function has a sanity check so we can pass
> >>> pointers that weren't allocated"... It's garbage code.
> >>>
> >>> But I understand that other people don't agree.
> >>
> >> In my opinion, it is good for code understanding to only do what is
> >> useful to do. It's not a hard and fast rule, but I think it is
> >> something to take into account.
> >
> > On the other hand it complicates the error handling code by increasing the
> > number of goto labels, and it then becomes pretty easy when reworking code
> > to goto the wrong label. This is even more of an issue when the rework
> > doesn't touch the error handling code, in which case the reviewers can
> > easily miss the issue if they don't open the original source file to
> > check the goto labels.
>
> It's really not. I've looked at a lot of error handling in the past
> five years and sent hundreds of fixes for error paths, more than any
> other kernel developer during that time. Although it seems obvious in
> retrospect, it took me years to realize this but the canonical way of
> doing error handling is the least error prone.
>
> Counting the labels is the wrong way to measure complexity. That's like
> counting the number of functions. Code with lots of functions is not
> necessarily more complicated than if it's just one big function.
>
> Part of the key to unwinding correctly is using good label names. It
> should say what the label does. Some people use come-from labels names
> like "goto kmalloc_failed". Those are totally useless. It's like
> naming your functions "called_from_foo()". If there is only one goto
> then come-from label names are useless and if there are more than one
> goto which go to the same label then it's useless *and* misleading.
Yes, label naming is (or at least should be) largely agreed upon, they should
name the unwinding action, not the cause of the failure.
> Functions should be written so you can read them from top to bottom
> without scrolling back and forth.
>
> a = alloc();
> if (!a)
> return -ENOMEM;
>
> b = alloc();
> if (!b) {
> ret = -ENOMEM;
> goto free_a;
> }
But then you get the following patch (which, apart from being totally made up,
probably shows what I've watched yesterday evening).
@@ ... @@
return -ENOMEM;
}
+ ret = check_time_vortex();
+ if (ret < 0)
+ goto power_off_tardis;
+
matt_smith = alloc_regeneration();
if (math_smith->status != OK) {
ret = -E_NEEDS_FISH_FINGERS_AND_CUSTARD;
>From that code only you can't tell whether the jump label is the right one. If
a single jump label is used with an unwinding code block that supports non-
allocated resources, you don't have to ask yourself any question.
> That code tells a complete story without any scrolling. It's future
> proof too. You can tell the goto is correct just from the name. But
> when it's:
>
> a = alloc();
> if (!a)
> goto out;
> b = alloc();
> goto out;
>
> That code doesn't have enough information to be understandable on it's
> own. It's way more bug prone than the first sample.
--
Regards,
Laurent Pinchart
next prev parent reply other threads:[~2016-11-30 13:53 UTC|newest]
Thread overview: 30+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-11-25 10:28 [patch] [media] uvcvideo: freeing an error pointer Dan Carpenter
2016-11-25 10:28 ` Dan Carpenter
2016-11-25 13:40 ` SF Markus Elfring
2016-11-25 13:40 ` SF Markus Elfring
2016-11-25 13:57 ` Laurent Pinchart
2016-11-25 13:57 ` Laurent Pinchart
2016-11-25 14:47 ` walter harms
2016-11-25 14:47 ` walter harms
2016-11-25 16:02 ` Laurent Pinchart
2016-11-25 16:02 ` Laurent Pinchart
2016-11-25 19:20 ` Dan Carpenter
2016-11-25 19:20 ` Dan Carpenter
2016-11-27 16:21 ` Sakari Alius
2016-11-27 16:21 ` Sakari Alius
2016-11-28 13:49 ` Dan Carpenter
2016-11-28 13:49 ` Dan Carpenter
2016-11-28 13:54 ` Julia Lawall
2016-11-28 13:54 ` Julia Lawall
2016-11-28 14:49 ` Laurent Pinchart
2016-11-28 14:49 ` Laurent Pinchart
2016-11-30 12:33 ` Dan Carpenter
2016-11-30 12:33 ` Dan Carpenter
2016-11-30 13:53 ` Laurent Pinchart [this message]
2016-11-30 13:53 ` Laurent Pinchart
2016-11-30 14:45 ` Dan Carpenter
2016-11-30 14:45 ` Dan Carpenter
2016-11-29 6:48 ` Julia Lawall
2016-11-29 6:48 ` Julia Lawall
2016-11-25 19:08 ` Dan Carpenter
2016-11-25 19:08 ` Dan Carpenter
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=3099994.m2oKJeJMud@avalon \
--to=laurent.pinchart@ideasonboard.com \
--cc=dan.carpenter@oracle.com \
--cc=julia.lawall@lip6.fr \
--cc=kernel-janitors@vger.kernel.org \
--cc=linux-media@vger.kernel.org \
--cc=sakari.ailus@iki.fi \
--cc=wharms@bfs.de \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.