parport: Use kasprintf() instead of fixed buffer formatting

Improve readability and maintainability by replacing a hardcoded string
allocation and formatting by the use of the kasprintf() helper.

Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Link: https://lore.kernel.org/r/20231016133135.1203643-2-andriy.shevchenko@linux.intel.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
This commit is contained in:
Andy Shevchenko 2023-10-16 16:31:33 +03:00 committed by Greg Kroah-Hartman
parent c1426d392a
commit 8d8ae17eb0
3 changed files with 15 additions and 55 deletions

View File

@ -32,13 +32,6 @@
#define PARPORT_MAX_TIMESLICE_VALUE ((unsigned long) HZ) #define PARPORT_MAX_TIMESLICE_VALUE ((unsigned long) HZ)
#define PARPORT_MIN_SPINTIME_VALUE 1 #define PARPORT_MIN_SPINTIME_VALUE 1
#define PARPORT_MAX_SPINTIME_VALUE 1000 #define PARPORT_MAX_SPINTIME_VALUE 1000
/*
* PARPORT_BASE_* is the size of the known parts of the sysctl path
* in dev/partport/%s/devices/%s. "dev/parport/"(12), "/devices/"(9
* and null char(1).
*/
#define PARPORT_BASE_PATH_SIZE 13
#define PARPORT_BASE_DEVICES_PATH_SIZE 22
static int do_active_device(struct ctl_table *table, int write, static int do_active_device(struct ctl_table *table, int write,
void *result, size_t *lenp, loff_t *ppos) void *result, size_t *lenp, loff_t *ppos)
@ -431,8 +424,7 @@ int parport_proc_register(struct parport *port)
{ {
struct parport_sysctl_table *t; struct parport_sysctl_table *t;
char *tmp_dir_path; char *tmp_dir_path;
size_t tmp_path_len, port_name_len; int i, err = 0;
int bytes_written, i, err = 0;
t = kmemdup(&parport_sysctl_template, sizeof(*t), GFP_KERNEL); t = kmemdup(&parport_sysctl_template, sizeof(*t), GFP_KERNEL);
if (t == NULL) if (t == NULL)
@ -446,35 +438,23 @@ int parport_proc_register(struct parport *port)
t->vars[5 + i].extra2 = &port->probe_info[i]; t->vars[5 + i].extra2 = &port->probe_info[i];
} }
port_name_len = strnlen(port->name, PARPORT_NAME_MAX_LEN); tmp_dir_path = kasprintf(GFP_KERNEL, "dev/parport/%s/devices", port->name);
/*
* Allocate a buffer for two paths: dev/parport/PORT and dev/parport/PORT/devices.
* We calculate for the second as that will give us enough for the first.
*/
tmp_path_len = PARPORT_BASE_DEVICES_PATH_SIZE + port_name_len;
tmp_dir_path = kzalloc(tmp_path_len, GFP_KERNEL);
if (!tmp_dir_path) { if (!tmp_dir_path) {
err = -ENOMEM; err = -ENOMEM;
goto exit_free_t; goto exit_free_t;
} }
bytes_written = snprintf(tmp_dir_path, tmp_path_len,
"dev/parport/%s/devices", port->name);
if (tmp_path_len <= bytes_written) {
err = -ENOENT;
goto exit_free_tmp_dir_path;
}
t->devices_header = register_sysctl(tmp_dir_path, t->device_dir); t->devices_header = register_sysctl(tmp_dir_path, t->device_dir);
if (t->devices_header == NULL) { if (t->devices_header == NULL) {
err = -ENOENT; err = -ENOENT;
goto exit_free_tmp_dir_path; goto exit_free_tmp_dir_path;
} }
tmp_path_len = PARPORT_BASE_PATH_SIZE + port_name_len; kfree(tmp_dir_path);
bytes_written = snprintf(tmp_dir_path, tmp_path_len,
"dev/parport/%s", port->name); tmp_dir_path = kasprintf(GFP_KERNEL, "dev/parport/%s", port->name);
if (tmp_path_len <= bytes_written) { if (!tmp_dir_path) {
err = -ENOENT; err = -ENOMEM;
goto unregister_devices_h; goto unregister_devices_h;
} }
@ -514,34 +494,22 @@ int parport_proc_unregister(struct parport *port)
int parport_device_proc_register(struct pardevice *device) int parport_device_proc_register(struct pardevice *device)
{ {
int bytes_written, err = 0;
struct parport_device_sysctl_table *t; struct parport_device_sysctl_table *t;
struct parport * port = device->port; struct parport * port = device->port;
size_t port_name_len, device_name_len, tmp_dir_path_len;
char *tmp_dir_path; char *tmp_dir_path;
int err = 0;
t = kmemdup(&parport_device_sysctl_template, sizeof(*t), GFP_KERNEL); t = kmemdup(&parport_device_sysctl_template, sizeof(*t), GFP_KERNEL);
if (t == NULL) if (t == NULL)
return -ENOMEM; return -ENOMEM;
port_name_len = strnlen(port->name, PARPORT_NAME_MAX_LEN);
device_name_len = strnlen(device->name, PATH_MAX);
/* Allocate a buffer for two paths: dev/parport/PORT/devices/DEVICE. */ /* Allocate a buffer for two paths: dev/parport/PORT/devices/DEVICE. */
tmp_dir_path_len = PARPORT_BASE_DEVICES_PATH_SIZE + port_name_len + device_name_len; tmp_dir_path = kasprintf(GFP_KERNEL, "dev/parport/%s/devices/%s", port->name, device->name);
tmp_dir_path = kzalloc(tmp_dir_path_len, GFP_KERNEL);
if (!tmp_dir_path) { if (!tmp_dir_path) {
err = -ENOMEM; err = -ENOMEM;
goto exit_free_t; goto exit_free_t;
} }
bytes_written = snprintf(tmp_dir_path, tmp_dir_path_len, "dev/parport/%s/devices/%s",
port->name, device->name);
if (tmp_dir_path_len <= bytes_written) {
err = -ENOENT;
goto exit_free_path;
}
t->vars[0].data = &device->timeslice; t->vars[0].data = &device->timeslice;
t->sysctl_header = register_sysctl(tmp_dir_path, t->vars); t->sysctl_header = register_sysctl(tmp_dir_path, t->vars);
@ -554,9 +522,6 @@ int parport_device_proc_register(struct pardevice *device)
kfree(tmp_dir_path); kfree(tmp_dir_path);
return 0; return 0;
exit_free_path:
kfree(tmp_dir_path);
exit_free_t: exit_free_t:
kfree(t); kfree(t);

View File

@ -438,7 +438,6 @@ struct parport *parport_register_port(unsigned long base, int irq, int dma,
struct parport *tmp; struct parport *tmp;
int num; int num;
int device; int device;
char *name;
int ret; int ret;
tmp = kzalloc(sizeof(struct parport), GFP_KERNEL); tmp = kzalloc(sizeof(struct parport), GFP_KERNEL);
@ -467,11 +466,6 @@ struct parport *parport_register_port(unsigned long base, int irq, int dma,
atomic_set(&tmp->ref_count, 1); atomic_set(&tmp->ref_count, 1);
INIT_LIST_HEAD(&tmp->full_list); INIT_LIST_HEAD(&tmp->full_list);
name = kmalloc(PARPORT_NAME_MAX_LEN, GFP_KERNEL);
if (!name) {
kfree(tmp);
return NULL;
}
/* Search for the lowest free parport number. */ /* Search for the lowest free parport number. */
spin_lock(&full_list_lock); spin_lock(&full_list_lock);
@ -487,11 +481,14 @@ struct parport *parport_register_port(unsigned long base, int irq, int dma,
/* /*
* Now that the portnum is known finish doing the Init. * Now that the portnum is known finish doing the Init.
*/ */
sprintf(name, "parport%d", tmp->portnum = tmp->number); tmp->name = kasprintf(GFP_KERNEL, "parport%d", tmp->portnum);
tmp->name = name; if (!tmp->name) {
kfree(tmp);
return NULL;
}
dev_set_name(&tmp->bus_dev, tmp->name);
tmp->bus_dev.bus = &parport_bus_type; tmp->bus_dev.bus = &parport_bus_type;
tmp->bus_dev.release = free_port; tmp->bus_dev.release = free_port;
dev_set_name(&tmp->bus_dev, name);
tmp->bus_dev.type = &parport_device_type; tmp->bus_dev.type = &parport_device_type;
for (device = 0; device < 5; device++) for (device = 0; device < 5; device++)

View File

@ -180,8 +180,6 @@ struct ieee1284_info {
struct semaphore irq; struct semaphore irq;
}; };
#define PARPORT_NAME_MAX_LEN 15
/* A parallel port */ /* A parallel port */
struct parport { struct parport {
unsigned long base; /* base address */ unsigned long base; /* base address */