[PATCH] power: supply: qcom_battmgr: fix battery chemistry strncmp length

Tingguo Cheng posted 1 patch 1 month, 2 weeks ago
drivers/power/supply/qcom_battmgr.c | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
[PATCH] power: supply: qcom_battmgr: fix battery chemistry strncmp length
Posted by Tingguo Cheng 1 month, 2 weeks ago
From: Tingguo Cheng <tingguoc@qti.qualcomm.com>

The battery_chemistry field is a 4-byte array without guaranteed null
termination. Using BATTMGR_CHEMISTRY_LEN (4) as the strncmp length for
3-character string literals implicitly requires chemistry[3] == '\0',
which may not hold. Use 3 instead to match only the significant bytes.

Signed-off-by: Tingguo Cheng <tingguo.cheng@oss.qualcomm.com>
---
 drivers/power/supply/qcom_battmgr.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/drivers/power/supply/qcom_battmgr.c b/drivers/power/supply/qcom_battmgr.c
index 91cf39b0083a..7716eb9e1aff 100644
--- a/drivers/power/supply/qcom_battmgr.c
+++ b/drivers/power/supply/qcom_battmgr.c
@@ -1238,11 +1238,11 @@ static void qcom_battmgr_sc8280xp_strcpy(char *dest, const char *src)
 
 static unsigned int qcom_battmgr_sc8280xp_parse_technology(const char *chemistry)
 {
-	if ((!strncmp(chemistry, "LIO", BATTMGR_CHEMISTRY_LEN)) ||
-	    (!strncmp(chemistry, "OOI", BATTMGR_CHEMISTRY_LEN)))
+	if ((!strncmp(chemistry, "LIO", 3)) ||
+	    (!strncmp(chemistry, "OOI", 3)))
 		return POWER_SUPPLY_TECHNOLOGY_LION;
-	if (!strncmp(chemistry, "LIP", BATTMGR_CHEMISTRY_LEN) ||
-	    !strncmp(chemistry, "LiP", BATTMGR_CHEMISTRY_LEN))
+	if (!strncmp(chemistry, "LIP", 3) ||
+	    !strncmp(chemistry, "LiP", 3))
 		return POWER_SUPPLY_TECHNOLOGY_LIPO;
 
 	pr_err("Unknown battery technology '%s'\n", chemistry);

---
base-commit: 8ca3d2492e247e36fa9a4350fba2010ea5eec6f6
change-id: 20260812-fix-qcom-batt-chemistry-strn-6434c631ecb5

Best regards,
--  
Tingguo Cheng <tingguo.cheng@oss.qualcomm.com>
Re: [PATCH] power: supply: qcom_battmgr: fix battery chemistry strncmp length
Posted by Sebastian Reichel 1 month, 2 weeks ago
On Wed, 12 Aug 2026 00:44:47 -0700, Tingguo Cheng wrote:
> The battery_chemistry field is a 4-byte array without guaranteed null
> termination. Using BATTMGR_CHEMISTRY_LEN (4) as the strncmp length for
> 3-character string literals implicitly requires chemistry[3] == '\0',
> which may not hold. Use 3 instead to match only the significant bytes.

Applied, thanks!

[1/1] power: supply: qcom_battmgr: fix battery chemistry strncmp length
      commit: 37689bcbc41b3850781b6c84f90cb4bd632bfbf0

Best regards,
-- 
Sebastian Reichel <sebastian.reichel@collabora.com>