summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJos Dehaes <jos.dehaes@gmail.com>2021-10-06 15:13:18 +0200
committerJos Dehaes <jos.dehaes@gmail.com>2021-10-06 15:13:18 +0200
commita5f10f1a0ff9d10fa8d9d3038aa160b2380aa294 (patch)
treec4303c9dbfcee6c9ec2d91b0c9fd484bbdcfc4fe
parent155c848b97e1385bd29eaba91a87ecfb1d846bf7 (diff)
check array length
-rw-r--r--src/osx/btop_collect.cpp42
1 files changed, 23 insertions, 19 deletions
diff --git a/src/osx/btop_collect.cpp b/src/osx/btop_collect.cpp
index 900c8ec..94a5bd2 100644
--- a/src/osx/btop_collect.cpp
+++ b/src/osx/btop_collect.cpp
@@ -340,27 +340,31 @@ namespace Cpu {
if (ps_info) {
CFArrayRef one_ps_descriptor = IOPSCopyPowerSourcesList(ps_info);
if (one_ps_descriptor) {
- CFDictionaryRef one_ps = IOPSGetPowerSourceDescription(ps_info, CFArrayGetValueAtIndex(one_ps_descriptor, 0));
- has_battery = true;
- CFNumberRef remaining = (CFNumberRef)CFDictionaryGetValue(one_ps, CFSTR(kIOPSTimeToEmptyKey));
- int32_t estimatedMinutesRemaining;
- if (remaining) {
- CFNumberGetValue(remaining, kCFNumberSInt32Type, &estimatedMinutesRemaining);
- seconds = estimatedMinutesRemaining * 60;
- }
- CFNumberRef charge = (CFNumberRef)CFDictionaryGetValue(one_ps, CFSTR(kIOPSCurrentCapacityKey));
- if (charge) {
- CFNumberGetValue(charge, kCFNumberSInt32Type, &percent);
- }
- CFBooleanRef charging = (CFBooleanRef)CFDictionaryGetValue(one_ps, CFSTR(kIOPSIsChargingKey));
- if (charging) {
- bool isCharging = CFBooleanGetValue(charging);
- if (isCharging) {
- status = "charging";
- if (percent == 100) {
- status = "full";
+ if (CFArrayGetCount(one_ps_descriptor)) {
+ CFDictionaryRef one_ps = IOPSGetPowerSourceDescription(ps_info, CFArrayGetValueAtIndex(one_ps_descriptor, 0));
+ has_battery = true;
+ CFNumberRef remaining = (CFNumberRef)CFDictionaryGetValue(one_ps, CFSTR(kIOPSTimeToEmptyKey));
+ int32_t estimatedMinutesRemaining;
+ if (remaining) {
+ CFNumberGetValue(remaining, kCFNumberSInt32Type, &estimatedMinutesRemaining);
+ seconds = estimatedMinutesRemaining * 60;
+ }
+ CFNumberRef charge = (CFNumberRef)CFDictionaryGetValue(one_ps, CFSTR(kIOPSCurrentCapacityKey));
+ if (charge) {
+ CFNumberGetValue(charge, kCFNumberSInt32Type, &percent);
+ }
+ CFBooleanRef charging = (CFBooleanRef)CFDictionaryGetValue(one_ps, CFSTR(kIOPSIsChargingKey));
+ if (charging) {
+ bool isCharging = CFBooleanGetValue(charging);
+ if (isCharging) {
+ status = "charging";
+ if (percent == 100) {
+ status = "full";
+ }
}
}
+ } else {
+ has_battery = false;
}
CFRelease(one_ps_descriptor);
} else {