Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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>

authored by

Andy Shevchenko and committed by
Greg Kroah-Hartman
8d8ae17e c1426d39

+15 -55
+9 -44
drivers/parport/procfs.c
··· 32 32 #define PARPORT_MAX_TIMESLICE_VALUE ((unsigned long) HZ) 33 33 #define PARPORT_MIN_SPINTIME_VALUE 1 34 34 #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 42 35 43 36 static int do_active_device(struct ctl_table *table, int write, 44 37 void *result, size_t *lenp, loff_t *ppos) ··· 424 431 { 425 432 struct parport_sysctl_table *t; 426 433 char *tmp_dir_path; 427 - size_t tmp_path_len, port_name_len; 428 - int bytes_written, i, err = 0; 434 + int i, err = 0; 429 435 430 436 t = kmemdup(&parport_sysctl_template, sizeof(*t), GFP_KERNEL); 431 437 if (t == NULL) ··· 438 446 t->vars[5 + i].extra2 = &port->probe_info[i]; 439 447 } 440 448 441 - port_name_len = strnlen(port->name, PARPORT_NAME_MAX_LEN); 442 - /* 443 - * Allocate a buffer for two paths: dev/parport/PORT and dev/parport/PORT/devices. 444 - * We calculate for the second as that will give us enough for the first. 445 - */ 446 - tmp_path_len = PARPORT_BASE_DEVICES_PATH_SIZE + port_name_len; 447 - tmp_dir_path = kzalloc(tmp_path_len, GFP_KERNEL); 449 + tmp_dir_path = kasprintf(GFP_KERNEL, "dev/parport/%s/devices", port->name); 448 450 if (!tmp_dir_path) { 449 451 err = -ENOMEM; 450 452 goto exit_free_t; 451 453 } 452 454 453 - bytes_written = snprintf(tmp_dir_path, tmp_path_len, 454 - "dev/parport/%s/devices", port->name); 455 - if (tmp_path_len <= bytes_written) { 456 - err = -ENOENT; 457 - goto exit_free_tmp_dir_path; 458 - } 459 455 t->devices_header = register_sysctl(tmp_dir_path, t->device_dir); 460 456 if (t->devices_header == NULL) { 461 457 err = -ENOENT; 462 458 goto exit_free_tmp_dir_path; 463 459 } 464 460 465 - tmp_path_len = PARPORT_BASE_PATH_SIZE + port_name_len; 466 - bytes_written = snprintf(tmp_dir_path, tmp_path_len, 467 - "dev/parport/%s", port->name); 468 - if (tmp_path_len <= bytes_written) { 469 - err = -ENOENT; 461 + kfree(tmp_dir_path); 462 + 463 + tmp_dir_path = kasprintf(GFP_KERNEL, "dev/parport/%s", port->name); 464 + if (!tmp_dir_path) { 465 + err = -ENOMEM; 470 466 goto unregister_devices_h; 471 467 } 472 468 ··· 494 514 495 515 int parport_device_proc_register(struct pardevice *device) 496 516 { 497 - int bytes_written, err = 0; 498 517 struct parport_device_sysctl_table *t; 499 518 struct parport * port = device->port; 500 - size_t port_name_len, device_name_len, tmp_dir_path_len; 501 519 char *tmp_dir_path; 520 + int err = 0; 502 521 503 522 t = kmemdup(&parport_device_sysctl_template, sizeof(*t), GFP_KERNEL); 504 523 if (t == NULL) 505 524 return -ENOMEM; 506 525 507 - port_name_len = strnlen(port->name, PARPORT_NAME_MAX_LEN); 508 - device_name_len = strnlen(device->name, PATH_MAX); 509 - 510 526 /* Allocate a buffer for two paths: dev/parport/PORT/devices/DEVICE. */ 511 - tmp_dir_path_len = PARPORT_BASE_DEVICES_PATH_SIZE + port_name_len + device_name_len; 512 - tmp_dir_path = kzalloc(tmp_dir_path_len, GFP_KERNEL); 527 + tmp_dir_path = kasprintf(GFP_KERNEL, "dev/parport/%s/devices/%s", port->name, device->name); 513 528 if (!tmp_dir_path) { 514 529 err = -ENOMEM; 515 530 goto exit_free_t; 516 - } 517 - 518 - bytes_written = snprintf(tmp_dir_path, tmp_dir_path_len, "dev/parport/%s/devices/%s", 519 - port->name, device->name); 520 - if (tmp_dir_path_len <= bytes_written) { 521 - err = -ENOENT; 522 - goto exit_free_path; 523 531 } 524 532 525 533 t->vars[0].data = &device->timeslice; ··· 521 553 522 554 kfree(tmp_dir_path); 523 555 return 0; 524 - 525 - exit_free_path: 526 - kfree(tmp_dir_path); 527 556 528 557 exit_free_t: 529 558 kfree(t);
+6 -9
drivers/parport/share.c
··· 438 438 struct parport *tmp; 439 439 int num; 440 440 int device; 441 - char *name; 442 441 int ret; 443 442 444 443 tmp = kzalloc(sizeof(struct parport), GFP_KERNEL); ··· 466 467 atomic_set(&tmp->ref_count, 1); 467 468 INIT_LIST_HEAD(&tmp->full_list); 468 469 469 - name = kmalloc(PARPORT_NAME_MAX_LEN, GFP_KERNEL); 470 - if (!name) { 471 - kfree(tmp); 472 - return NULL; 473 - } 474 470 /* Search for the lowest free parport number. */ 475 471 476 472 spin_lock(&full_list_lock); ··· 481 487 /* 482 488 * Now that the portnum is known finish doing the Init. 483 489 */ 484 - sprintf(name, "parport%d", tmp->portnum = tmp->number); 485 - tmp->name = name; 490 + tmp->name = kasprintf(GFP_KERNEL, "parport%d", tmp->portnum); 491 + if (!tmp->name) { 492 + kfree(tmp); 493 + return NULL; 494 + } 495 + dev_set_name(&tmp->bus_dev, tmp->name); 486 496 tmp->bus_dev.bus = &parport_bus_type; 487 497 tmp->bus_dev.release = free_port; 488 - dev_set_name(&tmp->bus_dev, name); 489 498 tmp->bus_dev.type = &parport_device_type; 490 499 491 500 for (device = 0; device < 5; device++)
-2
include/linux/parport.h
··· 180 180 struct semaphore irq; 181 181 }; 182 182 183 - #define PARPORT_NAME_MAX_LEN 15 184 - 185 183 /* A parallel port */ 186 184 struct parport { 187 185 unsigned long base; /* base address */