From a6d03fde18adf458fd1c743d141e13aadf1828cf Mon Sep 17 00:00:00 2001 From: Peter Hutterer Date: Thu, 13 Jul 2017 13:39:02 +1000 Subject: [PATCH] ratbagd: use strdup_safe for all string copies Because if we can't strdup a device name or dbus path, we have problems we can't recover from anyway. Signed-off-by: Peter Hutterer --- meson.build | 3 ++- ratbagd/ratbagd-device.c | 16 ++++------------ ratbagd/ratbagd-profile.c | 31 ++++--------------------------- 3 files changed, 10 insertions(+), 40 deletions(-) diff --git a/meson.build b/meson.build index 60f7689c..4575cedd 100644 --- a/meson.build +++ b/meson.build @@ -437,7 +437,8 @@ src_ratbagd = [ 'ratbagd/ratbagd-button.c', 'ratbagd/ratbagd-device.c', 'ratbagd/ratbagd-profile.c', - 'ratbagd/ratbagd-resolution.c' + 'ratbagd/ratbagd-resolution.c', + 'src/libratbag-util.h' ] deps_ratbagd = [ diff --git a/ratbagd/ratbagd-device.c b/ratbagd/ratbagd-device.c index 5a32d5da..11650b6e 100644 --- a/ratbagd/ratbagd-device.c +++ b/ratbagd/ratbagd-device.c @@ -38,6 +38,8 @@ #include "shared-macro.h" #include "shared-rbtree.h" +#include "libratbag-util.h" + struct ratbagd_device { struct ratbagd *ctx; RBNode node; @@ -104,20 +106,12 @@ static int ratbagd_device_list_profiles(sd_bus *bus, if (!profile) continue; - profiles[i] = strdup(ratbagd_profile_get_path(profile)); - if (!profiles[i]) - goto error; + profiles[i] = strdup_safe(ratbagd_profile_get_path(profile)); } profiles[i] = NULL; *paths = profiles; return 1; - -error: - for (i = 0; profiles[i]; ++i) - free(profiles[i]); - free(profiles); - return -ENOMEM; } static int ratbagd_device_get_device_name(sd_bus *bus, @@ -387,9 +381,7 @@ int ratbagd_device_new(struct ratbagd_device **out, rbnode_init(&device->node); device->lib_device = ratbag_device_ref(lib_device); - device->name = strdup(name); - if (!device->name) - return -ENOMEM; + device->name = strdup_safe(name); r = sd_bus_path_encode(RATBAGD_OBJ_ROOT "/device", device->name, diff --git a/ratbagd/ratbagd-profile.c b/ratbagd/ratbagd-profile.c index 913fb9ae..23acac0f 100644 --- a/ratbagd/ratbagd-profile.c +++ b/ratbagd/ratbagd-profile.c @@ -34,6 +34,7 @@ #include #include #include "ratbagd.h" +#include "libratbag-util.h" #include "shared-macro.h" struct ratbagd_profile { @@ -530,20 +531,12 @@ static int ratbagd_profile_list_resolutions(sd_bus *bus, if (!resolution) continue; - resolutions[i] = strdup(ratbagd_resolution_get_path(resolution)); - if (!resolutions[i]) - goto error; + resolutions[i] = strdup_safe(ratbagd_resolution_get_path(resolution)); } resolutions[i] = NULL; *paths = resolutions; return 1; - -error: - for (i = 0; resolutions[i]; ++i) - free(resolutions[i]); - free(resolutions); - return -ENOMEM; } @@ -607,20 +600,12 @@ static int ratbagd_profile_list_buttons(sd_bus *bus, if (!button) continue; - buttons[i] = strdup(ratbagd_button_get_path(button)); - if (!buttons[i]) - goto error; + buttons[i] = strdup_safe(ratbagd_button_get_path(button)); } buttons[i] = NULL; *paths = buttons; return 1; - -error: - for (i = 0; buttons[i]; ++i) - free(buttons[i]); - free(buttons); - return -ENOMEM; } int ratbagd_profile_register_buttons(struct sd_bus *bus, @@ -683,20 +668,12 @@ static int ratbagd_profile_list_leds(sd_bus *bus, if (!led) continue; - leds[i] = strdup(ratbagd_led_get_path(led)); - if (!leds[i]) - goto error; + leds[i] = strdup_safe(ratbagd_led_get_path(led)); } leds[i] = NULL; *paths = leds; return 1; - -error: - for (i = 0; leds[i]; ++i) - free(leds[i]); - free(leds); - return -ENOMEM; } int ratbagd_profile_register_leds(struct sd_bus *bus,