From nobody Tue Apr 16 23:40:08 2024 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4VK0rm3kvrz5HLR8; Tue, 16 Apr 2024 23:40:08 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4VK0rm2qn7z41kP; Tue, 16 Apr 2024 23:40:08 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1713310808; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=2RnoMMenQspH/ObmnDf7IXVrJV8pvwsMqWVOvlKGwhw=; b=gPweH3rMpmwLyQUA23Jf4rMOBdKki744RMVEaaoeCv/p7dRTWQGMizORYALHFhputtqtyq +7FOPMj4UQT/ZDGHoPTTA2pvPwZt6wn+xqdJw+RFoLAECprt2hV8opAlLVQAaoRG3EutJy XN3eq8r0uWQDN9XP5wwYF7G342ap7KJMA42mZaeKxBKshrqciinivuSAXQH04U+rXvPPhu n/VxGi7CnfJtTTPprqOPMvAfODfEy7TXL8h2Ls6L6KQi8tgMDFksy88PlovyFsbivbVQUi 1tCt9dn03jEcrrSZismuvYgvtHdoa0HN0e4ODHuWevpgFE4ngmENVw7Es/ViTA== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1713310808; a=rsa-sha256; cv=none; b=VnOsfFCJoGJ3o4zKrdbHoCYYmQw5xrVhsfyVhFENF+L0/KkiJ9+ULZr5OtCWkZ+QDbvcNF pZvyyT8TPVIOEpokp68WQ4WwcZx0v1gZbGxIPcCxV8L2KXiTqiVAAwmtcsPCT/CqgXQLRg 3nuDHYxbBojHQzvn3kYARp8e3TV08dVJ6fPFCHpZm8G68UF57lhmHrsQWefvq2rZvelIm0 MQ3bXQAOItdjXcQ9qte8n82ha2Zqn6l1YonbauD84RYBSKh7B3myeu2f5GDcm5b/ijzKeI bSlN/RZol6tqGmuKwFjcD6NJNCckCY/6N83aesdErcR+QA4on6blE3vs9KW1Rg== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1713310808; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=2RnoMMenQspH/ObmnDf7IXVrJV8pvwsMqWVOvlKGwhw=; b=f0cpGptbFIQ+GFbznvcrL92ItO2rZyRe8ieETxvdN1CQq5qpBaDNvYPUV05QgOyp3/nZsj AedOuST5MWzyvTP6BESCwHVst5L+HAIM4bm7agJ/eG0gMyfb2hRkiMoIyzlO+W7qbQQsxe 1HEsDRclvy+gIIR3VRL/6suASyk0Qzuc+mQN068NdK2A+wUAheQD7/Ux6ZRbfNuSGEe6dF KqFh2JWGcyd2J8x5ToZGK1ANRg7cF2anIG50FaNVVaDYki6/OfR+FfSIHadeLt1AQIKweV BtlAQWAp65Po5qoIwugoyAg5IdhOjQpYWdKqFNl+16xlbhGL13zqsgbxWQIG5Q== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4VK0rm2Qwkz13L3; Tue, 16 Apr 2024 23:40:08 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.17.1/8.17.1) with ESMTP id 43GNe8h9084761; Tue, 16 Apr 2024 23:40:08 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 43GNe8tn084756; Tue, 16 Apr 2024 23:40:08 GMT (envelope-from git) Date: Tue, 16 Apr 2024 23:40:08 GMT Message-Id: <202404162340.43GNe8tn084756@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Ed Maste Subject: git: 2e850b832f5d - main - acpi_battery: avoid divide-by-zero when no devices have capacity info List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-all@freebsd.org Sender: owner-dev-commits-src-all@FreeBSD.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: emaste X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 2e850b832f5d2adb9b230d191277d67c00caaab9 Auto-Submitted: auto-generated The branch main has been updated by emaste: URL: https://cgit.FreeBSD.org/src/commit/?id=2e850b832f5d2adb9b230d191277d67c00caaab9 commit 2e850b832f5d2adb9b230d191277d67c00caaab9 Author: Josef 'Jeff' Sipek AuthorDate: 2024-04-16 23:01:28 +0000 Commit: Ed Maste CommitDate: 2024-04-16 23:39:45 +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 --- 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;