Use kstrdup instead of kmalloc and strncpy. Signed-off-by: Geliang Tang <geliangt...@163.com> --- drivers/misc/mic/cosm/cosm_sysfs.c | 23 ++++------------------- 1 file changed, 4 insertions(+), 19 deletions(-)
diff --git a/drivers/misc/mic/cosm/cosm_sysfs.c b/drivers/misc/mic/cosm/cosm_sysfs.c index 29d6863..c3db466 100644 --- a/drivers/misc/mic/cosm/cosm_sysfs.c +++ b/drivers/misc/mic/cosm/cosm_sysfs.c @@ -211,18 +211,14 @@ cmdline_store(struct device *dev, struct device_attribute *attr, mutex_lock(&cdev->cosm_mutex); kfree(cdev->cmdline); - cdev->cmdline = kmalloc(count + 1, GFP_KERNEL); + cdev->cmdline = kstrdup(buf, GFP_KERNEL); if (!cdev->cmdline) { count = -ENOMEM; goto unlock; } - strncpy(cdev->cmdline, buf, count); - if (cdev->cmdline[count - 1] == '\n') cdev->cmdline[count - 1] = '\0'; - else - cdev->cmdline[count] = '\0'; unlock: mutex_unlock(&cdev->cosm_mutex); return count; @@ -257,17 +253,14 @@ firmware_store(struct device *dev, struct device_attribute *attr, mutex_lock(&cdev->cosm_mutex); kfree(cdev->firmware); - cdev->firmware = kmalloc(count + 1, GFP_KERNEL); + cdev->firmware = kstrdup(buf, GFP_KERNEL); if (!cdev->firmware) { count = -ENOMEM; goto unlock; } - strncpy(cdev->firmware, buf, count); if (cdev->firmware[count - 1] == '\n') cdev->firmware[count - 1] = '\0'; - else - cdev->firmware[count] = '\0'; unlock: mutex_unlock(&cdev->cosm_mutex); return count; @@ -302,18 +295,14 @@ ramdisk_store(struct device *dev, struct device_attribute *attr, mutex_lock(&cdev->cosm_mutex); kfree(cdev->ramdisk); - cdev->ramdisk = kmalloc(count + 1, GFP_KERNEL); + cdev->ramdisk = kstrdup(buf, GFP_KERNEL); if (!cdev->ramdisk) { count = -ENOMEM; goto unlock; } - strncpy(cdev->ramdisk, buf, count); - if (cdev->ramdisk[count - 1] == '\n') cdev->ramdisk[count - 1] = '\0'; - else - cdev->ramdisk[count] = '\0'; unlock: mutex_unlock(&cdev->cosm_mutex); return count; @@ -351,18 +340,14 @@ bootmode_store(struct device *dev, struct device_attribute *attr, mutex_lock(&cdev->cosm_mutex); kfree(cdev->bootmode); - cdev->bootmode = kmalloc(count + 1, GFP_KERNEL); + cdev->bootmode = kstrdup(buf, GFP_KERNEL); if (!cdev->bootmode) { count = -ENOMEM; goto unlock; } - strncpy(cdev->bootmode, buf, count); - if (cdev->bootmode[count - 1] == '\n') cdev->bootmode[count - 1] = '\0'; - else - cdev->bootmode[count] = '\0'; unlock: mutex_unlock(&cdev->cosm_mutex); return count; -- 1.9.1 -- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majord...@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/