OpenCloudOS-Kernel/drivers/media/usb/pvrusb2
Kees Cook 6396bb2215 treewide: kzalloc() -> kcalloc()
The kzalloc() function has a 2-factor argument form, kcalloc(). This
patch replaces cases of:

        kzalloc(a * b, gfp)

with:
        kcalloc(a * b, gfp)

as well as handling cases of:

        kzalloc(a * b * c, gfp)

with:

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

as it's slightly less ugly than:

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

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

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

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

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

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

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

(
- kzalloc
+ kcalloc
  (
-	sizeof(TYPE) * (COUNT_ID)
+	COUNT_ID, sizeof(TYPE)
  , ...)
|
- kzalloc
+ kcalloc
  (
-	sizeof(TYPE) * COUNT_ID
+	COUNT_ID, sizeof(TYPE)
  , ...)
|
- kzalloc
+ kcalloc
  (
-	sizeof(TYPE) * (COUNT_CONST)
+	COUNT_CONST, sizeof(TYPE)
  , ...)
|
- kzalloc
+ kcalloc
  (
-	sizeof(TYPE) * COUNT_CONST
+	COUNT_CONST, sizeof(TYPE)
  , ...)
|
- kzalloc
+ kcalloc
  (
-	sizeof(THING) * (COUNT_ID)
+	COUNT_ID, sizeof(THING)
  , ...)
|
- kzalloc
+ kcalloc
  (
-	sizeof(THING) * COUNT_ID
+	COUNT_ID, sizeof(THING)
  , ...)
|
- kzalloc
+ kcalloc
  (
-	sizeof(THING) * (COUNT_CONST)
+	COUNT_CONST, sizeof(THING)
  , ...)
|
- kzalloc
+ kcalloc
  (
-	sizeof(THING) * COUNT_CONST
+	COUNT_CONST, sizeof(THING)
  , ...)
)

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

