public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH 1/5] drivers/video: Add kmalloc NULL tests
@ 2009-07-30 14:00 Julia Lawall
  2009-08-03 20:46 ` Andrew Morton
  0 siblings, 1 reply; 2+ messages in thread
From: Julia Lawall @ 2009-07-30 14:00 UTC (permalink / raw)
  To: linux-kernel, kernel-janitors

From: Julia Lawall <julia@diku.dk>

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>

Signed-off-by: Julia Lawall <julia@diku.dk>

---
 drivers/video/au1100fb.c            |    4 ++++
 1 files changed, 4 insertions(+), 0 deletions(-)

diff --git a/drivers/video/au1100fb.c b/drivers/video/au1100fb.c
index 378f277..1766846 100644
--- a/drivers/video/au1100fb.c
+++ b/drivers/video/au1100fb.c
@@ -716,6 +716,10 @@ 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);
+				if (!mode) {
+					print_err("memory allocation failed");
+					return -ENOMEM;
+				}
 				strncpy(mode, this_opt, strlen(this_opt) + 1);
 			}
 			/* Unsupported option */

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

* Re: [PATCH 1/5] drivers/video: Add kmalloc NULL tests
  2009-07-30 14:00 [PATCH 1/5] drivers/video: Add kmalloc NULL tests Julia Lawall
@ 2009-08-03 20:46 ` Andrew Morton
  0 siblings, 0 replies; 2+ messages in thread
From: Andrew Morton @ 2009-08-03 20:46 UTC (permalink / raw)
  To: Julia Lawall; +Cc: linux-kernel, kernel-janitors, Ming Lei

On Thu, 30 Jul 2009 16:00:33 +0200 (CEST)
Julia Lawall <julia@diku.dk> wrote:

> From: Julia Lawall <julia@diku.dk>
> 
> 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>
> 
> Signed-off-by: Julia Lawall <julia@diku.dk>
> 
> ---
>  drivers/video/au1100fb.c            |    4 ++++
>  1 files changed, 4 insertions(+), 0 deletions(-)
> 
> diff --git a/drivers/video/au1100fb.c b/drivers/video/au1100fb.c
> index 378f277..1766846 100644
> --- a/drivers/video/au1100fb.c
> +++ b/drivers/video/au1100fb.c
> @@ -716,6 +716,10 @@ 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);
> +				if (!mode) {
> +					print_err("memory allocation failed");
> +					return -ENOMEM;
> +				}
>  				strncpy(mode, this_opt, strlen(this_opt) + 1);
>  			}
>  			/* Unsupported option */

We may as well clean up that open-coded kstrdup() too?

--- a/drivers/video/au1100fb.c~drivers-video-add-kmalloc-null-tests-fix
+++ a/drivers/video/au1100fb.c
@@ -715,12 +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);
+				mode = kstrdup(this_opt, GFP_KERNEL);
 				if (!mode) {
 					print_err("memory allocation failed");
 					return -ENOMEM;
 				}
-				strncpy(mode, this_opt, strlen(this_opt) + 1);
 			}
 			/* Unsupported option */
 			else {
_


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

end of thread, other threads:[~2009-08-03 20:46 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-07-30 14:00 [PATCH 1/5] drivers/video: Add kmalloc NULL tests Julia Lawall
2009-08-03 20:46 ` Andrew Morton

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox