linux-uconsole/drivers/media/usb/gspca
Kees Cook 6da2ec5605 treewide: kmalloc() -> kmalloc_array()
The kmalloc() function has a 2-factor argument form, kmalloc_array(). This
patch replaces cases of:

        kmalloc(a * b, gfp)

with:
        kmalloc_array(a * b, gfp)

as well as handling cases of:

        kmalloc(a * b * c, gfp)

with:

        kmalloc(array3_size(a, b, c), gfp)

as it's slightly less ugly than:

        kmalloc_array(array_size(a, b), c, gfp)

This does, however, attempt to ignore constant size factors like:

        kmalloc(4 * 1024, gfp)

though any constants defined via macros get caught up in the conversion.

Any factors with a sizeof() of "unsigned char", "char", and "u8" were
dropped, since they're redundant.

The tools/ directory was manually excluded, since it has its own
implementation of kmalloc().

The Coccinelle script used for this was:

// Fix redundant parens around sizeof().
@@
type TYPE;
expression THING, E;
@@

(
  kmalloc(
-	(sizeof(TYPE)) * E
+	sizeof(TYPE) * E
  , ...)
|
  kmalloc(
-	(sizeof(THING)) * E
+	sizeof(THING) * E
  , ...)
)

// Drop single-byte sizes and redundant parens.
@@
expression COUNT;
typedef u8;
typedef __u8;
@@

(
  kmalloc(
-	sizeof(u8) * (COUNT)
+	COUNT
  , ...)
|
  kmalloc(
-	sizeof(__u8) * (COUNT)
+	COUNT
  , ...)
|
  kmalloc(
-	sizeof(char) * (COUNT)
+	COUNT
  , ...)
|
  kmalloc(
-	sizeof(unsigned char) * (COUNT)
+	COUNT
  , ...)
|
  kmalloc(
-	sizeof(u8) * COUNT
+	COUNT
  , ...)
|
  kmalloc(
-	sizeof(__u8) * COUNT
+	COUNT
  , ...)
|
  kmalloc(
-	sizeof(char) * COUNT
+	COUNT
  , ...)
|
  kmalloc(
-	sizeof(unsigned char) * COUNT
+	COUNT
  , ...)
)

// 2-factor product with sizeof(type/expression) and identifier or constant.
@@
type TYPE;
expression THING;
identifier COUNT_ID;
constant COUNT_CONST;
@@

(
- kmalloc
+ kmalloc_array
  (
-	sizeof(TYPE) * (COUNT_ID)
+	COUNT_ID, sizeof(TYPE)
  , ...)
|
- kmalloc
+ kmalloc_array
  (
-	sizeof(TYPE) * COUNT_ID
+	COUNT_ID, sizeof(TYPE)
  , ...)
|
- kmalloc
+ kmalloc_array
  (
-	sizeof(TYPE) * (COUNT_CONST)
+	COUNT_CONST, sizeof(TYPE)
  , ...)
|
- kmalloc
+ kmalloc_array
  (
-	sizeof(TYPE) * COUNT_CONST
+	COUNT_CONST, sizeof(TYPE)
  , ...)
|
- kmalloc
+ kmalloc_array
  (
-	sizeof(THING) * (COUNT_ID)
+	COUNT_ID, sizeof(THING)
  , ...)
|
- kmalloc
+ kmalloc_array
  (
-	sizeof(THING) * COUNT_ID
+	COUNT_ID, sizeof(THING)
  , ...)
|
- kmalloc
+ kmalloc_array
  (
-	sizeof(THING) * (COUNT_CONST)
+	COUNT_CONST, sizeof(THING)
  , ...)
|
- kmalloc
+ kmalloc_array
  (
-	sizeof(THING) * COUNT_CONST
+	COUNT_CONST, sizeof(THING)
  , ...)
)

// 2-factor product, only identifiers.
@@
identifier SIZE, COUNT;
@@

- kmalloc
+ kmalloc_array
  (
-	SIZE * COUNT
+	COUNT, SIZE
  , ...)

// 3-factor product with 1 sizeof(type) or sizeof(expression), with
// redundant parens removed.
@@
expression THING;
identifier STRIDE, COUNT;
type TYPE;
@@

