aboutsummaryrefslogtreecommitdiffstats
path: root/Documentation/CodingStyle
diff options
context:
space:
mode:
authorDan Carpenter <dan.carpenter@oracle.com>2014-12-02 03:59:50 -0500
committerJonathan Corbet <corbet@lwn.net>2014-12-02 08:55:32 -0500
commitea04036032edda6f771c1381d03832d2ed0f6c31 (patch)
tree3266fb1cff71f9e52440d2710b5f6b03a8f3b8d7 /Documentation/CodingStyle
parent86d3e023e05d90b2b5f88dcbf2e334b5835131f8 (diff)
CodingStyle: add some more error handling guidelines
I added a paragraph on choosing label names, and updated the example code to use a better label name. I also cleaned up the example code to more modern style by moving the allocation out of the initializer and changing the NULL check. Perhaps the most common type of error handling bug in the kernel is "one err bugs". CodingStyle already says that we should "avoid nesting" by using error labels and one err style error handling tends to have multiple indent levels, so this was already bad style. But I've added a new paragraph explaining how to avoid one err bugs by using multiple error labels which is, hopefully, more clear. Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com> Acked-by: Julia Lawall <julia.lawall@lip6.fr> [jc: added GFP_KERNEL to kmalloc() call] Signed-off-by: Jonathan Corbet <corbet@lwn.net>
Diffstat (limited to 'Documentation/CodingStyle')
-rw-r--r--Documentation/CodingStyle27
1 files changed, 22 insertions, 5 deletions
diff --git a/Documentation/CodingStyle b/Documentation/CodingStyle
index 9f28b140dc89..618a33c940df 100644
--- a/Documentation/CodingStyle
+++ b/Documentation/CodingStyle
@@ -392,7 +392,12 @@ The goto statement comes in handy when a function exits from multiple
392locations and some common work such as cleanup has to be done. If there is no 392locations and some common work such as cleanup has to be done. If there is no
393cleanup needed then just return directly. 393cleanup needed then just return directly.
394 394
395The rationale is: 395Choose label names which say what the goto does or why the goto exists. An
396example of a good name could be "out_buffer:" if the goto frees "buffer". Avoid
397using GW-BASIC names like "err1:" and "err2:". Also don't name them after the
398goto location like "err_kmalloc_failed:"
399
400The rationale for using gotos is:
396 401
397- unconditional statements are easier to understand and follow 402- unconditional statements are easier to understand and follow
398- nesting is reduced 403- nesting is reduced
@@ -403,9 +408,10 @@ The rationale is:
403int fun(int a) 408int fun(int a)
404{ 409{
405 int result = 0; 410 int result = 0;
406 char *buffer = kmalloc(SIZE); 411 char *buffer;
407 412
408 if (buffer == NULL) 413 buffer = kmalloc(SIZE, GFP_KERNEL);
414 if (!buffer)
409 return -ENOMEM; 415 return -ENOMEM;
410 416
411 if (condition1) { 417 if (condition1) {
@@ -413,14 +419,25 @@ int fun(int a)
413 ... 419 ...
414 } 420 }
415 result = 1; 421 result = 1;
416 goto out; 422 goto out_buffer;
417 } 423 }
418 ... 424 ...
419out: 425out_buffer:
420 kfree(buffer); 426 kfree(buffer);
421 return result; 427 return result;
422} 428}
423 429
430A common type of bug to be aware of it "one err bugs" which look like this:
431
432err:
433 kfree(foo->bar);
434 kfree(foo);
435 return ret;
436
437The bug in this code is that on some exit paths "foo" is NULL. Normally the
438fix for this is to split it up into two error labels "err_bar:" and "err_foo:".
439
440
424 Chapter 8: Commenting 441 Chapter 8: Commenting
425 442
426Comments are good, but there is also a danger of over-commenting. NEVER 443Comments are good, but there is also a danger of over-commenting. NEVER