check array length

This commit is contained in:
Jos Dehaes 2021-10-06 15:13:18 +02:00
parent 155c848b97
commit a5f10f1a0f

View file

@ -340,27 +340,31 @@ namespace Cpu {
if (ps_info) { if (ps_info) {
CFArrayRef one_ps_descriptor = IOPSCopyPowerSourcesList(ps_info); CFArrayRef one_ps_descriptor = IOPSCopyPowerSourcesList(ps_info);
if (one_ps_descriptor) { if (one_ps_descriptor) {
CFDictionaryRef one_ps = IOPSGetPowerSourceDescription(ps_info, CFArrayGetValueAtIndex(one_ps_descriptor, 0)); if (CFArrayGetCount(one_ps_descriptor)) {
has_battery = true; CFDictionaryRef one_ps = IOPSGetPowerSourceDescription(ps_info, CFArrayGetValueAtIndex(one_ps_descriptor, 0));
CFNumberRef remaining = (CFNumberRef)CFDictionaryGetValue(one_ps, CFSTR(kIOPSTimeToEmptyKey)); has_battery = true;
int32_t estimatedMinutesRemaining; CFNumberRef remaining = (CFNumberRef)CFDictionaryGetValue(one_ps, CFSTR(kIOPSTimeToEmptyKey));
if (remaining) { int32_t estimatedMinutesRemaining;
CFNumberGetValue(remaining, kCFNumberSInt32Type, &estimatedMinutesRemaining); if (remaining) {
seconds = estimatedMinutesRemaining * 60; CFNumberGetValue(remaining, kCFNumberSInt32Type, &estimatedMinutesRemaining);
} seconds = estimatedMinutesRemaining * 60;
CFNumberRef charge = (CFNumberRef)CFDictionaryGetValue(one_ps, CFSTR(kIOPSCurrentCapacityKey)); }
if (charge) { CFNumberRef charge = (CFNumberRef)CFDictionaryGetValue(one_ps, CFSTR(kIOPSCurrentCapacityKey));
CFNumberGetValue(charge, kCFNumberSInt32Type, &percent); if (charge) {
} CFNumberGetValue(charge, kCFNumberSInt32Type, &percent);
CFBooleanRef charging = (CFBooleanRef)CFDictionaryGetValue(one_ps, CFSTR(kIOPSIsChargingKey)); }
if (charging) { CFBooleanRef charging = (CFBooleanRef)CFDictionaryGetValue(one_ps, CFSTR(kIOPSIsChargingKey));
bool isCharging = CFBooleanGetValue(charging); if (charging) {
if (isCharging) { bool isCharging = CFBooleanGetValue(charging);
status = "charging"; if (isCharging) {
if (percent == 100) { status = "charging";
status = "full"; if (percent == 100) {
status = "full";
}
} }
} }
} else {
has_battery = false;
} }
CFRelease(one_ps_descriptor); CFRelease(one_ps_descriptor);
} else { } else {