Skip to content

Commit 8d8ae17

Browse files
andy-shevgregkh
authored andcommitted
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>
1 parent c1426d3 commit 8d8ae17

3 files changed

Lines changed: 15 additions & 55 deletions

File tree

drivers/parport/procfs.c

Lines changed: 9 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,6 @@
3232
#define PARPORT_MAX_TIMESLICE_VALUE ((unsigned long) HZ)
3333
#define PARPORT_MIN_SPINTIME_VALUE 1
3434
#define PARPORT_MAX_SPINTIME_VALUE 1000
35-
/*
36-
* PARPORT_BASE_* is the size of the known parts of the sysctl path
37-
* in dev/partport/%s/devices/%s. "dev/parport/"(12), "/devices/"(9
38-
* and null char(1).
39-
*/
40-
#define PARPORT_BASE_PATH_SIZE 13
41-
#define PARPORT_BASE_DEVICES_PATH_SIZE 22
4235

4336
static int do_active_device(struct ctl_table *table, int write,
4437
void *result, size_t *lenp, loff_t *ppos)
@@ -431,8 +424,7 @@ int parport_proc_register(struct parport *port)
431424
{
432425
struct parport_sysctl_table *t;
433426
char *tmp_dir_path;
434-
size_t tmp_path_len, port_name_len;
435-
int bytes_written, i, err = 0;
427+
int i, err = 0;
436428

437429
t = kmemdup(&parport_sysctl_template, sizeof(*t), GFP_KERNEL);
438430
if (t == NULL)
@@ -446,35 +438,23 @@ int parport_proc_register(struct parport *port)
446438
t->vars[5 + i].extra2 = &port->probe_info[i];
447439
}
448440

449-
port_name_len = strnlen(port->name, PARPORT_NAME_MAX_LEN);
450-
/*
451-
* Allocate a buffer for two paths: dev/parport/PORT and dev/parport/PORT/devices.
452-
* We calculate for the second as that will give us enough for the first.
453-
*/
454-
tmp_path_len = PARPORT_BASE_DEVICES_PATH_SIZE + port_name_len;
455-
tmp_dir_path = kzalloc(tmp_path_len, GFP_KERNEL);
441+
tmp_dir_path = kasprintf(GFP_KERNEL, "dev/parport/%s/devices", port->name);
456442
if (!tmp_dir_path) {
457443
err = -ENOMEM;
458444
goto exit_free_t;
459445
}
460446

461-
bytes_written = snprintf(tmp_dir_path, tmp_path_len,
462-
"dev/parport/%s/devices", port->name);
463-
if (tmp_path_len <= bytes_written) {
464-
err = -ENOENT;
465-
goto exit_free_tmp_dir_path;
466-
}
467447
t->devices_header = register_sysctl(tmp_dir_path, t->device_dir);
468448
if (t->devices_header == NULL) {
469449
err = -ENOENT;
470450
goto exit_free_tmp_dir_path;
471451
}
472452

473-
tmp_path_len = PARPORT_BASE_PATH_SIZE + port_name_len;
474-
bytes_written = snprintf(tmp_dir_path, tmp_path_len,
475-
"dev/parport/%s", port->name);
476-
if (tmp_path_len <= bytes_written) {
477-
err = -ENOENT;
453+
kfree(tmp_dir_path);
454+
455+
tmp_dir_path = kasprintf(GFP_KERNEL, "dev/parport/%s", port->name);
456+
if (!tmp_dir_path) {
457+
err = -ENOMEM;
478458
goto unregister_devices_h;
479459
}
480460

@@ -514,34 +494,22 @@ int parport_proc_unregister(struct parport *port)
514494

515495
int parport_device_proc_register(struct pardevice *device)
516496
{
517-
int bytes_written, err = 0;
518497
struct parport_device_sysctl_table *t;
519498
struct parport * port = device->port;
520-
size_t port_name_len, device_name_len, tmp_dir_path_len;
521499
char *tmp_dir_path;
500+
int err = 0;
522501

523502
t = kmemdup(&parport_device_sysctl_template, sizeof(*t), GFP_KERNEL);
524503
if (t == NULL)
525504
return -ENOMEM;
526505

527-
port_name_len = strnlen(port->name, PARPORT_NAME_MAX_LEN);
528-
device_name_len = strnlen(device->name, PATH_MAX);
529-
530506
/* Allocate a buffer for two paths: dev/parport/PORT/devices/DEVICE. */
531-
tmp_dir_path_len = PARPORT_BASE_DEVICES_PATH_SIZE + port_name_len + device_name_len;
532-
tmp_dir_path = kzalloc(tmp_dir_path_len, GFP_KERNEL);
507+
tmp_dir_path = kasprintf(GFP_KERNEL, "dev/parport/%s/devices/%s", port->name, device->name);
533508
if (!tmp_dir_path) {
534509
err = -ENOMEM;
535510
goto exit_free_t;
536511
}
537512

538-
bytes_written = snprintf(tmp_dir_path, tmp_dir_path_len, "dev/parport/%s/devices/%s",
539-
port->name, device->name);
540-
if (tmp_dir_path_len <= bytes_written) {
541-
err = -ENOENT;
542-
goto exit_free_path;
543-
}
544-
545513
t->vars[0].data = &device->timeslice;
546514

547515
t->sysctl_header = register_sysctl(tmp_dir_path, t->vars);
@@ -554,9 +522,6 @@ int parport_device_proc_register(struct pardevice *device)
554522
kfree(tmp_dir_path);
555523
return 0;
556524

557-
exit_free_path:
558-
kfree(tmp_dir_path);
559-
560525
exit_free_t:
561526
kfree(t);
562527

drivers/parport/share.c

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,6 @@ struct parport *parport_register_port(unsigned long base, int irq, int dma,
438438
struct parport *tmp;
439439
int num;
440440
int device;
441-
char *name;
442441
int ret;
443442

444443
tmp = kzalloc(sizeof(struct parport), GFP_KERNEL);
@@ -467,11 +466,6 @@ struct parport *parport_register_port(unsigned long base, int irq, int dma,
467466
atomic_set(&tmp->ref_count, 1);
468467
INIT_LIST_HEAD(&tmp->full_list);
469468

470-
name = kmalloc(PARPORT_NAME_MAX_LEN, GFP_KERNEL);
471-
if (!name) {
472-
kfree(tmp);
473-
return NULL;
474-
}
475469
/* Search for the lowest free parport number. */
476470

477471
spin_lock(&full_list_lock);
@@ -487,11 +481,14 @@ struct parport *parport_register_port(unsigned long base, int irq, int dma,
487481
/*
488482
* Now that the portnum is known finish doing the Init.
489483
*/
490-
sprintf(name, "parport%d", tmp->portnum = tmp->number);
491-
tmp->name = name;
484+
tmp->name = kasprintf(GFP_KERNEL, "parport%d", tmp->portnum);
485+
if (!tmp->name) {
486+
kfree(tmp);
487+
return NULL;
488+
}
489+
dev_set_name(&tmp->bus_dev, tmp->name);
492490
tmp->bus_dev.bus = &parport_bus_type;
493491
tmp->bus_dev.release = free_port;
494-
dev_set_name(&tmp->bus_dev, name);
495492
tmp->bus_dev.type = &parport_device_type;
496493

497494
for (device = 0; device < 5; device++)

include/linux/parport.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,6 @@ struct ieee1284_info {
180180
struct semaphore irq;
181181
};
182182

183-
#define PARPORT_NAME_MAX_LEN 15
184-
185183
/* A parallel port */
186184
struct parport {
187185
unsigned long base; /* base address */

0 commit comments

Comments
 (0)