Skip to content

Commit a355a46

Browse files
andy-shevgregkh
authored andcommitted
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 <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent d11f2a1 commit a355a46

File tree

1 file changed

+32
-38
lines changed

1 file changed

+32
-38
lines changed

drivers/base/core.c

Lines changed: 32 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
#include <linux/acpi.h>
1212
#include <linux/blkdev.h>
13+
#include <linux/cleanup.h>
1314
#include <linux/cpufreq.h>
1415
#include <linux/device.h>
1516
#include <linux/dma-map-ops.h> /* for dma_default_coherent */
@@ -563,24 +564,11 @@ static struct class devlink_class = {
563564

564565
static int devlink_add_symlinks(struct device *dev)
565566
{
567+
char *buf_con __free(kfree) = NULL, *buf_sup __free(kfree) = NULL;
566568
int ret;
567-
size_t len;
568569
struct device_link *link = to_devlink(dev);
569570
struct device *sup = link->supplier;
570571
struct device *con = link->consumer;
571-
char *buf;
572-
573-
len = max(strlen(dev_bus_name(sup)) + strlen(dev_name(sup)),
574-
strlen(dev_bus_name(con)) + strlen(dev_name(con)));
575-
len += strlen(":");
576-
/*
577-
* we kzalloc() memory for symlink name of both supplier and
578-
* consumer, so explicitly take into account both prefix.
579-
*/
580-
len += max(strlen("supplier:"), strlen("consumer:")) + 1;
581-
buf = kzalloc(len, GFP_KERNEL);
582-
if (!buf)
583-
return -ENOMEM;
584572

585573
ret = sysfs_create_link(&link->link_dev.kobj, &sup->kobj, "supplier");
586574
if (ret)
@@ -590,58 +578,64 @@ static int devlink_add_symlinks(struct device *dev)
590578
if (ret)
591579
goto err_con;
592580

593-
snprintf(buf, len, "consumer:%s:%s", dev_bus_name(con), dev_name(con));
594-
ret = sysfs_create_link(&sup->kobj, &link->link_dev.kobj, buf);
581+
buf_con = kasprintf(GFP_KERNEL, "consumer:%s:%s", dev_bus_name(con), dev_name(con));
582+
if (!buf_con) {
583+
ret = -ENOMEM;
584+
goto err_con_dev;
585+
}
586+
587+
ret = sysfs_create_link(&sup->kobj, &link->link_dev.kobj, buf_con);
595588
if (ret)
596589
goto err_con_dev;
597590

598-
snprintf(buf, len, "supplier:%s:%s", dev_bus_name(sup), dev_name(sup));
599-
ret = sysfs_create_link(&con->kobj, &link->link_dev.kobj, buf);
591+
buf_sup = kasprintf(GFP_KERNEL, "supplier:%s:%s", dev_bus_name(sup), dev_name(sup));
592+
if (!buf_sup) {
593+
ret = -ENOMEM;
594+
goto err_sup_dev;
595+
}
596+
597+
ret = sysfs_create_link(&con->kobj, &link->link_dev.kobj, buf_sup);
600598
if (ret)
601599
goto err_sup_dev;
602600

603601
goto out;
604602

605603
err_sup_dev:
606-
snprintf(buf, len, "consumer:%s:%s", dev_bus_name(con), dev_name(con));
607-
sysfs_remove_link(&sup->kobj, buf);
604+
sysfs_remove_link(&sup->kobj, buf_con);
608605
err_con_dev:
609606
sysfs_remove_link(&link->link_dev.kobj, "consumer");
610607
err_con:
611608
sysfs_remove_link(&link->link_dev.kobj, "supplier");
612609
out:
613-
kfree(buf);
614610
return ret;
615611
}
616612

617613
static void devlink_remove_symlinks(struct device *dev)
618614
{
615+
char *buf_con __free(kfree) = NULL, *buf_sup __free(kfree) = NULL;
619616
struct device_link *link = to_devlink(dev);
620-
size_t len;
621617
struct device *sup = link->supplier;
622618
struct device *con = link->consumer;
623-
char *buf;
624619

625620
sysfs_remove_link(&link->link_dev.kobj, "consumer");
626621
sysfs_remove_link(&link->link_dev.kobj, "supplier");
627622

628-
len = max(strlen(dev_bus_name(sup)) + strlen(dev_name(sup)),
629-
strlen(dev_bus_name(con)) + strlen(dev_name(con)));
630-
len += strlen(":");
631-
len += max(strlen("supplier:"), strlen("consumer:")) + 1;
632-
buf = kzalloc(len, GFP_KERNEL);
633-
if (!buf) {
634-
WARN(1, "Unable to properly free device link symlinks!\n");
635-
return;
636-
}
637-
638623
if (device_is_registered(con)) {
639-
snprintf(buf, len, "supplier:%s:%s", dev_bus_name(sup), dev_name(sup));
640-
sysfs_remove_link(&con->kobj, buf);
624+
buf_sup = kasprintf(GFP_KERNEL, "supplier:%s:%s", dev_bus_name(sup), dev_name(sup));
625+
if (!buf_sup)
626+
goto out;
627+
sysfs_remove_link(&con->kobj, buf_sup);
641628
}
642-
snprintf(buf, len, "consumer:%s:%s", dev_bus_name(con), dev_name(con));
643-
sysfs_remove_link(&sup->kobj, buf);
644-
kfree(buf);
629+
630+
buf_con = kasprintf(GFP_KERNEL, "consumer:%s:%s", dev_bus_name(con), dev_name(con));
631+
if (!buf_con)
632+
goto out;
633+
sysfs_remove_link(&sup->kobj, buf_con);
634+
635+
return;
636+
637+
out:
638+
WARN(1, "Unable to properly free device link symlinks!\n");
645639
}
646640

647641
static struct class_interface devlink_class_intf = {

0 commit comments

Comments
 (0)