battery plugin: null-terminate buffer
authorRuben Kerkhof <ruben@rubenkerkhof.com>
Sat, 17 Dec 2016 17:39:39 +0000 (18:39 +0100)
committerRuben Kerkhof <ruben@rubenkerkhof.com>
Sat, 17 Dec 2016 17:39:39 +0000 (18:39 +0100)
We call strstripnewline on it, which expects a null-terminated string.

CID #158524

src/battery.c

index 5c02fee..78e96b2 100644 (file)
@@ -350,10 +350,12 @@ static int sysfs_file_to_buffer(char const *dir, /* {{{ */
   ssnprintf(filename, sizeof(filename), "%s/%s/%s", dir, power_supply,
             basename);
 
-  status = (int)read_file_contents(filename, buffer, buffer_size);
+  status = (int)read_file_contents(filename, buffer, buffer_size - 1);
   if (status < 0)
     return status;
 
+  buffer[status] = '\0';
+
   strstripnewline(buffer);
   return 0;
 } /* }}} int sysfs_file_to_buffer */
@@ -364,7 +366,7 @@ static int sysfs_file_to_gauge(char const *dir, /* {{{ */
                                char const *power_supply, char const *basename,
                                gauge_t *ret_value) {
   int status;
-  char buffer[32] = "";
+  char buffer[32];
 
   status =
       sysfs_file_to_buffer(dir, power_supply, basename, buffer, sizeof(buffer));