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

Configure Feed

Select the types of activity you want to include in your feed.

driver core: 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/20240821154839.604259-3-andriy.shevchenko@linux.intel.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

authored by

Andy Shevchenko and committed by
Greg Kroah-Hartman
a355a465 d11f2a1a

+32 -38
+32 -38
drivers/base/core.c
··· 10 10 11 11 #include <linux/acpi.h> 12 12 #include <linux/blkdev.h> 13 + #include <linux/cleanup.h> 13 14 #include <linux/cpufreq.h> 14 15 #include <linux/device.h> 15 16 #include <linux/dma-map-ops.h> /* for dma_default_coherent */ ··· 564 563 565 564 static int devlink_add_symlinks(struct device *dev) 566 565 { 566 + char *buf_con __free(kfree) = NULL, *buf_sup __free(kfree) = NULL; 567 567 int ret; 568 - size_t len; 569 568 struct device_link *link = to_devlink(dev); 570 569 struct device *sup = link->supplier; 571 570 struct device *con = link->consumer; 572 - char *buf; 573 - 574 - len = max(strlen(dev_bus_name(sup)) + strlen(dev_name(sup)), 575 - strlen(dev_bus_name(con)) + strlen(dev_name(con))); 576 - len += strlen(":"); 577 - /* 578 - * we kzalloc() memory for symlink name of both supplier and 579 - * consumer, so explicitly take into account both prefix. 580 - */ 581 - len += max(strlen("supplier:"), strlen("consumer:")) + 1; 582 - buf = kzalloc(len, GFP_KERNEL); 583 - if (!buf) 584 - return -ENOMEM; 585 571 586 572 ret = sysfs_create_link(&link->link_dev.kobj, &sup->kobj, "supplier"); 587 573 if (ret) ··· 578 590 if (ret) 579 591 goto err_con; 580 592 581 - snprintf(buf, len, "consumer:%s:%s", dev_bus_name(con), dev_name(con)); 582 - ret = sysfs_create_link(&sup->kobj, &link->link_dev.kobj, buf); 593 + buf_con = kasprintf(GFP_KERNEL, "consumer:%s:%s", dev_bus_name(con), dev_name(con)); 594 + if (!buf_con) { 595 + ret = -ENOMEM; 596 + goto err_con_dev; 597 + } 598 + 599 + ret = sysfs_create_link(&sup->kobj, &link->link_dev.kobj, buf_con); 583 600 if (ret) 584 601 goto err_con_dev; 585 602 586 - snprintf(buf, len, "supplier:%s:%s", dev_bus_name(sup), dev_name(sup)); 587 - ret = sysfs_create_link(&con->kobj, &link->link_dev.kobj, buf); 603 + buf_sup = kasprintf(GFP_KERNEL, "supplier:%s:%s", dev_bus_name(sup), dev_name(sup)); 604 + if (!buf_sup) { 605 + ret = -ENOMEM; 606 + goto err_sup_dev; 607 + } 608 + 609 + ret = sysfs_create_link(&con->kobj, &link->link_dev.kobj, buf_sup); 588 610 if (ret) 589 611 goto err_sup_dev; 590 612 591 613 goto out; 592 614 593 615 err_sup_dev: 594 - snprintf(buf, len, "consumer:%s:%s", dev_bus_name(con), dev_name(con)); 595 - sysfs_remove_link(&sup->kobj, buf); 616 + sysfs_remove_link(&sup->kobj, buf_con); 596 617 err_con_dev: 597 618 sysfs_remove_link(&link->link_dev.kobj, "consumer"); 598 619 err_con: 599 620 sysfs_remove_link(&link->link_dev.kobj, "supplier"); 600 621 out: 601 - kfree(buf); 602 622 return ret; 603 623 } 604 624 605 625 static void devlink_remove_symlinks(struct device *dev) 606 626 { 627 + char *buf_con __free(kfree) = NULL, *buf_sup __free(kfree) = NULL; 607 628 struct device_link *link = to_devlink(dev); 608 - size_t len; 609 629 struct device *sup = link->supplier; 610 630 struct device *con = link->consumer; 611 - char *buf; 612 631 613 632 sysfs_remove_link(&link->link_dev.kobj, "consumer"); 614 633 sysfs_remove_link(&link->link_dev.kobj, "supplier"); 615 634 616 - len = max(strlen(dev_bus_name(sup)) + strlen(dev_name(sup)), 617 - strlen(dev_bus_name(con)) + strlen(dev_name(con))); 618 - len += strlen(":"); 619 - len += max(strlen("supplier:"), strlen("consumer:")) + 1; 620 - buf = kzalloc(len, GFP_KERNEL); 621 - if (!buf) { 622 - WARN(1, "Unable to properly free device link symlinks!\n"); 623 - return; 635 + if (device_is_registered(con)) { 636 + buf_sup = kasprintf(GFP_KERNEL, "supplier:%s:%s", dev_bus_name(sup), dev_name(sup)); 637 + if (!buf_sup) 638 + goto out; 639 + sysfs_remove_link(&con->kobj, buf_sup); 624 640 } 625 641 626 - if (device_is_registered(con)) { 627 - snprintf(buf, len, "supplier:%s:%s", dev_bus_name(sup), dev_name(sup)); 628 - sysfs_remove_link(&con->kobj, buf); 629 - } 630 - snprintf(buf, len, "consumer:%s:%s", dev_bus_name(con), dev_name(con)); 631 - sysfs_remove_link(&sup->kobj, buf); 632 - kfree(buf); 642 + buf_con = kasprintf(GFP_KERNEL, "consumer:%s:%s", dev_bus_name(con), dev_name(con)); 643 + if (!buf_con) 644 + goto out; 645 + sysfs_remove_link(&sup->kobj, buf_con); 646 + 647 + return; 648 + 649 + out: 650 + WARN(1, "Unable to properly free device link symlinks!\n"); 633 651 } 634 652 635 653 static struct class_interface devlink_class_intf = {