drivers/video: add kmalloc NULL tests
authorJulia Lawall <julia@diku.dk>
Tue, 22 Sep 2009 23:47:39 +0000 (16:47 -0700)
committerLinus Torvalds <torvalds@linux-foundation.org>
Wed, 23 Sep 2009 14:39:56 +0000 (07:39 -0700)
Check that the result of kmalloc is not NULL before passing it to other
functions.

The semantic match that finds this problem is as follows:
(http://www.emn.fr/x-info/coccinelle/)

// <smpl>
@@
expression *x;
identifier f;
constant char *C;
@@

x = \(kmalloc\|kcalloc\|kzalloc\)(...);
... when != x == NULL
    when != x != NULL
    when != (x || ...)
(
kfree(x)
|
f(...,C,...,x,...)
|
*f(...,x,...)
|
*x->f
)
// </smpl>

[akpm@linux-foundation.org: convert to kstrdup() as well]
Signed-off-by: Julia Lawall <julia@diku.dk>
Cc: Ming Lei <tom.leiming@gmail.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
drivers/video/au1100fb.c

index 378f27745a1db94e18e8fd3b70d0318c2e88b490..a699aab638205c01b0765610cd9846a5468a8944 100644 (file)
@@ -715,8 +715,11 @@ int au1100fb_setup(char *options)
                        }
                        /* Mode option (only option that start with digit) */
                        else if (isdigit(this_opt[0])) {
-                               mode = kmalloc(strlen(this_opt) + 1, GFP_KERNEL);
-                               strncpy(mode, this_opt, strlen(this_opt) + 1);
+                               mode = kstrdup(this_opt, GFP_KERNEL);
+                               if (!mode) {
+                                       print_err("memory allocation failed");
+                                       return -ENOMEM;
+                               }
                        }
                        /* Unsupported option */
                        else {