From 53f0aee26b574bfb127a09950a34159fa68f49fe Mon Sep 17 00:00:00 2001 From: "H. Peter Anvin" Date: Tue, 26 Apr 2011 15:24:17 -0700 Subject: [PATCH] dmi: strip whitespace from DMI strings Some BIOS vendors seem to have large spaces in DMI strings, presumably for easy patching. Therefore, clean up the strings before we use them. Signed-off-by: H. Peter Anvin --- core/dmi.c | 48 +++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 45 insertions(+), 3 deletions(-) diff --git a/core/dmi.c b/core/dmi.c index 674d27c3..9c9e0ab2 100644 --- a/core/dmi.c +++ b/core/dmi.c @@ -232,6 +232,50 @@ static const struct sysappend_dmi_strings dmi_strings[] = { { NULL, 0, 0, 0 } }; +/* + * Install the string in the string table, if nonempty, after + * removing leading and trailing whitespace. + */ +static bool is_ctl_or_whitespace(char c) +{ + return (c <= ' ' || c == '\x7f'); +} + +static const char *dmi_install_string(const char *pfx, const char *str) +{ + const char *p, *ep; + size_t pfxlen; + char *nstr, *q; + + if (!str) + return NULL; + + while (*str && is_ctl_or_whitespace(*str)) + str++; + + if (!*str) + return NULL; + + ep = p = str; + while (*p) { + if (!is_ctl_or_whitespace(*p)) + ep = str+1; + p++; + } + + pfxlen = strlen(pfx); + q = nstr = malloc(pfxlen + (ep-p) + 1); + if (!nstr) + return NULL; + memcpy(q, pfx, pfxlen); + q += pfxlen; + memcpy(q, str, ep-p); + q += (ep-p); + *q = '\0'; + + return nstr; +} + void dmi_init(void) { const struct sysappend_dmi_strings *ds; @@ -249,8 +293,6 @@ void dmi_init(void) free((char *)sysappend_strings[ds->sa]); sysappend_strings[ds->sa] = NULL; } - if (str) - asprintf((char **)&sysappend_strings[ds->sa], - "%s%s", ds->prefix, str); + sysappend_strings[ds->sa] = dmi_install_string(ds->prefix, str); } } -- 2.11.4.GIT