Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

media: media/usb: Use kmemdup rather than duplicating its implementation

kmemdup is introduced to duplicate a region of memory in a neat way.
Rather than kmalloc/kzalloc + memcpy, which the programmer needs to
write the size twice (sometimes lead to mistakes), kmemdup improves
readability, leads to smaller code and also reduce the chances of mistakes.
Suggestion to use kmemdup rather than using kmalloc/kzalloc + memcpy.

Signed-off-by: Fuqian Huang <huangfq.daxian@gmail.com>
Signed-off-by: Sean Young <sean@mess.org>
Signed-off-by: Mauro Carvalho Chehab <mchehab+samsung@kernel.org>

authored by

Fuqian Huang and committed by
Mauro Carvalho Chehab
771560e5 0dc99e04

+2 -5
+1 -2
drivers/media/usb/em28xx/em28xx-cards.c
··· 3566 3566 static int em28xx_duplicate_dev(struct em28xx *dev) 3567 3567 { 3568 3568 int nr; 3569 - struct em28xx *sec_dev = kzalloc(sizeof(*sec_dev), GFP_KERNEL); 3569 + struct em28xx *sec_dev = kmemdup(dev, sizeof(*sec_dev), GFP_KERNEL); 3570 3570 3571 3571 if (!sec_dev) { 3572 3572 dev->dev_next = NULL; 3573 3573 return -ENOMEM; 3574 3574 } 3575 - memcpy(sec_dev, dev, sizeof(*sec_dev)); 3576 3575 /* Check to see next free device and mark as used */ 3577 3576 do { 3578 3577 nr = find_first_zero_bit(em28xx_devused, EM28XX_MAXBOARDS);
+1 -3
drivers/media/usb/zr364xx/zr364xx.c
··· 197 197 { 198 198 int status; 199 199 200 - unsigned char *transfer_buffer = kmalloc(size, GFP_KERNEL); 200 + unsigned char *transfer_buffer = kmemdup(cp, size, GFP_KERNEL); 201 201 if (!transfer_buffer) 202 202 return -ENOMEM; 203 - 204 - memcpy(transfer_buffer, cp, size); 205 203 206 204 status = usb_control_msg(udev, 207 205 usb_sndctrlpipe(udev, 0),