[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[dmidecode] [PATCH 1/3] dmioem: Fix vendor string comparison
From: |
Jean Delvare |
Subject: |
[dmidecode] [PATCH 1/3] dmioem: Fix vendor string comparison |
Date: |
Tue, 1 Dec 2020 17:30:35 +0100 |
The vendor string comparison code made an incorrect assumption about
the length of the vendor string found in the DMI table. We would hit
a false positive if that string was shorter than, and an exact prefix
of, the vendor string we were comparing with.
Explicitly check for length equality before comparing the strings.
Also rewrite the whole thing as an iteration over a table, instead
of open-coding it. This will make it easier to add support for more
vendors in the future.
Signed-off-by: Jean Delvare <jdelvare@suse.de>
---
dmioem.c | 31 ++++++++++++++++++++-----------
1 file changed, 20 insertions(+), 11 deletions(-)
--- dmidecode.orig/dmioem.c 2020-11-30 13:02:39.846286201 +0100
+++ dmidecode/dmioem.c 2020-12-01 15:40:02.101612575 +0100
@@ -52,7 +52,17 @@ static const char *dmi_product = NULL;
*/
void dmi_set_vendor(const char *s, const char *p)
{
- int len;
+ const struct { const char *str; enum DMI_VENDORS id; } vendor[] = {
+ { "Acer", VENDOR_ACER },
+ { "HP", VENDOR_HP },
+ { "Hewlett-Packard", VENDOR_HP },
+ { "HPE", VENDOR_HPE },
+ { "Hewlett Packard Enterprise", VENDOR_HPE },
+ { "IBM", VENDOR_IBM },
+ { "LENOVO", VENDOR_LENOVO },
+ };
+ unsigned int i;
+ size_t len;
/*
* Often DMI strings have trailing spaces. Ignore these
@@ -62,16 +72,15 @@ void dmi_set_vendor(const char *s, const
while (len && s[len - 1] == ' ')
len--;
- if (strncmp(s, "Acer", len) == 0)
- dmi_vendor = VENDOR_ACER;
- else if (strncmp(s, "HP", len) == 0 || strncmp(s, "Hewlett-Packard",
len) == 0)
- dmi_vendor = VENDOR_HP;
- else if (strncmp(s, "HPE", len) == 0 || strncmp(s, "Hewlett Packard
Enterprise", len) == 0)
- dmi_vendor = VENDOR_HPE;
- else if (strncmp(s, "IBM", len) == 0)
- dmi_vendor = VENDOR_IBM;
- else if (strncmp(s, "LENOVO", len) == 0)
- dmi_vendor = VENDOR_LENOVO;
+ for (i = 0; i < ARRAY_SIZE(table); i++)
+ {
+ if (strlen(vendor[i].str) == len &&
+ strncmp(s, vendor[i].str, len) == 0)
+ {
+ dmi_vendor = vendor[i].id;
+ break;
+ }
+ }
dmi_product = p;
}
--
Jean Delvare
SUSE L3 Support
- [dmidecode] [PATCH 1/3] dmioem: Fix vendor string comparison,
Jean Delvare <=