Skip to content

Commit

Permalink
media: media/usb: Use kmemdup rather than duplicating its implementation
Browse files Browse the repository at this point in the history
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 <[email protected]>
Signed-off-by: Sean Young <[email protected]>
Signed-off-by: Mauro Carvalho Chehab <[email protected]>
  • Loading branch information
Yellow-Pay authored and mchehab committed Aug 14, 2019
1 parent 0dc99e0 commit 771560e
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 5 deletions.
3 changes: 1 addition & 2 deletions drivers/media/usb/em28xx/em28xx-cards.c
Original file line number Diff line number Diff line change
Expand Up @@ -3566,13 +3566,12 @@ static int em28xx_init_dev(struct em28xx *dev, struct usb_device *udev,
static int em28xx_duplicate_dev(struct em28xx *dev)
{
int nr;
struct em28xx *sec_dev = kzalloc(sizeof(*sec_dev), GFP_KERNEL);
struct em28xx *sec_dev = kmemdup(dev, sizeof(*sec_dev), GFP_KERNEL);

if (!sec_dev) {
dev->dev_next = NULL;
return -ENOMEM;
}
memcpy(sec_dev, dev, sizeof(*sec_dev));
/* Check to see next free device and mark as used */
do {
nr = find_first_zero_bit(em28xx_devused, EM28XX_MAXBOARDS);
Expand Down
4 changes: 1 addition & 3 deletions drivers/media/usb/zr364xx/zr364xx.c
Original file line number Diff line number Diff line change
Expand Up @@ -197,12 +197,10 @@ static int send_control_msg(struct usb_device *udev, u8 request, u16 value,
{
int status;

unsigned char *transfer_buffer = kmalloc(size, GFP_KERNEL);
unsigned char *transfer_buffer = kmemdup(cp, size, GFP_KERNEL);
if (!transfer_buffer)
return -ENOMEM;

memcpy(transfer_buffer, cp, size);

status = usb_control_msg(udev,
usb_sndctrlpipe(udev, 0),
request,
Expand Down

0 comments on commit 771560e

Please sign in to comment.