forked from drath/libratbag
Add strdup_safe() as a reliable strdup function
Handles NULL and guarantees a successful strdup Fixes #14 Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
This commit is contained in:
@@ -186,7 +186,7 @@ ratbag_open_hidraw_node(struct ratbag_device *device, struct udev_device *hidraw
|
||||
device->hidraw.reports = zalloc(reports_size);
|
||||
ratbag_hidraw_parse_report_descriptor(device);
|
||||
|
||||
device->hidraw.sysname = strdup(sysname);
|
||||
device->hidraw.sysname = strdup_safe(sysname);
|
||||
return 0;
|
||||
|
||||
err:
|
||||
|
||||
@@ -99,6 +99,24 @@ zalloc(size_t size)
|
||||
return p;
|
||||
}
|
||||
|
||||
/**
|
||||
* returns NULL if str is NULL, otherwise guarantees a successful strdup.
|
||||
*/
|
||||
static inline char *
|
||||
strdup_safe(const char *str)
|
||||
{
|
||||
char *s;
|
||||
|
||||
if (!str)
|
||||
return NULL;
|
||||
|
||||
s = strdup(str);
|
||||
if (!s)
|
||||
abort();
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
static inline void
|
||||
msleep(unsigned int ms)
|
||||
{
|
||||
|
||||
@@ -157,7 +157,7 @@ ratbag_device_new(struct ratbag *ratbag, struct udev_device *udev_device,
|
||||
struct ratbag_device *device = NULL;
|
||||
|
||||
device = zalloc(sizeof(*device));
|
||||
device->name = strdup(name);
|
||||
device->name = strdup_safe(name);
|
||||
|
||||
if (!name) {
|
||||
free(device);
|
||||
@@ -1190,7 +1190,7 @@ ratbag_button_set_macro(struct ratbag_button *button, const char *name)
|
||||
}
|
||||
|
||||
button->action.type = RATBAG_BUTTON_ACTION_TYPE_MACRO;
|
||||
button->action.macro->name = name ? strdup(name) : NULL;
|
||||
button->action.macro->name = strdup_safe(name);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user