This patch simplifies the string duplication code in

scst_sysfs.c and scst_vdisk.c. Code for duplicating '\0'-terminated
strings has been replaced by a call to kstrdup() and code for
duplicating fixed-length strings has been replaced by a call to
kasprintf(GFP_KERNEL, "%.*s", (int)size, buf).

Signed-off-by: Bart Van Assche <bvanassche@acm.org>



git-svn-id: http://svn.code.sf.net/p/scst/svn/trunk@2837 d57e44dd-8a1f-0410-8b47-8ef2f437770f
This commit is contained in:
Vladislav Bolkhovitin
2010-11-26 14:05:29 +00:00
parent 2935a05765
commit 515fda68cf
2 changed files with 12 additions and 39 deletions

View File

@@ -3950,8 +3950,7 @@ static int vcdrom_change(struct scst_vdisk_dev *virt_dev,
old_fn = virt_dev->filename;
if (!virt_dev->cdrom_empty) {
int len = strlen(filename) + 1;
char *fn = kmalloc(len, GFP_KERNEL);
char *fn = kstrdup(filename, GFP_KERNEL);
if (fn == NULL) {
TRACE(TRACE_OUT_OF_MEM, "%s",
"Allocation of filename failed");
@@ -3959,7 +3958,6 @@ static int vcdrom_change(struct scst_vdisk_dev *virt_dev,
goto out_unlock;
}
strlcpy(fn, filename, len);
virt_dev->filename = fn;
res = vdisk_get_file_size(virt_dev->filename,
@@ -4053,15 +4051,13 @@ static ssize_t vcdrom_sysfs_filename_store(struct kobject *kobj,
dev = container_of(kobj, struct scst_device, dev_kobj);
i_buf = kmalloc(count+1, GFP_KERNEL);
i_buf = kasprintf(GFP_KERNEL, "%.*s", (int)count, buf);
if (i_buf == NULL) {
PRINT_ERROR("Unable to alloc intermediate buffer with size %zd",
count+1);
res = -ENOMEM;
goto out;
}
memcpy(i_buf, buf, count);
i_buf[count] = '\0';
res = scst_alloc_sysfs_work(vcdrom_sysfs_process_filename_store,
false, &work);
@@ -4555,7 +4551,7 @@ static int vdisk_write_proc(char *buffer, char **start, off_t offset,
if ((length == 0) || (buffer == NULL) || (buffer[0] == '\0'))
goto out;
i_buf = kmalloc(length+1, GFP_KERNEL);
i_buf = kasprintf(GFP_KERNEL, "%.*s", (int)length, buffer);
if (i_buf == NULL) {
PRINT_ERROR("Unable to alloc intermediate buffer with size %d",
length+1);
@@ -4563,9 +4559,6 @@ static int vdisk_write_proc(char *buffer, char **start, off_t offset,
goto out;
}
memcpy(i_buf, buffer, length);
i_buf[length] = '\0';
if (mutex_lock_interruptible(&scst_vdisk_mutex) != 0) {
res = -EINTR;
goto out_free;
@@ -4726,15 +4719,13 @@ static int vdisk_write_proc(char *buffer, char **start, off_t offset,
goto out_up;
}
len = strlen(filename) + 1;
virt_dev->filename = kmalloc(len, GFP_KERNEL);
virt_dev->filename = kstrdup(filename, GFP_KERNEL);
if (virt_dev->filename == NULL) {
TRACE(TRACE_OUT_OF_MEM, "%s",
"Allocation of filename failed");
res = -ENOMEM;
goto out_free_vdev;
}
strlcpy(virt_dev->filename, filename, len);
list_add_tail(&virt_dev->vdev_list_entry,
&vdev_list);
@@ -4929,15 +4920,13 @@ static int vcdrom_open(char *p, char *name)
virt_dev->removable = 1;
if (!virt_dev->cdrom_empty) {
len = strlen(filename) + 1;
virt_dev->filename = kmalloc(len, GFP_KERNEL);
virt_dev->filename = kstrdup(filename, GFP_KERNEL);
if (virt_dev->filename == NULL) {
TRACE(TRACE_OUT_OF_MEM, "%s",
"Allocation of filename failed");
res = -ENOMEM;
goto out_free_vdev;
}
strncpy(virt_dev->filename, filename, len);
}
list_add_tail(&virt_dev->vdev_list_entry, &vdev_list);
@@ -5030,7 +5019,7 @@ static int vcdrom_write_proc(char *buffer, char **start, off_t offset,
if ((length == 0) || (buffer == NULL) || (buffer[0] == '\0'))
goto out;
i_buf = kmalloc(length+1, GFP_KERNEL);
i_buf = kasnprintf(GFP_KERNEL, "%.*s", (int)length, buffer);
if (i_buf == NULL) {
PRINT_ERROR("Unable to alloc intermediate buffer with size %d",
length+1);
@@ -5038,9 +5027,6 @@ static int vcdrom_write_proc(char *buffer, char **start, off_t offset,
goto out;
}
memcpy(i_buf, buffer, length);
i_buf[length] = '\0';
if (mutex_lock_interruptible(&scst_vdisk_mutex) != 0) {
res = -EINTR;
goto out_free;

View File

@@ -791,13 +791,11 @@ static ssize_t scst_tgtt_mgmt_store(struct kobject *kobj,
tgtt = container_of(kobj, struct scst_tgt_template, tgtt_kobj);
buffer = kzalloc(count+1, GFP_KERNEL);
buffer = kasprintf(GFP_KERNEL, "%.*s", (int)count, buf);
if (buffer == NULL) {
res = -ENOMEM;
goto out;
}
memcpy(buffer, buf, count);
buffer[count] = '\0';
res = scst_alloc_sysfs_work(scst_tgtt_mgmt_store_work_fn, false, &work);
if (res != 0)
@@ -2902,13 +2900,11 @@ static ssize_t __scst_acg_mgmt_store(struct scst_acg *acg,
TRACE_ENTRY();
buffer = kzalloc(count+1, GFP_KERNEL);
buffer = kasprintf(GFP_KERNEL, "%.*s", (int)count, buf);
if (buffer == NULL) {
res = -ENOMEM;
goto out;
}
memcpy(buffer, buf, count);
buffer[count] = '\0';
res = scst_alloc_sysfs_work(sysfs_work_fn, false, &work);
if (res != 0)
@@ -3702,13 +3698,11 @@ static ssize_t scst_ini_group_mgmt_store(struct kobject *kobj,
tgt = container_of(kobj->parent, struct scst_tgt, tgt_kobj);
buffer = kzalloc(count+1, GFP_KERNEL);
buffer = kasprintf(GFP_KERNEL, "%.*s", (int)count, buf);
if (buffer == NULL) {
res = -ENOMEM;
goto out;
}
memcpy(buffer, buf, count);
buffer[count] = '\0';
res = scst_alloc_sysfs_work(scst_ini_group_mgmt_store_work_fn, false,
&work);
@@ -3836,7 +3830,6 @@ out:
int scst_acn_sysfs_create(struct scst_acn *acn)
{
int res = 0;
int len;
struct scst_acg *acg = acn->acg;
struct kobj_attribute *attr = NULL;
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 34)
@@ -3857,15 +3850,13 @@ int scst_acn_sysfs_create(struct scst_acn *acn)
goto out;
}
len = strlen(acn->name) + 1;
attr->attr.name = kzalloc(len, GFP_KERNEL);
attr->attr.name = kstrdup(acn->name, GFP_KERNEL);
if (attr->attr.name == NULL) {
PRINT_ERROR("Unable to allocate attributes for initiator '%s'",
acn->name);
res = -ENOMEM;
goto out_free;
}
strlcpy((char *)attr->attr.name, acn->name, len);
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 36)
attr->attr.owner = THIS_MODULE;
@@ -4466,15 +4457,13 @@ static int scst_write_trace(const char *buf, size_t length,
goto out;
}
buffer = kmalloc(length+1, GFP_KERNEL);
buffer = kasprintf(GFP_KERNEL, "%.*s", (int)length, buf);
if (buffer == NULL) {
PRINT_ERROR("Unable to alloc intermediate buffer (size %zd)",
length+1);
res = -ENOMEM;
goto out;
}
memcpy(buffer, buf, length);
buffer[length] = '\0';
TRACE_DBG("buffer %s", buffer);
@@ -4955,13 +4944,11 @@ static ssize_t __scst_devt_mgmt_store(struct kobject *kobj,
devt = container_of(kobj, struct scst_dev_type, devt_kobj);
buffer = kzalloc(count+1, GFP_KERNEL);
buffer = kasprintf(GFP_KERNEL, "%.*s", (int)count, buf);
if (buffer == NULL) {
res = -ENOMEM;
goto out;
}
memcpy(buffer, buf, count);
buffer[count] = '\0';
res = scst_alloc_sysfs_work(sysfs_work_fn, false, &work);
if (res != 0)