From 37e02b455b2e0220a5922e1531d80b406b70b358 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Fri, 20 Dec 2024 12:09:41 +0100 Subject: [PATCH] analyze: not all smbios fields are always defined, deal with that As per previous commit, accept that not all SMBIOS fields are alwaysa available (or set, but empty), hence handle this gracefully and don't generate relevant CHIDs, as per docs. --- src/analyze/analyze-chid.c | 33 ++++++++++++++++++++++++++------- 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/src/analyze/analyze-chid.c b/src/analyze/analyze-chid.c index 5bb50f54d86..9e555e39aa0 100644 --- a/src/analyze/analyze-chid.c +++ b/src/analyze/analyze-chid.c @@ -120,11 +120,26 @@ int verb_chid(int argc, char *argv[], void *userdata) { _cleanup_free_ char *buf = NULL; size_t size; + /* According to the CHID spec we should not generate CHIDs for SMBIOS fields that aren't set + * or are set to an empty string. Hence leave them NULL here. */ + + if (!smbios_files[f]) + continue; + r = read_virtual_file_at(smbios_fd, smbios_files[f], SIZE_MAX, &buf, &size); + if (r == -ENOENT) { + log_debug_errno(r, "SMBIOS field '%s' not set, skipping.", smbios_files[f]); + continue; + } if (r < 0) return log_error_errno(r, "Failed to read SMBIOS field '%s': %m", smbios_files[f]); - if (size < 1 || buf[size-1] != '\n') + if (size == 0 || (size == 1 && buf[0] == '\n')) { + log_debug("SMBIOS field '%s' is empty, skipping.", smbios_files[f]); + continue; + } + + if (buf[size-1] != '\n') return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Expected SMBIOS field '%s' to end in newline, but it doesn't, refusing.", smbios_files[f]); size--; @@ -172,9 +187,13 @@ int verb_chid(int argc, char *argv[], void *userdata) { return log_oom(); for (ChidSmbiosFields f = 0; f < _CHID_SMBIOS_FIELDS_MAX; f++) { - _cleanup_free_ char *c = utf16_to_utf8(smbios_fields[f], SIZE_MAX); - if (!c) - return log_oom(); + _cleanup_free_ char *c = NULL; + + if (smbios_fields[f]) { + c = utf16_to_utf8(smbios_fields[f], SIZE_MAX); + if (!c) + return log_oom(); + } if (!strextend(&legend, ansi_grey(), @@ -191,8 +210,8 @@ int verb_chid(int argc, char *argv[], void *userdata) { smbios_files[f], ansi_grey(), " (", - ansi_highlight(), - c, + c ? ansi_highlight() : ansi_grey(), + strna(c), ansi_grey(), ")", ansi_normal())) @@ -202,7 +221,7 @@ int verb_chid(int argc, char *argv[], void *userdata) { 4 + utf8_console_width(smbios_files[f]) + 2 + - utf8_console_width(c) + + utf8_console_width(strna(c)) + 1; if (w > 79) { -- 2.47.3