From edef87469648b1c14f6049e7aa2b81c547794fc9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6ren=20Meier?= Date: Tue, 5 Apr 2022 18:33:40 +0200 Subject: [PATCH] libbacklight: Fix backlight never gets initialized MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit In 913d7c15f79da33aa97100286df5d8f88731e252 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 --- libweston/backend-drm/libbacklight.c | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/libweston/backend-drm/libbacklight.c b/libweston/backend-drm/libbacklight.c index 4bbc6db4..ca7f2d68 100644 --- a/libweston/backend-drm/libbacklight.c +++ b/libweston/backend-drm/libbacklight.c @@ -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);