From 8c5eb85136fd2482c5be3c2d249351eae219cca5 Mon Sep 17 00:00:00 2001 From: "Vanya A. Sergeev" Date: Fri, 10 Oct 2025 23:29:50 -0500 Subject: [PATCH] led: fix null termination of internal buffers unlikely to be encountered, but handled correctly now. --- src/led.c | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src/led.c b/src/led.c index 53d2261..a9e6cd8 100644 --- a/src/led.c +++ b/src/led.c @@ -111,18 +111,17 @@ int led_get_brightness(led_t *led, unsigned int *brightness) { if ((fd = open(led_path, O_RDONLY)) < 0) return _led_error(led, LED_ERROR_IO, errno, "Opening LED 'brightness'"); - if ((ret = read(fd, buf, sizeof(buf))) < 0) { + if ((ret = read(fd, buf, sizeof(buf) - 1)) < 0) { int errsv = errno; close(fd); return _led_error(led, LED_ERROR_IO, errsv, "Reading LED 'brightness'"); } + buf[ret] = '\0'; + if (close(fd) < 0) return _led_error(led, LED_ERROR_IO, errno, "Closing LED 'brightness'"); - /* Null-terminate over newline */ - buf[ret] = '\0'; - *brightness = strtoul(buf, NULL, 10); return 0; @@ -138,18 +137,17 @@ int led_get_max_brightness(led_t *led, unsigned int *max_brightness) { if ((fd = open(led_path, O_RDONLY)) < 0) return _led_error(led, LED_ERROR_QUERY, errno, "Opening LED 'max_brightness'"); - if ((ret = read(fd, buf, sizeof(buf))) < 0) { + if ((ret = read(fd, buf, sizeof(buf) - 1)) < 0) { int errsv = errno; close(fd); return _led_error(led, LED_ERROR_QUERY, errsv, "Reading LED 'max_brightness'"); } + buf[ret] = '\0'; + if (close(fd) < 0) return _led_error(led, LED_ERROR_QUERY, errno, "Closing LED 'max_brightness'"); - /* Null-terminate over newline */ - buf[ret] = '\0'; - led->max_brightness = strtoul(buf, NULL, 10); *max_brightness = led->max_brightness;