(
  kmalloc(
-	sizeof(TYPE) * (COUNT) * (STRIDE)
+	array3_size(COUNT, STRIDE, sizeof(TYPE))
  , ...)
|
  kmalloc(
-	sizeof(TYPE) * (COUNT) * STRIDE
+	array3_size(COUNT, STRIDE, sizeof(TYPE))
  , ...)
|
  kmalloc(
-	sizeof(TYPE) * COUNT * (STRIDE)
+	array3_size(COUNT, STRIDE, sizeof(TYPE))
  , ...)
|
  kmalloc(
-	sizeof(TYPE) * COUNT * STRIDE
+	array3_size(COUNT, STRIDE, sizeof(TYPE))
  , ...)
|
  kmalloc(
-	sizeof(THING) * (COUNT) * (STRIDE)
+	array3_size(COUNT, STRIDE, sizeof(THING))
  , ...)
|
  kmalloc(
-	sizeof(THING) * (COUNT) * STRIDE
+	array3_size(COUNT, STRIDE, sizeof(THING))
  , ...)
|
  kmalloc(
-	sizeof(THING) * COUNT * (STRIDE)
+	array3_size(COUNT, STRIDE, sizeof(THING))
  , ...)
|
  kmalloc(
-	sizeof(THING) * COUNT * STRIDE
+	array3_size(COUNT, STRIDE, sizeof(THING))
  , ...)
)

// 3-factor product with 2 sizeof(variable), with redundant parens removed.
@@
expression THING1, THING2;
identifier COUNT;
type TYPE1, TYPE2;
@@

(
  kmalloc(
-	sizeof(TYPE1) * sizeof(TYPE2) * COUNT
+	array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2))
  , ...)
|
  kmalloc(
-	sizeof(TYPE1) * sizeof(THING2) * (COUNT)
+	array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2))
  , ...)
|
  kmalloc(
-	sizeof(THING1) * sizeof(THING2) * COUNT
+	array3_size(COUNT, sizeof(THING1), sizeof(THING2))
  , ...)
|
  kmalloc(
-	sizeof(THING1) * sizeof(THING2) * (COUNT)
+	array3_size(COUNT, sizeof(THING1), sizeof(THING2))
  , ...)
|
  kmalloc(
-	sizeof(TYPE1) * sizeof(THING2) * COUNT
+	array3_size(COUNT, sizeof(TYPE1), sizeof(THING2))
  , ...)
|
  kmalloc(
-	sizeof(TYPE1) * sizeof(THING2) * (COUNT)
+	array3_size(COUNT, sizeof(TYPE1), sizeof(THING2))
  , ...)
)

// 3-factor product, only identifiers, with redundant parens removed.
@@
identifier STRIDE, SIZE, COUNT;
@@

(
  kmalloc(
-	(COUNT) * STRIDE * SIZE
+	array3_size(COUNT, STRIDE, SIZE)
  , ...)
|
  kmalloc(
-	COUNT * (STRIDE) * SIZE
+	array3_size(COUNT, STRIDE, SIZE)
  , ...)
|
  kmalloc(
-	COUNT * STRIDE * (SIZE)
+	array3_size(COUNT, STRIDE, SIZE)
  , ...)
|
  kmalloc(
-	(COUNT) * (STRIDE) * SIZE
+	array3_size(COUNT, STRIDE, SIZE)
  , ...)
|
  kmalloc(
-	COUNT * (STRIDE) * (SIZE)
+	array3_size(COUNT, STRIDE, SIZE)
  , ...)
|
  kmalloc(
-	(COUNT) * STRIDE * (SIZE)
+	array3_size(COUNT, STRIDE, SIZE)
  , ...)
|
  kmalloc(
-	(COUNT) * (STRIDE) * (SIZE)
+	array3_size(COUNT, STRIDE, SIZE)
  , ...)
|
  kmalloc(
-	COUNT * STRIDE * SIZE
+	array3_size(COUNT, STRIDE, SIZE)
  , ...)
)

// Any remaining multi-factor products, first at least 3-factor products,
// when they're not all constants...
@@
expression E1, E2, E3;
constant C1, C2, C3;
@@

