Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 24 Apr 2024 14:06:43 GMT
From:      Ed Maste <emaste@FreeBSD.org>
To:        src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org
Subject:   git: 1a4e512adbab - stable/13 - acpi_battery: avoid divide-by-zero when no devices have capacity info
Message-ID:  <202404241406.43OE6hE9039309@gitrepo.freebsd.org>

next in thread | raw e-mail | index | archive | help
The branch stable/13 has been updated by emaste:

URL: https://cgit.FreeBSD.org/src/commit/?id=1a4e512adbab4dfc072d6331a7483f4188fb96ce

commit 1a4e512adbab4dfc072d6331a7483f4188fb96ce
Author:     Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
AuthorDate: 2024-04-16 23:01:28 +0000
Commit:     Ed Maste <emaste@FreeBSD.org>
CommitDate: 2024-04-24 14:06:08 +0000

    acpi_battery: avoid divide-by-zero when no devices have capacity info
    
    On laptops with builtin batteries, disconnecting the battery may show up
    as a battery without any capacity information. (The theory is that one
    is disconnecting the cells but the electronics identifying the battery
    are still connected.) As a result, the loop over all batteries in
    acpi_battery_get_battinfo results in total_lfcap == 0.
    
    So, just check that total_lfcap is non-zero to avoid a division by zero
    (triggerable by sysctl hw.acpi.battery).
    
    Reported by:    Stefano Marinelli
    Tested by:      Stefano Marinelli
    Reviewed by:    emaste
    Differential Revision: https://reviews.freebsd.org/D44818
    
    (cherry picked from commit 2e850b832f5d2adb9b230d191277d67c00caaab9)
    (cherry picked from commit 788987e034b1c73d779a3aa179f79f672bbdb366)
---
 sys/dev/acpica/acpi_battery.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/sys/dev/acpica/acpi_battery.c b/sys/dev/acpica/acpi_battery.c
index 7455c6afc6a6..cfd8261d5eab 100644
--- a/sys/dev/acpica/acpi_battery.c
+++ b/sys/dev/acpica/acpi_battery.c
@@ -266,7 +266,14 @@ acpi_battery_get_battinfo(device_t dev, struct acpi_battinfo *battinfo)
      */
     if (valid_units > 0) {
 	if (dev == NULL) {
-	    battinfo->cap = (total_cap * 100) / total_lfcap;
+	    /*
+	     * Avoid division by zero if none of the batteries had valid
+	     * capacity info.
+	     */
+	    if (total_lfcap > 0)
+		battinfo->cap = (total_cap * 100) / total_lfcap;
+	    else
+		battinfo->cap = 0;
 	    battinfo->min = total_min;
 	    battinfo->state = batt_stat;
 	    battinfo->rate = valid_rate;



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?202404241406.43OE6hE9039309>