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.
This commit is contained in:
Thomas Zimmermann 2012-09-26 08:45:06 -07:00
parent 9bb8822425
commit 8070156e24

View File

@ -351,8 +351,8 @@ GetCurrentBatteryInformation(hal::BatteryInformation *aBatteryInfo)
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 {