linux/drivers/staging
Kees Cook fad953ce0b treewide: Use array_size() in vzalloc()
The vzalloc() function has no 2-factor argument form, so multiplication
factors need to be wrapped in array_size(). This patch replaces cases of:

        vzalloc(a * b)

with:
        vzalloc(array_size(a, b))

as well as handling cases of:

        vzalloc(a * b * c)

with:

        vzalloc(array3_size(a, b, c))

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

        vzalloc(4 * 1024)

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 Coccinelle script used for this was:

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

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

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

(
  vzalloc(
-	sizeof(u8) * (COUNT)
+	COUNT
  , ...)
|
  vzalloc(
-	sizeof(__u8) * (COUNT)
+	COUNT
  , ...)
|
  vzalloc(
-	sizeof(char) * (COUNT)
+	COUNT
  , ...)
|
  vzalloc(
-	sizeof(unsigned char) * (COUNT)
+	COUNT
  , ...)
|
  vzalloc(
-	sizeof(u8) * COUNT
+	COUNT
  , ...)
|
  vzalloc(
-	sizeof(__u8) * COUNT
+	COUNT
  , ...)
|
  vzalloc(
-	sizeof(char) * COUNT
+	COUNT
  , ...)
|
  vzalloc(
-	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;
@@

(
  vzalloc(
-	sizeof(TYPE) * (COUNT_ID)
+	array_size(COUNT_ID, sizeof(TYPE))
  , ...)
|
  vzalloc(
-	sizeof(TYPE) * COUNT_ID
+	array_size(COUNT_ID, sizeof(TYPE))
  , ...)
|
  vzalloc(
-	sizeof(TYPE) * (COUNT_CONST)
+	array_size(COUNT_CONST, sizeof(TYPE))
  , ...)
|
  vzalloc(
-	sizeof(TYPE) * COUNT_CONST
+	array_size(COUNT_CONST, sizeof(TYPE))
  , ...)
|
  vzalloc(
-	sizeof(THING) * (COUNT_ID)
+	array_size(COUNT_ID, sizeof(THING))
  , ...)
|
  vzalloc(
-	sizeof(THING) * COUNT_ID
+	array_size(COUNT_ID, sizeof(THING))
  , ...)
|
  vzalloc(
-	sizeof(THING) * (COUNT_CONST)
+	array_size(COUNT_CONST, sizeof(THING))
  , ...)
|
  vzalloc(
-	sizeof(THING) * COUNT_CONST
+	array_size(COUNT_CONST, sizeof(THING))
  , ...)
)

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

  vzalloc(
-	SIZE * COUNT
+	array_size(COUNT, SIZE)
  , ...)

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

(
  vzalloc(
-	sizeof(TYPE) * (COUNT) * (STRIDE)
+	array3_size(COUNT, STRIDE, sizeof(TYPE))
  , ...)
|
  vzalloc(
-	sizeof(TYPE) * (COUNT) * STRIDE
+	array3_size(COUNT, STRIDE, sizeof(TYPE))
  , ...)
|
  vzalloc(
-	sizeof(TYPE) * COUNT * (STRIDE)
+	array3_size(COUNT, STRIDE, sizeof(TYPE))
  , ...)
|
  vzalloc(
-	sizeof(TYPE) * COUNT * STRIDE
+	array3_size(COUNT, STRIDE, sizeof(TYPE))
  , ...)
|
  vzalloc(
-	sizeof(THING) * (COUNT) * (STRIDE)
+	array3_size(COUNT, STRIDE, sizeof(THING))
  , ...)
|
  vzalloc(
-	sizeof(THING) * (COUNT) * STRIDE
+	array3_size(COUNT, STRIDE, sizeof(THING))
  , ...)
|
  vzalloc(
-	sizeof(THING) * COUNT * (STRIDE)
+	array3_size(COUNT, STRIDE, sizeof(THING))
  , ...)
|
  vzalloc(
-	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;
@@

(
  vzalloc(
-	sizeof(TYPE1) * sizeof(TYPE2) * COUNT
+	array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2))
  , ...)
|
  vzalloc(
-	sizeof(TYPE1) * sizeof(THING2) * (COUNT)
+	array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2))
  , ...)
|
  vzalloc(
-	sizeof(THING1) * sizeof(THING2) * COUNT
+	array3_size(COUNT, sizeof(THING1), sizeof(THING2))
  , ...)
|
  vzalloc(
-	sizeof(THING1) * sizeof(THING2) * (COUNT)
+	array3_size(COUNT, sizeof(THING1), sizeof(THING2))
  , ...)
|
  vzalloc(
-	sizeof(TYPE1) * sizeof(THING2) * COUNT
+	array3_size(COUNT, sizeof(TYPE1), sizeof(THING2))
  , ...)
|
  vzalloc(
-	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;
@@

(
  vzalloc(
-	(COUNT) * STRIDE * SIZE
+	array3_size(COUNT, STRIDE, SIZE)
  , ...)
|
  vzalloc(
-	COUNT * (STRIDE) * SIZE
+	array3_size(COUNT, STRIDE, SIZE)
  , ...)
|
  vzalloc(
-	COUNT * STRIDE * (SIZE)
+	array3_size(COUNT, STRIDE, SIZE)
  , ...)
|
  vzalloc(
-	(COUNT) * (STRIDE) * SIZE
+	array3_size(COUNT, STRIDE, SIZE)
  , ...)
|
  vzalloc(
-	COUNT * (STRIDE) * (SIZE)
+	array3_size(COUNT, STRIDE, SIZE)
  , ...)
|
  vzalloc(
-	(COUNT) * STRIDE * (SIZE)
+	array3_size(COUNT, STRIDE, SIZE)
  , ...)
|
  vzalloc(
-	(COUNT) * (STRIDE) * (SIZE)
+	array3_size(COUNT, STRIDE, SIZE)
  , ...)
|
  vzalloc(
-	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;
@@

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

// And then all remaining 2 factors products when they're not all constants.
@@
expression E1, E2;
constant C1, C2;
@@

(
  vzalloc(C1 * C2, ...)
|
  vzalloc(
-	E1 * E2
+	array_size(E1, E2)
  , ...)
)

Signed-off-by: Kees Cook <keescook@chromium.org>
2018-06-12 16:19:22 -07:00
..
android treewide: Use array_size() in vmalloc() 2018-06-12 16:19:22 -07:00
board staging: board: Replace license boilerplate with SPDX identifiers 2018-05-06 19:11:23 -07:00
clocking-wizard
comedi Staging/IIO patches for 4.18-rc1 2018-06-09 10:32:39 -07:00
dgnc
emxx_udc
fbtft - Core Frameworks 2018-06-11 07:23:19 -07:00
fsl-dpaa2 Staging/IIO patches for 4.18-rc1 2018-06-09 10:32:39 -07:00
fsl-mc staging: fsl-mc/dpio: Fix the error handling in probe() 2018-04-29 15:25:08 +02:00
fwserial tty: replace ->proc_fops with ->proc_show 2018-05-16 07:24:30 +02:00
gdm724x staging: gdm724x: remove redundant license information 2018-04-26 09:17:34 +02:00
goldfish Staging: goldfish: Kconfig: fixed code style issue 2018-05-20 14:31:28 +02:00
greybus treewide: Use array_size() in vmalloc() 2018-06-12 16:19:22 -07:00
gs_fpgaboot
iio staging:iio:meter: Drop ADE7759 driver 2018-05-20 12:21:54 +01:00
ks7010 staging: ks7010: avoid casting inside cpu_to_* assignments 2018-05-20 14:47:14 +02:00
media treewide: Use array_size() in vmalloc() 2018-06-12 16:19:22 -07:00
most staging: most: video: fix build warnings 2018-05-14 11:42:28 +02:00
mt29f_spinand
mt7621-dma
mt7621-dts staging: mt7621-gpio: update #interrupt-cells for the gpio node 2018-06-01 13:16:23 +02:00
mt7621-eth staging: mt7621-eth: Remove unused variable 2018-05-15 09:31:38 +02:00
mt7621-gpio staging: mt7621-gpio: reorder includes alphabetically 2018-06-01 13:16:24 +02:00
mt7621-mmc staging: mt7621-mmc: Fix line size exceeding 80 columns 2018-06-01 10:52:48 +02:00
mt7621-pci staging: mt7621-pci: Fix line size exceeding 80 columns. 2018-05-31 19:00:53 +02:00
mt7621-pinctrl treewide: devm_kzalloc() -> devm_kcalloc() 2018-06-12 16:19:22 -07:00
mt7621-spi staging: mt7621-spi: remove unused lock. 2018-05-06 19:09:23 -07:00
netlogic
nvec
octeon
octeon-usb
olpc_dcon
pi433
rtl8188eu treewide: Use array_size() in vzalloc() 2018-06-12 16:19:22 -07:00
rtl8192e staging: rtl8192e: rtllib_tx: fix spelling issue. 2018-05-25 18:44:14 +02:00
rtl8192u treewide: kmalloc() -> kmalloc_array() 2018-06-12 16:19:22 -07:00
rtl8712
rtl8723bs treewide: Use array_size() in vzalloc() 2018-06-12 16:19:22 -07:00
rtlwifi treewide: kzalloc() -> kcalloc() 2018-06-12 16:19:22 -07:00
rts5208 treewide: Use array_size() in vzalloc() 2018-06-12 16:19:22 -07:00
skein
sm750fb staging: sm750fb: add drm development branch details 2018-05-06 18:51:38 -07:00
speakup staging: speakup: use true/false instead of 1/0 2018-05-15 09:31:38 +02:00
typec
unisys treewide: kzalloc() -> kcalloc() 2018-06-12 16:19:22 -07:00
vboxvideo Staging/IIO patches for 4.18-rc1 2018-06-09 10:32:39 -07:00
vc04_services staging: vc04_services: no need to save the log debufs dentries 2018-06-01 20:15:56 +02:00
vme
vt6655
vt6656
wilc1000 staging: wilc1000: Avoid overriding rates_no while parsing ies element. 2018-05-25 18:45:47 +02:00
wlan-ng staging: wlan-ng: remove unused declarations from p80211types.h 2018-05-31 19:00:53 +02:00
xgifb
Kconfig staging: ipx: delete it from the tree 2018-06-05 19:23:26 +02:00
Makefile staging: ipx: delete it from the tree 2018-06-05 19:23:26 +02:00