Skip to contentSkip to conversation

acpi: battery: Sanitise model_number by dropping unprintable characters

PATCH Superseded Kate Hsuan 2 participants 3 messages Jul 8 – Jul 10 ACPI

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Kate Hsuan

author
#0 lore raw

PATCH 1/1 acpi: battery: Sanitise model_number by dropping unprintable characters

The battery Embedded Controller (EC) may return the model name with trailing unprintable or non-ASCII characters. For example, on some systems:

$ cat /sys/class/power_supply/BAT0/model_name
LNV-5B10W51864��

Strip these unprintable characters to ensure the model_name attribute contains a valid string. If left intact, these malformed strings prevent udev rules and hwdb from matching the attribute correctly.

---
 drivers/acpi/battery.c | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
drivers/acpi/battery.c +19 -0
index f5e0eb299610..dea0c3cc5160 100644
@@ -10,6 +10,7 @@
 
 #define pr_fmt(fmt) "ACPI: battery: " fmt
 
+#include <linux/ctype.h>
 #include <linux/delay.h>
 #include <linux/dmi.h>
 #include <linux/jiffies.h>
@@ -194,6 +195,21 @@ static int acpi_battery_handle_discharging(struct acpi_battery *battery)
 	return POWER_SUPPLY_STATUS_DISCHARGING;
 }
 
+static void acpi_battery_remove_special_chars(char *str)
+{
+	char *out = str;
+	unsigned int i = 0;
+
+	while (*str && i < MAX_STRING_LENGTH) {
+		unsigned char c = *str++;
+
+		if (isascii(c) && isprint(c))
+			*out++ = c;
+		i++;
+	}
+	*out = '\0';
+}
+
 static int acpi_battery_get_property(struct power_supply *psy,
 				     enum power_supply_property psp,
 				     union power_supply_propval *val)
@@ -524,6 +540,9 @@ static int extract_battery_info(const int use_bix,
 	    battery->capacity_now > battery->full_charge_capacity)
 		battery->capacity_now = battery->full_charge_capacity;
 
+	if (!result)
+		acpi_battery_remove_special_chars(battery->model_number);
+
 	return result;
 }
 
Signature · 1 line
2.55.0
#1 lore raw
Quoted 6 lines · Kate Hsuan

The battery Embedded Controller (EC) may return the model name with trailing unprintable or non-ASCII characters. For example, on some systems:

$ cat /sys/class/power_supply/BAT0/model_name
LNV-5B10W51864��

If they are just trailing and not in the middle of the string, you could just find the first one and overwrite it with a NUL character.

Rewriting the entire string is overkill in that case.

Quoted 24 lines

Strip these unprintable characters to ensure the model_name attribute contains a valid string. If left intact, these malformed strings prevent udev rules and hwdb from matching the attribute correctly.

---
 drivers/acpi/battery.c | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
drivers/acpi/battery.c +1 -0
index f5e0eb299610..dea0c3cc5160 100644
@@ -10,6 +10,7 @@
 
 #define pr_fmt(fmt) "ACPI: battery: " fmt
 
+#include <linux/ctype.h>
 #include <linux/delay.h>
 #include <linux/dmi.h>
 #include <linux/jiffies.h>

drivers/acpi/battery.c static int acpi_battery_handle_discharging(struct acpi_battery *battery) · line 202

Quoted 8 lines
drivers/acpi/battery.c +5 -0
@@ -194,6 +195,21 @@ static int acpi_battery_handle_discharging(struct acpi_battery *battery)
        return POWER_SUPPLY_STATUS_DISCHARGING;
 }
 
+static void acpi_battery_remove_special_chars(char *str)
+{
+       char *out = str;
+       unsigned int i = 0;
+

And I would use a for () loop below anyway.

Quoted 23 lines
drivers/acpi/battery.c +13 -0
+       while (*str && i < MAX_STRING_LENGTH) {
+               unsigned char c = *str++;
+
+               if (isascii(c) && isprint(c))
+                       *out++ = c;
+               i++;
+       }
+       *out = '\0';
+}
+
 static int acpi_battery_get_property(struct power_supply *psy,
                                     enum power_supply_property psp,
                                     union power_supply_propval *val)
@@ -524,6 +540,9 @@ static int extract_battery_info(const int use_bix,
            battery->capacity_now > battery->full_charge_capacity)
                battery->capacity_now = battery->full_charge_capacity;
 
+       if (!result)
+               acpi_battery_remove_special_chars(battery->model_number);
+
        return result;
 }
 
author
#2 lore raw

Hi Rafael,

Thank you for the review.

Quoted 6 lines · Rafael J. Wysocki (Intel)
Quoted 4 lines · Kate Hsuan

The battery Embedded Controller (EC) may return the model name with trailing unprintable or non-ASCII characters. For example, on some systems:

Quoted 7 lines
Quoted 2 lines
$ cat /sys/class/power_supply/BAT0/model_name
LNV-5B10W51864��

If they are just trailing and not in the middle of the string, you could just find the first one and overwrite it with a NUL character.

Rewriting the entire string is overkill in that case.

You are right. I'll find the first unprintable character and replace it with a NUL and break the loop.

Quoted 28 lines
Quoted 28 lines

Strip these unprintable characters to ensure the model_name attribute contains a valid string. If left intact, these malformed strings prevent udev rules and hwdb from matching the attribute correctly.

---
 drivers/acpi/battery.c | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
drivers/acpi/battery.c +1 -0
index f5e0eb299610..dea0c3cc5160 100644
@@ -10,6 +10,7 @@
 
 #define pr_fmt(fmt) "ACPI: battery: " fmt
 
+#include <linux/ctype.h>
 #include <linux/delay.h>
 #include <linux/dmi.h>
 #include <linux/jiffies.h>
@@ -194,6 +195,21 @@ static int acpi_battery_handle_discharging(struct acpi_battery *battery)
        return POWER_SUPPLY_STATUS_DISCHARGING;
 }
 
Quoted 7 lines

drivers/acpi/battery.c static int acpi_battery_handle_discharging(struct acpi_battery *battery) · line 202

Quoted 5 lines
drivers/acpi/battery.c +5 -0
+static void acpi_battery_remove_special_chars(char *str)
+{
+       char *out = str;
+       unsigned int i = 0;
+

And I would use a for () loop below anyway.

Okay

Quoted 25 lines
Quoted 24 lines
drivers/acpi/battery.c +13 -0
+       while (*str && i < MAX_STRING_LENGTH) {
+               unsigned char c = *str++;
+
+               if (isascii(c) && isprint(c))
+                       *out++ = c;
+               i++;
+       }
+       *out = '\0';
+}
+
 static int acpi_battery_get_property(struct power_supply *psy,
                                     enum power_supply_property psp,
                                     union power_supply_propval *val)
@@ -524,6 +540,9 @@ static int extract_battery_info(const int use_bix,
            battery->capacity_now > battery->full_charge_capacity)
                battery->capacity_now = battery->full_charge_capacity;
 
+       if (!result)
+               acpi_battery_remove_special_chars(battery->model_number);
+
        return result;
 }
 
Signature · 2 lines
BR,
Kate