Bug 794051 - Read battery status via fgets. r=dhylands
authorThomas Zimmermann <tdz@users.sourceforge.net>
Wed, 26 Sep 2012 08:45:06 -0700
changeset 108147 20169e3f33b350e251a8c228a140c5afa51f25e4
parent 108146 745b5180e4ebf5afec3265c50dca3344dcfe3395
child 108148 6351566e60838680b216e7b3bdbd970904ed9fbd
push id23539
push userryanvm@gmail.com
push dateWed, 26 Sep 2012 22:55:55 +0000
treeherdermozilla-central@ec079fd92224 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersdhylands
bugs794051
milestone18.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
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;
     }