Skip to contentSkip to conversation

staging: fbtft: Use kmalloc_array

PATCH In review Bhaktipriya Shridhar 2 participants 4 messages Feb 22, 2016 – Feb 23, 2016

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 staging: fbtft: Use kmalloc_array

Use kmalloc_array instead of kmalloc for arrays to prevent integer overflows.
This was done using Coccinelle:

@@
expression e1, e2;
constant C;
type t;
@@
(
- kmalloc(
+ kmalloc_array(
e2
- *
+ ,
sizeof(e1), C)
|
- kmalloc(
+ kmalloc_array(
e1
- *
+ ,
sizeof(t), C)
|
- kmalloc(
+ kmalloc_array(
- sizeof(e1)
- *
e2
+ ,
+ sizeof(e1)
, C)
|
- kmalloc(
+ kmalloc_array(
- sizeof(t)
- *
e1
+ ,
+ sizeof(t)
, C)
)

---
Changes in v2:
- Modified Coccinelle script to respect 80 character limit.

 drivers/staging/fbtft/fb_agm1264k-fl.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/staging/fbtft/fb_agm1264k-fl.c +2 -2
index 2a50cf9..ba9fc44 100644
@@ -272,8 +272,8 @@ static int write_vmem(struct fbtft_par *par, size_t offset, size_t len)
 	int ret = 0;
 
 	/* buffer to convert RGB565 -> grayscale16 -> Dithered image 1bpp */
-	signed short *convert_buf = kmalloc(par->info->var.xres *
-		par->info->var.yres * sizeof(signed short), GFP_NOIO);
+	signed short *convert_buf = kmalloc_array(par->info->var.xres *
+		par->info->var.yres, sizeof(signed short), GFP_NOIO);
 
 	if (!convert_buf)
 		return -ENOMEM;

--
2.1.4

#1 lore raw
Quoted 43 lines · Bhaktipriya Shridhar

Use kmalloc_array instead of kmalloc for arrays to prevent integer overflows.
This was done using Coccinelle:

@@
expression e1, e2;
constant C;
type t;
@@
(
- kmalloc(
+ kmalloc_array(
e2
- *
+ ,
sizeof(e1), C)
|
- kmalloc(
+ kmalloc_array(
e1
- *
+ ,
sizeof(t), C)
|
- kmalloc(
+ kmalloc_array(
- sizeof(e1)
- *
e2
+ ,
+ sizeof(e1)
, C)
|
- kmalloc(
+ kmalloc_array(
- sizeof(t)
- *
e1
+ ,
+ sizeof(t)
, C)
)

Quoted 4 lines

---
Changes in v2:
- Modified Coccinelle script to respect 80 character limit.

I'm not sure to understand this comment. Was the Coccinelle script itself over 80 characters, or was it producing code that went over 80 characters. In the latter case, changing the spacing in the semantic patch won't affect the spacing in the generated code.

julia

Quoted 28 lines
 drivers/staging/fbtft/fb_agm1264k-fl.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/staging/fbtft/fb_agm1264k-fl.c +2 -3
index 2a50cf9..ba9fc44 100644
@@ -272,8 +272,8 @@ static int write_vmem(struct fbtft_par *par, size_t offset, size_t len)
 	int ret = 0;
 
 	/* buffer to convert RGB565 -> grayscale16 -> Dithered image 1bpp */
-	signed short *convert_buf = kmalloc(par->info->var.xres *
-		par->info->var.yres * sizeof(signed short), GFP_NOIO);
+	signed short *convert_buf = kmalloc_array(par->info->var.xres *
+		par->info->var.yres, sizeof(signed short), GFP_NOIO);
 
 	if (!convert_buf)
 		return -ENOMEM;
--

2.1.4

Signature · 5 lines
You received this message because you are subscribed to the Google Groups "outreachy-kernel" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit https://groups.google.com/d/msgid/outreachy-kernel/20160222174716.GA27662%40Karyakshetra.
For more options, visit https://groups.google.com/d/optout.
#2 lore raw

Hello Julia,

Quoted 52 lines · Julia Lawall
Quoted 43 lines · Bhaktipriya Shridhar

Use kmalloc_array instead of kmalloc for arrays to prevent integer overflows.
This was done using Coccinelle:

@@
expression e1, e2;
constant C;
type t;
@@
(
- kmalloc(
+ kmalloc_array(
e2
- *
+ ,
sizeof(e1), C)
|
- kmalloc(
+ kmalloc_array(
e1
- *
+ ,
sizeof(t), C)
|
- kmalloc(
+ kmalloc_array(
- sizeof(e1)
- *
e2
+ ,
+ sizeof(e1)
, C)
|
- kmalloc(
+ kmalloc_array(
- sizeof(t)
- *
e1
+ ,
+ sizeof(t)
, C)
)

Quoted 4 lines

---
Changes in v2:
- Modified Coccinelle script to respect 80 character limit.

Quoted 5 lines

I'm not sure to understand this comment. Was the Coccinelle script itself over 80 characters, or was it producing code that went over 80 characters. In the latter case, changing the spacing in the semantic patch won't affect the spacing in the generated code.

The earlier Coccinelle script produced code that went over 80 characters. In my current script, I'm replacing asterisk with a comma, so that the expressions around stay in their place.

This was my script earlier, which failed to respect the 80 character limit. It replaced the function calls line by line and hence the 80 character limit was violated.

@@
expression e1, e2;
constant C;
type t;
@@
(
- kmalloc(sizeof(e1)*e2, C)
+ kmalloc_array(e2, sizeof(e1), C)
|
- kmalloc(e2*sizeof(e1), C)
+ kmalloc_array(e2, sizeof(e1), C)
|
- kmalloc(e2*sizeof(t), C)
+ kmalloc_array(e2, sizeof(t), C)
|
- kmalloc(sizeof(t)*e2, C)
+ kmalloc_array(e2, sizeof(t), C)
)

If you insist, I can send a v3 for this with the "Change in v3" statement modified.

Thanks,
Bhaktipriya

#3 lore raw
Quoted 62 lines · Bhaktipriya Shridhar

Hello Julia,

Quoted 52 lines · Julia Lawall
Quoted 43 lines · Bhaktipriya Shridhar

Use kmalloc_array instead of kmalloc for arrays to prevent integer overflows.
This was done using Coccinelle:

@@
expression e1, e2;
constant C;
type t;
@@
(
- kmalloc(
+ kmalloc_array(
e2
- *
+ ,
sizeof(e1), C)
|
- kmalloc(
+ kmalloc_array(
e1
- *
+ ,
sizeof(t), C)
|
- kmalloc(
+ kmalloc_array(
- sizeof(e1)
- *
e2
+ ,
+ sizeof(e1)
, C)
|
- kmalloc(
+ kmalloc_array(
- sizeof(t)
- *
e1
+ ,
+ sizeof(t)
, C)
)

Quoted 4 lines

---
Changes in v2:
- Modified Coccinelle script to respect 80 character limit.

Quoted 5 lines

I'm not sure to understand this comment. Was the Coccinelle script itself over 80 characters, or was it producing code that went over 80 characters. In the latter case, changing the spacing in the semantic patch won't affect the spacing in the generated code.

Quoted 7 lines

The earlier Coccinelle script produced code that went over 80 characters. In my current script, I'm replacing asterisk with a comma, so that the expressions around stay in their place.

This was my script earlier, which failed to respect the 80 character limit. It replaced the function calls line by line and hence the 80 character limit was violated.

OK, I see. No need for v3.

julia

Quoted 34 lines

@@
expression e1, e2;
constant C;
type t;
@@
(
- kmalloc(sizeof(e1)*e2, C)
+ kmalloc_array(e2, sizeof(e1), C)
|
- kmalloc(e2*sizeof(e1), C)
+ kmalloc_array(e2, sizeof(e1), C)
|
- kmalloc(e2*sizeof(t), C)
+ kmalloc_array(e2, sizeof(t), C)
|
- kmalloc(sizeof(t)*e2, C)
+ kmalloc_array(e2, sizeof(t), C)
)

If you insist, I can send a v3 for this with the "Change in v3" statement modified.

Thanks,
Bhaktipriya

Signature · 5 lines
You received this message because you are subscribed to the Google Groups "outreachy-kernel" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit https://groups.google.com/d/msgid/outreachy-kernel/CAFNHFT85TWqvUEdyNyKh5oocNGpDsbugX%2BhkjcyPaQ4oAUVfrw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.