mbpfan-josie

Prefer smprintf instead of unsafe sprintf

Previously checking fans >= 10 overflowed the buffer and corrupted the
heap.  Found via AddressSanitizer.  Regression from
968133eb103ce543e2fab6c55e306be023509452.

559a2f39f10e19c38deceb4f81b746012dcf2f56
Andrew Gaul <andrew@gaul.org> · 2019-10-07T19:48 · browse files at this commit

parents: 272ef08

diff --git a/src/mbpfan.c b/src/mbpfan.c
index 85879ac..b9da5bf 100644
--- a/src/mbpfan.c
+++ b/src/mbpfan.c
@@ -167,9 +167,7 @@ t_sensors *retrieve_sensors()
 	    int counter;
 	    for (counter = 0; counter < NUM_HWMONS; counter++) {
 
-		char hwmon_path[strlen(path_begin)+2];
-
-		sprintf(hwmon_path, "%s%d", path_begin, counter);
+                char *hwmon_path = smprintf("%s%d", path_begin, counter);
 
 		int res = access(hwmon_path, R_OK);
 		if (res == 0) {
@@ -186,8 +184,11 @@ t_sensors *retrieve_sensors()
 
 		    }
 
+                    free(hwmon_path);
 		    break;
 		}
+
+                free(hwmon_path);
 	    }
 
 	    int core = 0;