(
  kmalloc(C1 * C2 * C3, ...)
|
  kmalloc(
-	(E1) * E2 * E3
+	array3_size(E1, E2, E3)
  , ...)
|
  kmalloc(
-	(E1) * (E2) * E3
+	array3_size(E1, E2, E3)
  , ...)
|
  kmalloc(
-	(E1) * (E2) * (E3)
+	array3_size(E1, E2, E3)
  , ...)
|
  kmalloc(
-	E1 * E2 * E3
+	array3_size(E1, E2, E3)
  , ...)
)

// And then all remaining 2 factors products when they're not all constants,
// keeping sizeof() as the second factor argument.
@@
expression THING, E1, E2;
type TYPE;
constant C1, C2, C3;
@@

(
  kmalloc(sizeof(THING) * C2, ...)
|
  kmalloc(sizeof(TYPE) * C2, ...)
|
  kmalloc(C1 * C2 * C3, ...)
|
  kmalloc(C1 * C2, ...)
|
- kmalloc
+ kmalloc_array
  (
-	sizeof(TYPE) * (E2)
+	E2, sizeof(TYPE)
  , ...)
|
- kmalloc
+ kmalloc_array
  (
-	sizeof(TYPE) * E2
+	E2, sizeof(TYPE)
  , ...)
|
- kmalloc
+ kmalloc_array
  (
-	sizeof(THING) * (E2)
+	E2, sizeof(THING)
  , ...)
|
- kmalloc
+ kmalloc_array
  (
-	sizeof(THING) * E2
+	E2, sizeof(THING)
  , ...)
|
- kmalloc
+ kmalloc_array
  (
-	(E1) * E2
+	E1, E2
  , ...)
|
- kmalloc
+ kmalloc_array
  (
-	(E1) * (E2)
+	E1, E2
  , ...)
|
- kmalloc
+ kmalloc_array
  (
-	E1 * E2
+	E1, E2
  , ...)
)

