mbpfan-josie

Fix out-of-bounds write when reading sensors

refresh_sensors wrote the terminating NUL at buf[len] without checking
the pread result.  A failed read (e.g., a core going offline) wrote to
buf[-1] and a full 16-byte read wrote one past the end.  Reserve space
for the terminator and keep the previous temperature on error.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

bf6650e3abcf09540fc94110d29d178986fd5276
Andrew Gaul <andrew@gaul.org> · 2026-11-08T11:26 · browse files at this commit

parents: afb8bbb

diff --git a/src/mbpfan.c b/src/mbpfan.c
index b45788c..8d1226a 100644
--- a/src/mbpfan.c
+++ b/src/mbpfan.c
@@ -405,9 +405,11 @@ t_sensors *refresh_sensors(t_sensors *sensors)
     while (tmp != NULL) {
         if (tmp->file != NULL) {
             char buf[16];
-            int len = pread(fileno(tmp->file), buf, sizeof(buf), /*offset=*/0);
-            buf[len] = '\0';
-            tmp->temperature = strtod(buf, NULL);
+            ssize_t len = pread(fileno(tmp->file), buf, sizeof(buf) - 1, /*offset=*/0);
+            if (len >= 0) {
+                buf[len] = '\0';
+                tmp->temperature = strtod(buf, NULL);
+            }
         }
 
         tmp = tmp->next;