libbacklight: Fix backlight never gets initialized

In 913d7c15f7 stricter error checking was
introduced to the strtol call, which broke reading backlight values.
Since every sysfs backlight file ends with a newline.

As noted in a comment in the previous MR to prevent damaged pointers
after calling asprintf, replace every asprintf call with str_printf.

Previous-MR: https://gitlab.freedesktop.org/wayland/weston/-/merge_requests/543

Signed-off-by: Sören Meier <soerenmeier@livgood.ch>
This commit is contained in:
Sören Meier 2022-04-05 18:33:40 +02:00 committed by Marius Vlad
parent 6ee6e76a0c
commit edef874696

View File

@ -53,8 +53,10 @@ static long backlight_get(struct backlight *backlight, char *node)
int fd, value;
long ret;
if (asprintf(&path, "%s/%s", backlight->path, node) < 0)
str_printf(&path, "%s/%s", backlight->path, node);
if (!path)
return -ENOMEM;
fd = open(path, O_RDONLY);
if (fd < 0) {
ret = -1;
@ -67,6 +69,9 @@ static long backlight_get(struct backlight *backlight, char *node)
goto out;
}
if (buffer[ret - 1] == '\n')
buffer[ret - 1] = '\0';
if (!safe_strtoint(buffer, &value)) {
ret = -1;
goto out;
@ -103,7 +108,8 @@ long backlight_set_brightness(struct backlight *backlight, long brightness)
int fd;
long ret;
if (asprintf(&path, "%s/%s", backlight->path, "brightness") < 0)
str_printf(&path, "%s/%s", backlight->path, "brightness");
if (!path)
return -ENOMEM;
fd = open(path, O_RDWR);
@ -118,7 +124,8 @@ long backlight_set_brightness(struct backlight *backlight, long brightness)
goto out;
}
if (asprintf(&buffer, "%ld", brightness) < 0) {
str_printf(&buffer, "%ld", brightness);
if (!buffer) {
ret = -1;
goto out;
}
@ -171,7 +178,8 @@ struct backlight *backlight_init(struct udev_device *drm_device,
if (!syspath)
return NULL;
if (asprintf(&path, "%s/%s", syspath, "device") < 0)
str_printf(&path, "%s/%s", syspath, "device");
if (!path)
return NULL;
ret = readlink(path, buffer, sizeof(buffer) - 1);
@ -214,11 +222,13 @@ struct backlight *backlight_init(struct udev_device *drm_device,
if (entry->d_name[0] == '.')
continue;
if (asprintf(&backlight_path, "%s/%s", "/sys/class/backlight",
entry->d_name) < 0)
str_printf(&backlight_path, "%s/%s", "/sys/class/backlight",
entry->d_name);
if (!backlight_path)
goto err;
if (asprintf(&path, "%s/%s", backlight_path, "type") < 0) {
str_printf(&path, "%s/%s", backlight_path, "type");
if (!path) {
free(backlight_path);
goto err;
}
@ -255,7 +265,8 @@ struct backlight *backlight_init(struct udev_device *drm_device,
free (path);
if (asprintf(&path, "%s/%s", backlight_path, "device") < 0)
str_printf(&path, "%s/%s", backlight_path, "device");
if (!path)
goto err;
ret = readlink(path, buffer, sizeof(buffer) - 1);