linux/drivers/usb/gadget/udc
Kees Cook a86854d0c5 treewide: devm_kzalloc() -> devm_kcalloc()
The devm_kzalloc() function has a 2-factor argument form, devm_kcalloc().
This patch replaces cases of:

        devm_kzalloc(handle, a * b, gfp)

with:
        devm_kcalloc(handle, a * b, gfp)

as well as handling cases of:

        devm_kzalloc(handle, a * b * c, gfp)

with:

        devm_kzalloc(handle, array3_size(a, b, c), gfp)

as it's slightly less ugly than:

        devm_kcalloc(handle, array_size(a, b), c, gfp)

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

        devm_kzalloc(handle, 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.

Some manual whitespace fixes were needed in this patch, as Coccinelle
really liked to write "=devm_kcalloc..." instead of "= devm_kcalloc...".

The Coccinelle script used for this was:

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

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

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

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

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

(
- devm_kzalloc
+ devm_kcalloc
  (HANDLE,
-	sizeof(TYPE) * (COUNT_ID)
+	COUNT_ID, sizeof(TYPE)
  , ...)
|
- devm_kzalloc
+ devm_kcalloc
  (HANDLE,
-	sizeof(TYPE) * COUNT_ID
+	COUNT_ID, sizeof(TYPE)
  , ...)
|
- devm_kzalloc
+ devm_kcalloc
  (HANDLE,
-	sizeof(TYPE) * (COUNT_CONST)
+	COUNT_CONST, sizeof(TYPE)
  , ...)
|
- devm_kzalloc
+ devm_kcalloc
  (HANDLE,
-	sizeof(TYPE) * COUNT_CONST
+	COUNT_CONST, sizeof(TYPE)
  , ...)
|
- devm_kzalloc
+ devm_kcalloc
  (HANDLE,
-	sizeof(THING) * (COUNT_ID)
+	COUNT_ID, sizeof(THING)
  , ...)
|
- devm_kzalloc
+ devm_kcalloc
  (HANDLE,
-	sizeof(THING) * COUNT_ID
+	COUNT_ID, sizeof(THING)
  , ...)
|
- devm_kzalloc
+ devm_kcalloc
  (HANDLE,
-	sizeof(THING) * (COUNT_CONST)
+	COUNT_CONST, sizeof(THING)
  , ...)
|
- devm_kzalloc
+ devm_kcalloc
  (HANDLE,
-	sizeof(THING) * COUNT_CONST
+	COUNT_CONST, sizeof(THING)
  , ...)
)

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

- devm_kzalloc
+ devm_kcalloc
  (HANDLE,
-	SIZE * COUNT
+	COUNT, SIZE
  , ...)

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

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

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

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

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

(
  devm_kzalloc(HANDLE,
-	(COUNT) * STRIDE * SIZE
+	array3_size(COUNT, STRIDE, SIZE)
  , ...)
|
  devm_kzalloc(HANDLE,
-	COUNT * (STRIDE) * SIZE
+	array3_size(COUNT, STRIDE, SIZE)
  , ...)
|
  devm_kzalloc(HANDLE,
-	COUNT * STRIDE * (SIZE)
+	array3_size(COUNT, STRIDE, SIZE)
  , ...)
|
  devm_kzalloc(HANDLE,
-	(COUNT) * (STRIDE) * SIZE
+	array3_size(COUNT, STRIDE, SIZE)
  , ...)
|
  devm_kzalloc(HANDLE,
-	COUNT * (STRIDE) * (SIZE)
+	array3_size(COUNT, STRIDE, SIZE)
  , ...)
|
  devm_kzalloc(HANDLE,
-	(COUNT) * STRIDE * (SIZE)
+	array3_size(COUNT, STRIDE, SIZE)
  , ...)
|
  devm_kzalloc(HANDLE,
-	(COUNT) * (STRIDE) * (SIZE)
+	array3_size(COUNT, STRIDE, SIZE)
  , ...)
|
  devm_kzalloc(HANDLE,
-	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 HANDLE;
expression E1, E2, E3;
constant C1, C2, C3;
@@

(
  devm_kzalloc(HANDLE, C1 * C2 * C3, ...)
|
  devm_kzalloc(HANDLE,
-	(E1) * E2 * E3
+	array3_size(E1, E2, E3)
  , ...)
|
  devm_kzalloc(HANDLE,
-	(E1) * (E2) * E3
+	array3_size(E1, E2, E3)
  , ...)
|
  devm_kzalloc(HANDLE,
-	(E1) * (E2) * (E3)
+	array3_size(E1, E2, E3)
  , ...)
|
  devm_kzalloc(HANDLE,
-	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 HANDLE;
expression THING, E1, E2;
type TYPE;
constant C1, C2, C3;
@@

(
  devm_kzalloc(HANDLE, sizeof(THING) * C2, ...)
|
  devm_kzalloc(HANDLE, sizeof(TYPE) * C2, ...)
|
  devm_kzalloc(HANDLE, C1 * C2 * C3, ...)
|
  devm_kzalloc(HANDLE, C1 * C2, ...)
|
- devm_kzalloc
+ devm_kcalloc
  (HANDLE,
-	sizeof(TYPE) * (E2)
+	E2, sizeof(TYPE)
  , ...)
|
- devm_kzalloc
+ devm_kcalloc
  (HANDLE,
-	sizeof(TYPE) * E2
+	E2, sizeof(TYPE)
  , ...)
|
- devm_kzalloc
+ devm_kcalloc
  (HANDLE,
-	sizeof(THING) * (E2)
+	E2, sizeof(THING)
  , ...)
|
- devm_kzalloc
+ devm_kcalloc
  (HANDLE,
-	sizeof(THING) * E2
+	E2, sizeof(THING)
  , ...)
|
- devm_kzalloc
+ devm_kcalloc
  (HANDLE,
-	(E1) * E2
+	E1, E2
  , ...)
|
- devm_kzalloc
+ devm_kcalloc
  (HANDLE,
-	(E1) * (E2)
+	E1, E2
  , ...)
|
- devm_kzalloc
+ devm_kcalloc
  (HANDLE,
-	E1 * E2
+	E1, E2
  , ...)
)

Signed-off-by: Kees Cook <keescook@chromium.org>
2018-06-12 16:19:22 -07:00
..
aspeed-vhub usb/gadget: Add driver for Aspeed SoC virtual hub 2018-05-15 10:06:53 +03:00
bdc treewide: kzalloc() -> kcalloc() 2018-06-12 16:19:22 -07:00
Kconfig usb: changes for v4.18 merge window 2018-05-24 17:46:53 +02:00
Makefile usb/gadget: Add driver for Aspeed SoC virtual hub 2018-05-15 10:06:53 +03:00
amd5536udc.h USB: gadget: udc: Remove redundant license text 2017-11-07 15:45:02 +01:00
amd5536udc_pci.c USB: gadget: udc: Remove redundant license text 2017-11-07 15:45:02 +01:00
at91_udc.c proc: introduce proc_create_single{,_data} 2018-05-16 07:23:35 +02:00
at91_udc.h USB: gadget: udc: Remove redundant license text 2017-11-07 15:45:02 +01:00
atmel_usba_udc.c treewide: devm_kzalloc() -> devm_kcalloc() 2018-06-12 16:19:22 -07:00
atmel_usba_udc.h USB: udc: atmel_usba_udc: no need to check return value of debugfs_create functions 2018-05-31 12:54:22 +02:00
bcm63xx_udc.c USB: gadget: udc: bcm63xx_udc: no need to check return value of debugfs_create functions 2018-05-31 12:54:22 +02:00
core.c usb: gadget: udc: core: Document the relation between usb_ep_queue() and completion callback 2018-04-23 14:07:36 +03:00
dummy_hcd.c usb: gadget: udc: Use scnprintf() instead of snprintf() 2018-03-13 10:47:51 +02:00
fotg210-udc.c USB: gadget: udc: Remove redundant license text 2017-11-07 15:45:02 +01:00
fotg210.h USB: gadget: udc: Remove redundant license text 2017-11-07 15:45:02 +01:00
fsl_mxc_udc.c USB: gadget: udc: Remove redundant license text 2017-11-07 15:45:02 +01:00
fsl_qe_udc.c USB: gadget: udc: Remove redundant license text 2017-11-07 15:45:02 +01:00
fsl_qe_udc.h USB: gadget: udc: Remove redundant license text 2017-11-07 15:45:02 +01:00
fsl_udc_core.c treewide: kzalloc() -> kcalloc() 2018-06-12 16:19:22 -07:00
fsl_usb2_udc.h USB: gadget: udc: Remove redundant license text 2017-11-07 15:45:02 +01:00
fusb300_udc.c USB: gadget: udc: Remove redundant license text 2017-11-07 15:45:02 +01:00
fusb300_udc.h USB: gadget: udc: Remove redundant license text 2017-11-07 15:45:02 +01:00
goku_udc.c proc: introduce proc_create_single{,_data} 2018-05-16 07:23:35 +02:00
goku_udc.h usb: gadget: udc: change comparison to bitshift when dealing with a mask 2018-03-08 15:12:00 +02:00
gr_udc.c USB: gadget: udc: gr_udc: no need to check return value of debugfs_create functions 2018-05-31 12:54:22 +02:00
gr_udc.h USB: gadget: udc: gr_udc: no need to check return value of debugfs_create functions 2018-05-31 12:54:22 +02:00
lpc32xx_udc.c USB: gadget: udc: Remove redundant license text 2017-11-07 15:45:02 +01:00
m66592-udc.c treewide: setup_timer() -> timer_setup() 2017-11-21 15:57:07 -08:00
m66592-udc.h USB: gadget: udc: Remove redundant license text 2017-11-07 15:45:02 +01:00
mv_u3d.h USB: gadget: udc: Remove redundant license text 2017-11-07 15:45:02 +01:00
mv_u3d_core.c USB: gadget: udc: Remove redundant license text 2017-11-07 15:45:02 +01:00
mv_udc.h USB: gadget: udc: Remove redundant license text 2017-11-07 15:45:02 +01:00
mv_udc_core.c USB: gadget: udc: Remove redundant license text 2017-11-07 15:45:02 +01:00
net2272.c USB: gadget: udc: Remove redundant license text 2017-11-07 15:45:02 +01:00
net2272.h USB: gadget: udc: Remove redundant license text 2017-11-07 15:45:02 +01:00
net2280.c USB: gadget: udc: Remove redundant license text 2017-11-07 15:45:02 +01:00
net2280.h USB: gadget: udc: Remove redundant license text 2017-11-07 15:45:02 +01:00
omap_udc.c proc: introduce proc_create_single{,_data} 2018-05-16 07:23:35 +02:00
omap_udc.h License cleanup: add SPDX GPL-2.0 license identifier to files with no license 2017-11-02 11:10:55 +01:00
pch_udc.c USB: gadget: udc: Remove redundant license text 2017-11-07 15:45:02 +01:00
pxa25x_udc.c USB: gadget: pxa25x: Re-use DEFINE_SHOW_ATTRIBUTE() macro 2018-03-08 15:12:00 +02:00
pxa25x_udc.h USB: gadget: udc: Remove redundant license text 2017-11-07 15:45:02 +01:00
pxa27x_udc.c USB: gadget: udc: pxa27x_udc: no need to check return value of debugfs_create functions 2018-05-31 12:54:22 +02:00
pxa27x_udc.h USB: gadget: udc: pxa27x_udc: no need to check return value of debugfs_create functions 2018-05-31 12:54:22 +02:00
r8a66597-udc.c treewide: setup_timer() -> timer_setup() 2017-11-21 15:57:07 -08:00
r8a66597-udc.h USB: gadget: udc: Remove redundant license text 2017-11-07 15:45:02 +01:00
renesas_usb3.c treewide: devm_kzalloc() -> devm_kcalloc() 2018-06-12 16:19:22 -07:00
s3c-hsudc.c USB: gadget: udc: Remove redundant license text 2017-11-07 15:45:02 +01:00
s3c2410_udc.c USB: gadget: udc: s3c2410_udc: no need to check return value of debugfs_create functions 2018-05-31 12:54:23 +02:00
s3c2410_udc.h USB: gadget: udc: Remove redundant license text 2017-11-07 15:45:02 +01:00
snps_udc_core.c USB: gadget: udc: Remove redundant license text 2017-11-07 15:45:02 +01:00
snps_udc_plat.c USB: gadget: udc: Remove redundant license text 2017-11-07 15:45:02 +01:00
trace.c USB: gadget: udc: Remove redundant license text 2017-11-07 15:45:02 +01:00
trace.h gadget event trace : add request pointer 2017-12-11 12:36:49 +02:00
udc-xilinx.c usb: gadget: udc-xilinx: remove redundant pointer udc 2017-12-11 12:36:51 +02:00