Commit 94002c07 authored by Julia Lawall's avatar Julia Lawall Committed by Greg Kroah-Hartman

Staging: Use kmemdup

Use kmemdup when some other buffer is immediately copied into the
allocated region.

A simplified version of the semantic patch that makes this change is as
follows: (http://coccinelle.lip6.fr/)

// <smpl>
@@
expression from,to,size,flag;
statement S;
@@

-  to = \(kmalloc\|kzalloc\)(size,flag);
+  to = kmemdup(from,size,flag);
   if (to==NULL || ...) S
-  memcpy(to, from, size);
// </smpl>
Signed-off-by: default avatarJulia Lawall <julia@diku.dk>
Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@suse.de>
parent 96fe9ee2
...@@ -840,13 +840,12 @@ static int firmwareUpload(struct usbduxsub *usbduxsub, ...@@ -840,13 +840,12 @@ static int firmwareUpload(struct usbduxsub *usbduxsub,
} }
/* we generate a local buffer for the firmware */ /* we generate a local buffer for the firmware */
fwBuf = kzalloc(sizeFirmware, GFP_KERNEL); fwBuf = kmemdup(firmwareBinary, sizeFirmware, GFP_KERNEL);
if (!fwBuf) { if (!fwBuf) {
dev_err(&usbduxsub->interface->dev, dev_err(&usbduxsub->interface->dev,
"comedi_: mem alloc for firmware failed\n"); "comedi_: mem alloc for firmware failed\n");
return -ENOMEM; return -ENOMEM;
} }
memcpy(fwBuf, firmwareBinary, sizeFirmware);
ret = usbduxsub_stop(usbduxsub); ret = usbduxsub_stop(usbduxsub);
if (ret < 0) { if (ret < 0) {
......
...@@ -1368,13 +1368,12 @@ static int firmwareUpload(struct usbduxfastsub_s *usbduxfastsub, ...@@ -1368,13 +1368,12 @@ static int firmwareUpload(struct usbduxfastsub_s *usbduxfastsub,
} }
/* we generate a local buffer for the firmware */ /* we generate a local buffer for the firmware */
fwBuf = kzalloc(sizeFirmware, GFP_KERNEL); fwBuf = kmemdup(firmwareBinary, sizeFirmware, GFP_KERNEL);
if (!fwBuf) { if (!fwBuf) {
dev_err(&usbduxfastsub->interface->dev, dev_err(&usbduxfastsub->interface->dev,
"comedi_: mem alloc for firmware failed\n"); "comedi_: mem alloc for firmware failed\n");
return -ENOMEM; return -ENOMEM;
} }
memcpy(fwBuf, firmwareBinary, sizeFirmware);
ret = usbduxfastsub_stop(usbduxfastsub); ret = usbduxfastsub_stop(usbduxfastsub);
if (ret < 0) { if (ret < 0) {
......
...@@ -185,11 +185,10 @@ static void VmbusOnMsgDPC(struct hv_driver *drv) ...@@ -185,11 +185,10 @@ static void VmbusOnMsgDPC(struct hv_driver *drv)
/* no msg */ /* no msg */
break; break;
} else { } else {
copied = kmalloc(sizeof(*copied), GFP_ATOMIC); copied = kmemdup(msg, sizeof(*copied), GFP_ATOMIC);
if (copied == NULL) if (copied == NULL)
continue; continue;
memcpy(copied, msg, sizeof(*copied));
osd_schedule_callback(gVmbusConnection.WorkQueue, osd_schedule_callback(gVmbusConnection.WorkQueue,
VmbusOnChannelMessage, VmbusOnChannelMessage,
(void *)copied); (void *)copied);
......
...@@ -105,10 +105,9 @@ int line6_wait_dump(struct line6_dump_request *l6dr, int nonblock) ...@@ -105,10 +105,9 @@ int line6_wait_dump(struct line6_dump_request *l6dr, int nonblock)
int line6_dumpreq_initbuf(struct line6_dump_request *l6dr, const void *buf, int line6_dumpreq_initbuf(struct line6_dump_request *l6dr, const void *buf,
size_t len, int num) size_t len, int num)
{ {
l6dr->reqbufs[num].buffer = kmalloc(len, GFP_KERNEL); l6dr->reqbufs[num].buffer = kmemdup(buf, len, GFP_KERNEL);
if (l6dr->reqbufs[num].buffer == NULL) if (l6dr->reqbufs[num].buffer == NULL)
return -ENOMEM; return -ENOMEM;
memcpy(l6dr->reqbufs[num].buffer, buf, len);
l6dr->reqbufs[num].length = len; l6dr->reqbufs[num].length = len;
return 0; return 0;
} }
......
...@@ -1074,7 +1074,8 @@ int pod_init(struct usb_interface *interface, struct usb_line6_pod *pod) ...@@ -1074,7 +1074,8 @@ int pod_init(struct usb_interface *interface, struct usb_line6_pod *pod)
return -ENOMEM; return -ENOMEM;
} }
pod->buffer_versionreq = kmalloc(sizeof(pod_request_version), pod->buffer_versionreq = kmemdup(pod_request_version,
sizeof(pod_request_version),
GFP_KERNEL); GFP_KERNEL);
if (pod->buffer_versionreq == NULL) { if (pod->buffer_versionreq == NULL) {
...@@ -1083,9 +1084,6 @@ int pod_init(struct usb_interface *interface, struct usb_line6_pod *pod) ...@@ -1083,9 +1084,6 @@ int pod_init(struct usb_interface *interface, struct usb_line6_pod *pod)
return -ENOMEM; return -ENOMEM;
} }
memcpy(pod->buffer_versionreq, pod_request_version,
sizeof(pod_request_version));
/* create sysfs entries: */ /* create sysfs entries: */
err = pod_create_files2(&interface->dev); err = pod_create_files2(&interface->dev);
if (err < 0) { if (err < 0) {
......
...@@ -486,7 +486,8 @@ int variax_init(struct usb_interface *interface, ...@@ -486,7 +486,8 @@ int variax_init(struct usb_interface *interface,
return err; return err;
} }
variax->buffer_activate = kmalloc(sizeof(variax_activate), GFP_KERNEL); variax->buffer_activate = kmemdup(variax_activate,
sizeof(variax_activate), GFP_KERNEL);
if (variax->buffer_activate == NULL) { if (variax->buffer_activate == NULL) {
dev_err(&interface->dev, "Out of memory\n"); dev_err(&interface->dev, "Out of memory\n");
...@@ -494,8 +495,6 @@ int variax_init(struct usb_interface *interface, ...@@ -494,8 +495,6 @@ int variax_init(struct usb_interface *interface,
return -ENOMEM; return -ENOMEM;
} }
memcpy(variax->buffer_activate, variax_activate,
sizeof(variax_activate));
init_timer(&variax->activate_timer); init_timer(&variax->activate_timer);
/* create sysfs entries: */ /* create sysfs entries: */
......
...@@ -204,18 +204,18 @@ int pohmelfs_copy_crypto(struct pohmelfs_sb *psb) ...@@ -204,18 +204,18 @@ int pohmelfs_copy_crypto(struct pohmelfs_sb *psb)
} }
if (g->hash_keysize) { if (g->hash_keysize) {
psb->hash_key = kmalloc(g->hash_keysize, GFP_KERNEL); psb->hash_key = kmemdup(g->hash_key, g->hash_keysize,
GFP_KERNEL);
if (!psb->hash_key) if (!psb->hash_key)
goto err_out_free_cipher_string; goto err_out_free_cipher_string;
memcpy(psb->hash_key, g->hash_key, g->hash_keysize);
psb->hash_keysize = g->hash_keysize; psb->hash_keysize = g->hash_keysize;
} }
if (g->cipher_keysize) { if (g->cipher_keysize) {
psb->cipher_key = kmalloc(g->cipher_keysize, GFP_KERNEL); psb->cipher_key = kmemdup(g->cipher_key, g->cipher_keysize,
GFP_KERNEL);
if (!psb->cipher_key) if (!psb->cipher_key)
goto err_out_free_hash; goto err_out_free_hash;
memcpy(psb->cipher_key, g->cipher_key, g->cipher_keysize);
psb->cipher_keysize = g->cipher_keysize; psb->cipher_keysize = g->cipher_keysize;
} }
...@@ -454,14 +454,12 @@ static int pohmelfs_crypto_hash_init(struct pohmelfs_config_group *g, struct poh ...@@ -454,14 +454,12 @@ static int pohmelfs_crypto_hash_init(struct pohmelfs_config_group *g, struct poh
g->hash_strlen = c->strlen; g->hash_strlen = c->strlen;
g->hash_keysize = c->keysize; g->hash_keysize = c->keysize;
g->hash_key = kmalloc(c->keysize, GFP_KERNEL); g->hash_key = kmemdup(key, c->keysize, GFP_KERNEL);
if (!g->hash_key) { if (!g->hash_key) {
kfree(g->hash_string); kfree(g->hash_string);
return -ENOMEM; return -ENOMEM;
} }
memcpy(g->hash_key, key, c->keysize);
return 0; return 0;
} }
...@@ -479,14 +477,12 @@ static int pohmelfs_crypto_cipher_init(struct pohmelfs_config_group *g, struct p ...@@ -479,14 +477,12 @@ static int pohmelfs_crypto_cipher_init(struct pohmelfs_config_group *g, struct p
g->cipher_strlen = c->strlen; g->cipher_strlen = c->strlen;
g->cipher_keysize = c->keysize; g->cipher_keysize = c->keysize;
g->cipher_key = kmalloc(c->keysize, GFP_KERNEL); g->cipher_key = kmemdup(key, c->keysize, GFP_KERNEL);
if (!g->cipher_key) { if (!g->cipher_key) {
kfree(g->cipher_string); kfree(g->cipher_string);
return -ENOMEM; return -ENOMEM;
} }
memcpy(g->cipher_key, key, c->keysize);
return 0; return 0;
} }
......
...@@ -2667,11 +2667,11 @@ static int ieee80211_wpa_set_wpa_ie(struct ieee80211_device *ieee, ...@@ -2667,11 +2667,11 @@ static int ieee80211_wpa_set_wpa_ie(struct ieee80211_device *ieee,
return -EINVAL; return -EINVAL;
if (param->u.wpa_ie.len) { if (param->u.wpa_ie.len) {
buf = kmalloc(param->u.wpa_ie.len, GFP_KERNEL); buf = kmemdup(param->u.wpa_ie.data, param->u.wpa_ie.len,
GFP_KERNEL);
if (buf == NULL) if (buf == NULL)
return -ENOMEM; return -ENOMEM;
memcpy(buf, param->u.wpa_ie.data, param->u.wpa_ie.len);
kfree(ieee->wpa_ie); kfree(ieee->wpa_ie);
ieee->wpa_ie = buf; ieee->wpa_ie = buf;
ieee->wpa_ie_len = param->u.wpa_ie.len; ieee->wpa_ie_len = param->u.wpa_ie.len;
......
...@@ -727,10 +727,9 @@ int ieee80211_wx_set_gen_ie(struct ieee80211_device *ieee, u8 *ie, size_t len) ...@@ -727,10 +727,9 @@ int ieee80211_wx_set_gen_ie(struct ieee80211_device *ieee, u8 *ie, size_t len)
printk("len:%zu, ie:%d\n", len, ie[1]); printk("len:%zu, ie:%d\n", len, ie[1]);
return -EINVAL; return -EINVAL;
} }
buf = kmalloc(len, GFP_KERNEL); buf = kmemdup(ie, len, GFP_KERNEL);
if (buf == NULL) if (buf == NULL)
return -ENOMEM; return -ENOMEM;
memcpy(buf, ie, len);
kfree(ieee->wpa_ie); kfree(ieee->wpa_ie);
ieee->wpa_ie = buf; ieee->wpa_ie = buf;
ieee->wpa_ie_len = len; ieee->wpa_ie_len = len;
......
...@@ -3255,11 +3255,11 @@ static int ieee80211_wpa_set_wpa_ie(struct ieee80211_device *ieee, ...@@ -3255,11 +3255,11 @@ static int ieee80211_wpa_set_wpa_ie(struct ieee80211_device *ieee,
return -EINVAL; return -EINVAL;
if (param->u.wpa_ie.len) { if (param->u.wpa_ie.len) {
buf = kmalloc(param->u.wpa_ie.len, GFP_KERNEL); buf = kmemdup(param->u.wpa_ie.data, param->u.wpa_ie.len,
GFP_KERNEL);
if (buf == NULL) if (buf == NULL)
return -ENOMEM; return -ENOMEM;
memcpy(buf, param->u.wpa_ie.data, param->u.wpa_ie.len);
kfree(ieee->wpa_ie); kfree(ieee->wpa_ie);
ieee->wpa_ie = buf; ieee->wpa_ie = buf;
ieee->wpa_ie_len = param->u.wpa_ie.len; ieee->wpa_ie_len = param->u.wpa_ie.len;
......
...@@ -979,10 +979,9 @@ int ieee80211_wx_set_gen_ie(struct ieee80211_device *ieee, u8 *ie, size_t len) ...@@ -979,10 +979,9 @@ int ieee80211_wx_set_gen_ie(struct ieee80211_device *ieee, u8 *ie, size_t len)
printk("len:%zu, ie:%d\n", len, ie[1]); printk("len:%zu, ie:%d\n", len, ie[1]);
return -EINVAL; return -EINVAL;
} }
buf = kmalloc(len, GFP_KERNEL); buf = kmemdup(ie, len, GFP_KERNEL);
if (buf == NULL) if (buf == NULL)
return -ENOMEM; return -ENOMEM;
memcpy(buf, ie, len);
kfree(ieee->wpa_ie); kfree(ieee->wpa_ie);
ieee->wpa_ie = buf; ieee->wpa_ie = buf;
ieee->wpa_ie_len = len; ieee->wpa_ie_len = len;
......
...@@ -2844,11 +2844,11 @@ static int ieee80211_wpa_set_wpa_ie(struct ieee80211_device *ieee, ...@@ -2844,11 +2844,11 @@ static int ieee80211_wpa_set_wpa_ie(struct ieee80211_device *ieee,
return -EINVAL; return -EINVAL;
if (param->u.wpa_ie.len) { if (param->u.wpa_ie.len) {
buf = kmalloc(param->u.wpa_ie.len, GFP_KERNEL); buf = kmemdup(param->u.wpa_ie.data, param->u.wpa_ie.len,
GFP_KERNEL);
if (buf == NULL) if (buf == NULL)
return -ENOMEM; return -ENOMEM;
memcpy(buf, param->u.wpa_ie.data, param->u.wpa_ie.len);
kfree(ieee->wpa_ie); kfree(ieee->wpa_ie);
ieee->wpa_ie = buf; ieee->wpa_ie = buf;
ieee->wpa_ie_len = param->u.wpa_ie.len; ieee->wpa_ie_len = param->u.wpa_ie.len;
......
...@@ -767,10 +767,9 @@ int ieee80211_wx_set_gen_ie(struct ieee80211_device *ieee, u8 *ie, size_t len) ...@@ -767,10 +767,9 @@ int ieee80211_wx_set_gen_ie(struct ieee80211_device *ieee, u8 *ie, size_t len)
printk("len: %Zd, ie:%d\n", len, ie[1]); printk("len: %Zd, ie:%d\n", len, ie[1]);
return -EINVAL; return -EINVAL;
} }
buf = kmalloc(len, GFP_KERNEL); buf = kmemdup(ie, len, GFP_KERNEL);
if (buf == NULL) if (buf == NULL)
return -ENOMEM; return -ENOMEM;
memcpy(buf, ie, len);
kfree(ieee->wpa_ie); kfree(ieee->wpa_ie);
ieee->wpa_ie = buf; ieee->wpa_ie = buf;
ieee->wpa_ie_len = len; ieee->wpa_ie_len = len;
......
...@@ -1579,10 +1579,9 @@ static inline u16 auth_parse(struct sk_buff *skb, u8** challenge, int *chlen) ...@@ -1579,10 +1579,9 @@ static inline u16 auth_parse(struct sk_buff *skb, u8** challenge, int *chlen)
if(*(t++) == MFIE_TYPE_CHALLENGE){ if(*(t++) == MFIE_TYPE_CHALLENGE){
*chlen = *(t++); *chlen = *(t++);
*challenge = kmalloc(*chlen, GFP_ATOMIC); *challenge = kmemdup(t, *chlen, GFP_ATOMIC);
if (!*challenge) if (!*challenge)
return -ENOMEM; return -ENOMEM;
memcpy(*challenge, t, *chlen);
} }
} }
...@@ -2870,11 +2869,11 @@ static int ieee80211_wpa_set_wpa_ie(struct ieee80211_device *ieee, ...@@ -2870,11 +2869,11 @@ static int ieee80211_wpa_set_wpa_ie(struct ieee80211_device *ieee,
return -EINVAL; return -EINVAL;
if (param->u.wpa_ie.len) { if (param->u.wpa_ie.len) {
buf = kmalloc(param->u.wpa_ie.len, GFP_KERNEL); buf = kmemdup(param->u.wpa_ie.data, param->u.wpa_ie.len,
GFP_KERNEL);
if (buf == NULL) if (buf == NULL)
return -ENOMEM; return -ENOMEM;
memcpy(buf, param->u.wpa_ie.data, param->u.wpa_ie.len);
kfree(ieee->wpa_ie); kfree(ieee->wpa_ie);
ieee->wpa_ie = buf; ieee->wpa_ie = buf;
ieee->wpa_ie_len = param->u.wpa_ie.len; ieee->wpa_ie_len = param->u.wpa_ie.len;
......
...@@ -847,10 +847,9 @@ int ieee80211_wx_set_gen_ie(struct ieee80211_device *ieee, u8 *ie, size_t len) ...@@ -847,10 +847,9 @@ int ieee80211_wx_set_gen_ie(struct ieee80211_device *ieee, u8 *ie, size_t len)
printk("len:%zu, ie:%d\n", len, ie[1]); printk("len:%zu, ie:%d\n", len, ie[1]);
return -EINVAL; return -EINVAL;
} }
buf = kmalloc(len, GFP_KERNEL); buf = kmemdup(ie, len, GFP_KERNEL);
if (buf == NULL) if (buf == NULL)
return -ENOMEM; return -ENOMEM;
memcpy(buf, ie, len);
kfree(ieee->wpa_ie); kfree(ieee->wpa_ie);
ieee->wpa_ie = buf; ieee->wpa_ie = buf;
ieee->wpa_ie_len = len; ieee->wpa_ie_len = len;
......
...@@ -502,13 +502,13 @@ static void stub_recv_cmd_submit(struct stub_device *sdev, ...@@ -502,13 +502,13 @@ static void stub_recv_cmd_submit(struct stub_device *sdev,
} }
/* set priv->urb->setup_packet */ /* set priv->urb->setup_packet */
priv->urb->setup_packet = kzalloc(8, GFP_KERNEL); priv->urb->setup_packet = kmemdup(&pdu->u.cmd_submit.setup, 8,
GFP_KERNEL);
if (!priv->urb->setup_packet) { if (!priv->urb->setup_packet) {
dev_err(&sdev->interface->dev, "allocate setup_packet\n"); dev_err(&sdev->interface->dev, "allocate setup_packet\n");
usbip_event_add(ud, SDEV_EVENT_ERROR_MALLOC); usbip_event_add(ud, SDEV_EVENT_ERROR_MALLOC);
return; return;
} }
memcpy(priv->urb->setup_packet, &pdu->u.cmd_submit.setup, 8);
/* set other members from the base header of pdu */ /* set other members from the base header of pdu */
priv->urb->context = (void *) priv; priv->urb->context = (void *) priv;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment