media: media/tuners: Use kmemdup rather than duplicating its implementation
authorFuqian Huang <huangfq.daxian@gmail.com>
Wed, 3 Jul 2019 16:28:48 +0000 (13:28 -0300)
committerMauro Carvalho Chehab <mchehab+samsung@kernel.org>
Wed, 14 Aug 2019 08:03:11 +0000 (05:03 -0300)
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>
drivers/media/tuners/tuner-xc2028.c
drivers/media/tuners/xc4000.c

index aa6861dcd3fd78fd0df75c358b1149564564371a..574c3bb135d7250f8c10144d8cf3f2b0ac8e8ab3 100644 (file)
@@ -381,7 +381,7 @@ static int load_all_firmwares(struct dvb_frontend *fe,
                        goto corrupt;
                }
 
-               priv->firm[n].ptr = kzalloc(size, GFP_KERNEL);
+               priv->firm[n].ptr = kmemdup(p, size, GFP_KERNEL);
                if (priv->firm[n].ptr == NULL) {
                        tuner_err("Not enough memory to load firmware file.\n");
                        rc = -ENOMEM;
@@ -394,7 +394,6 @@ static int load_all_firmwares(struct dvb_frontend *fe,
                               type, (unsigned long long)id, size);
                }
 
-               memcpy(priv->firm[n].ptr, p, size);
                priv->firm[n].type = type;
                priv->firm[n].id   = id;
                priv->firm[n].size = size;
index 43925e219d8152f78b3c31d4967c36c6e4a64c54..d9606738ce432ff8cbf8455a2b48698f77b7da4d 100644 (file)
@@ -812,7 +812,7 @@ static int xc4000_fwupload(struct dvb_frontend *fe)
                        goto corrupt;
                }
 
-               priv->firm[n].ptr = kzalloc(size, GFP_KERNEL);
+               priv->firm[n].ptr = kmemdup(p, size, GFP_KERNEL);
                if (priv->firm[n].ptr == NULL) {
                        printk(KERN_ERR "Not enough memory to load firmware file.\n");
                        rc = -ENOMEM;
@@ -826,7 +826,6 @@ static int xc4000_fwupload(struct dvb_frontend *fe)
                               type, (unsigned long long)id, size);
                }
 
-               memcpy(priv->firm[n].ptr, p, size);
                priv->firm[n].type = type;
                priv->firm[n].id   = id;
                priv->firm[n].size = size;