- kzalloc
+ kcalloc
  (
-	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;
@@

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

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

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

(
  kzalloc(C1 * C2 * C3, ...)
|
  kzalloc(
-	(E1) * E2 * E3
+	array3_size(E1, E2, E3)
  , ...)
|
  kzalloc(
-	(E1) * (E2) * E3
+	array3_size(E1, E2, E3)
  , ...)
|
  kzalloc(
-	(E1) * (E2) * (E3)
+	array3_size(E1, E2, E3)
  , ...)
|
  kzalloc(
-	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;
@@

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

Signed-off-by: Kees Cook <keescook@chromium.org>
2018-06-12 16:19:22 -07:00
..
Kconfig media: fix media Kconfig help syntax issues 2017-09-23 14:59:24 -04:00
Makefile media: don't include drivers/media/i2c at cflags 2017-12-28 14:14:09 -05:00
pvrusb2-audio.c [media] media: Drop FSF's postal address from the source code files 2017-01-27 11:38:09 -02:00
pvrusb2-audio.h [media] media: Drop FSF's postal address from the source code files 2017-01-27 11:38:09 -02:00
pvrusb2-context.c [media] media: Drop FSF's postal address from the source code files 2017-01-27 11:38:09 -02:00
pvrusb2-context.h [media] media: Drop FSF's postal address from the source code files 2017-01-27 11:38:09 -02:00
pvrusb2-cs53l32a.c [media] media: Drop FSF's postal address from the source code files 2017-01-27 11:38:09 -02:00
pvrusb2-cs53l32a.h [media] media: Drop FSF's postal address from the source code files 2017-01-27 11:38:09 -02:00
pvrusb2-ctrl.c [media] media: Drop FSF's postal address from the source code files 2017-01-27 11:38:09 -02:00
pvrusb2-ctrl.h [media] media: Drop FSF's postal address from the source code files 2017-01-27 11:38:09 -02:00
pvrusb2-cx2584x-v4l.c media: pvrusb2: delete unneeded include 2018-05-09 16:28:19 -04:00
pvrusb2-cx2584x-v4l.h [media] media: Drop FSF's postal address from the source code files 2017-01-27 11:38:09 -02:00
pvrusb2-debug.h [media] media: Drop FSF's postal address from the source code files 2017-01-27 11:38:09 -02:00
pvrusb2-debugifc.c [media] media: Drop FSF's postal address from the source code files 2017-01-27 11:38:09 -02:00
pvrusb2-debugifc.h [media] media: Drop FSF's postal address from the source code files 2017-01-27 11:38:09 -02:00
pvrusb2-devattr.c media: lgdt330x: convert it to the new I2C binding way 2018-05-04 11:09:26 -04:00
pvrusb2-devattr.h [media] media: Drop FSF's postal address from the source code files 2017-01-27 11:38:09 -02:00
pvrusb2-dvb.c media: move dvb kAPI headers to include/media 2017-12-28 13:16:01 -05:00
pvrusb2-dvb.h media: move dvb kAPI headers to include/media 2017-12-28 13:16:01 -05:00
pvrusb2-eeprom.c [media] tveeprom: get rid of unused arg on tveeprom_hauppauge_analog() 2017-03-03 07:35:02 -03:00
pvrusb2-eeprom.h [media] media: Drop FSF's postal address from the source code files 2017-01-27 11:38:09 -02:00
pvrusb2-encoder.c media: pvrusb2: fix the retry logic 2017-07-20 16:25:41 -04:00
pvrusb2-encoder.h [media] media: Drop FSF's postal address from the source code files 2017-01-27 11:38:09 -02:00
pvrusb2-fx2-cmd.h [media] media: Drop FSF's postal address from the source code files 2017-01-27 11:38:09 -02:00
pvrusb2-hdw-internal.h [media] media: Drop FSF's postal address from the source code files 2017-01-27 11:38:09 -02:00
pvrusb2-hdw.c treewide: kzalloc() -> kcalloc() 2018-06-12 16:19:22 -07:00
pvrusb2-hdw.h [media] media: Drop FSF's postal address from the source code files 2017-01-27 11:38:09 -02:00
pvrusb2-i2c-core.c media: merge ir_tx_z8f0811_haup and ir_rx_z8f0811_haup i2c devices 2017-12-14 09:58:20 -05:00
pvrusb2-i2c-core.h [media] media: Drop FSF's postal address from the source code files 2017-01-27 11:38:09 -02:00
pvrusb2-io.c [media] pvrusb2-io: Add some spaces for better code readability 2017-01-31 08:05:45 -02:00
pvrusb2-io.h [media] media: Drop FSF's postal address from the source code files 2017-01-27 11:38:09 -02:00
pvrusb2-ioread.c [media] media: Drop FSF's postal address from the source code files 2017-01-27 11:38:09 -02:00
pvrusb2-ioread.h [media] media: Drop FSF's postal address from the source code files 2017-01-27 11:38:09 -02:00
pvrusb2-main.c [media] media: Drop FSF's postal address from the source code files 2017-01-27 11:38:09 -02:00
pvrusb2-std.c treewide: kzalloc() -> kcalloc() 2018-06-12 16:19:22 -07:00
pvrusb2-std.h [media] media: Drop FSF's postal address from the source code files 2017-01-27 11:38:09 -02:00
pvrusb2-sysfs.c [media] media: Drop FSF's postal address from the source code files 2017-01-27 11:38:09 -02:00
pvrusb2-sysfs.h [media] media: Drop FSF's postal address from the source code files 2017-01-27 11:38:09 -02:00
pvrusb2-util.h [media] media: Drop FSF's postal address from the source code files 2017-01-27 11:38:09 -02:00
pvrusb2-v4l2.c media: pvrusb2: replace pvr2_v4l2_ioctl by video_ioctl2 2018-05-28 16:28:09 -04:00
pvrusb2-v4l2.h [media] media: Drop FSF's postal address from the source code files 2017-01-27 11:38:09 -02:00
pvrusb2-video-v4l.c [media] media: Drop FSF's postal address from the source code files 2017-01-27 11:38:09 -02:00
pvrusb2-video-v4l.h [media] media: Drop FSF's postal address from the source code files 2017-01-27 11:38:09 -02:00
pvrusb2-wm8775.c [media] media: Drop FSF's postal address from the source code files 2017-01-27 11:38:09 -02:00
pvrusb2-wm8775.h [media] media: Drop FSF's postal address from the source code files 2017-01-27 11:38:09 -02:00
pvrusb2.h [media] media: Drop FSF's postal address from the source code files 2017-01-27 11:38:09 -02:00