Bug 794051 - Read battery status via fgets. r=dhylands
authorThomas Zimmermann <tdz@users.sourceforge.net>
Wed, 26 Sep 2012 08:45:06 -0700
changeset 108256 20169e3f33b350e251a8c228a140c5afa51f25e4
parent 108255 745b5180e4ebf5afec3265c50dca3344dcfe3395
child 108257 6351566e60838680b216e7b3bdbd970904ed9fbd
push id82
push usershu@rfrn.org
push dateFri, 05 Oct 2012 13:20:22 +0000
reviewersdhylands
bugs794051
milestone18.0a1
Bug 794051 - Read battery status via fgets. r=dhylands The call to fscanf for reading the battery status used an incorrect pointer in its arguments. The code only worked because the first byte of a character array is the first element within the array. To fix this problem, fscanf has been replaced by fgets, which is less error prone and checks the length of the supplied array. Additionaly, we don't want to operate on an undefined result if fgets fails. In this case we assume that the battery is not being charged.
hal/gonk/GonkHal.cpp
--- a/hal/gonk/GonkHal.cpp
+++ b/hal/gonk/GonkHal.cpp
@@ -346,18 +346,18 @@ GetCurrentBatteryInformation(hal::Batter
     done = true;
   }
 
   if (!done) {
     // toro devices support
     chargingFile = fopen("/sys/class/power_supply/battery/status", "r");
     if (chargingFile) {
       char status[16];
-      fscanf(chargingFile, "%s", &status);
-      if (!strcmp(status, "Charging") || !strcmp(status, "Full")) {
+      char *str = fgets(status, sizeof(status), chargingFile);
+      if (str && (!strcmp(str, "Charging\n") || !strcmp(str, "Full\n"))) {
         // no way here to know if we're charging from USB or AC.
         chargingSrc = BATTERY_CHARGING_USB;
       } else {
         chargingSrc = BATTERY_NOT_CHARGING;
       }
       fclose(chargingFile);
       done = true;
     }