Signed-off-by: Kees Cook <keescook@chromium.org>
2018-06-12 16:19:22 -07:00
..
gl860 media: gspca: Convert PDEBUG to gspca_dbg 2017-12-08 10:10:15 -05:00
m5602 media: gspca: convert to vb2 2018-05-28 15:57:17 -04:00
stv06xx media: replace all <spaces><tab> occurrences 2018-01-04 13:15:05 -05:00
autogain_functions.c media: fix usage of whitespaces and on indentation 2018-01-04 13:12:01 -05:00
benq.c media: gspca: Convert PDEBUG to gspca_dbg 2017-12-08 10:10:15 -05:00
conex.c media: gspca: Convert PDEBUG to gspca_dbg 2017-12-08 10:10:15 -05:00
cpia1.c media: fix usage of whitespaces and on indentation 2018-01-04 13:12:01 -05:00
dtcs033.c media: gspca: dtcs033: use %*ph to print small buffer 2018-02-26 08:14:47 -05:00
etoms.c media: gspca: Convert PDEBUG to gspca_dbg 2017-12-08 10:10:15 -05:00
finepix.c media: gspca: Convert PDEBUG to gspca_dbg 2017-12-08 10:10:15 -05:00
gspca.c media: gspca: Kill all URBs before releasing any of them 2018-05-28 16:05:03 -04:00
gspca.h media: gspca: convert to vb2 2018-05-28 15:57:17 -04:00
jeilinj.c media: gspca: Convert PDEBUG to gspca_dbg 2017-12-08 10:10:15 -05:00
jl2005bcd.c media: gspca: Stop using GFP_DMA for buffers for USB bulk transfers 2018-05-05 11:42:43 -04:00
jpeg.h
Kconfig media: gspca: convert to vb2 2018-05-28 15:57:17 -04:00
kinect.c media: gspca: Convert PDEBUG to gspca_dbg 2017-12-08 10:10:15 -05:00
konica.c media: gspca: Convert PDEBUG to gspca_dbg 2017-12-08 10:10:15 -05:00
Makefile
mars.c media: gspca: Convert PDEBUG to gspca_dbg 2017-12-08 10:10:15 -05:00
mr97310a.c media: gspca: Convert PDEBUG to gspca_dbg 2017-12-08 10:10:15 -05:00
nw80x.c media: gspca: Convert PDEBUG to gspca_dbg 2017-12-08 10:10:15 -05:00
ov519.c media: gspca: Convert PDEBUG to gspca_dbg 2017-12-08 10:10:15 -05:00
ov534.c media: gspca: fix g/s_parm handling 2018-05-28 16:02:45 -04:00
ov534_9.c media: gspca: Convert PDEBUG to gspca_dbg 2017-12-08 10:10:15 -05:00
pac207.c media: gspca: Convert PDEBUG to gspca_dbg 2017-12-08 10:10:15 -05:00
pac7302.c media: gspca: Convert PERR to gspca_err 2017-12-08 10:08:50 -05:00
pac7311.c media: gspca: Convert PERR to gspca_err 2017-12-08 10:08:50 -05:00
pac_common.h media: gspca: Convert PDEBUG to gspca_dbg 2017-12-08 10:10:15 -05:00
se401.c
se401.h
sn9c20x.c media: gspca: Convert PDEBUG to gspca_dbg 2017-12-08 10:10:15 -05:00
sn9c2028.c media: gspca: Convert PDEBUG to gspca_dbg 2017-12-08 10:10:15 -05:00
sn9c2028.h media: gspca: Convert PDEBUG to gspca_dbg 2017-12-08 10:10:15 -05:00
sonixb.c
sonixj.c media: gspca: Convert PDEBUG to gspca_dbg 2017-12-08 10:10:15 -05:00
spca500.c media: gspca: Convert PDEBUG to gspca_dbg 2017-12-08 10:10:15 -05:00
spca501.c media: gspca: Convert PDEBUG to gspca_dbg 2017-12-08 10:10:15 -05:00
spca505.c media: gspca: Convert PDEBUG to gspca_dbg 2017-12-08 10:10:15 -05:00
spca506.c media: gspca: Convert PDEBUG to gspca_dbg 2017-12-08 10:10:15 -05:00
spca508.c media: gspca: Convert PDEBUG to gspca_dbg 2017-12-08 10:10:15 -05:00
spca561.c media: gspca: Convert PDEBUG to gspca_dbg 2017-12-08 10:10:15 -05:00
spca1528.c media: gspca: Convert PDEBUG to gspca_dbg 2017-12-08 10:10:15 -05:00
sq905.c media: gspca: Stop using GFP_DMA for buffers for USB bulk transfers 2018-05-05 11:42:43 -04:00
sq905c.c media: gspca: Stop using GFP_DMA for buffers for USB bulk transfers 2018-05-05 11:42:43 -04:00
sq930x.c media: gspca: Convert PDEBUG to gspca_dbg 2017-12-08 10:10:15 -05:00
stk014.c media: gspca: Convert PDEBUG to gspca_dbg 2017-12-08 10:10:15 -05:00
stk1135.c media: gspca: Convert PDEBUG to gspca_dbg 2017-12-08 10:10:15 -05:00
stk1135.h
stv0680.c media: gspca: Convert PDEBUG to gspca_dbg 2017-12-08 10:10:15 -05:00
sunplus.c media: gspca: Convert PDEBUG to gspca_dbg 2017-12-08 10:10:15 -05:00
t613.c treewide: kmalloc() -> kmalloc_array() 2018-06-12 16:19:22 -07:00
topro.c media: gspca: fix g/s_parm handling 2018-05-28 16:02:45 -04:00
touptek.c media: gspca: Convert PDEBUG to gspca_dbg 2017-12-08 10:10:15 -05:00
tv8532.c
vc032x.c media: gspca: convert to vb2 2018-05-28 15:57:17 -04:00
vicam.c media: gspca: Stop using GFP_DMA for buffers for USB bulk transfers 2018-05-05 11:42:43 -04:00
w996Xcf.c media: gspca: Convert PDEBUG to gspca_dbg 2017-12-08 10:10:15 -05:00
xirlink_cit.c media: gspca: Convert PDEBUG to gspca_dbg 2017-12-08 10:10:15 -05:00
zc3xx-reg.h MAINTAINERS & files: Canonize the e-mails I use at files 2018-05-04 06:21:06 -04:00
zc3xx.c media: gspca_zc3xx: Enable short exposure times for OV7648 2018-05-28 16:35:41 -04:00