diff options
author | Linus Groh <mail@linusgroh.de> | 2022-04-26 20:15:50 +0200 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2022-04-26 20:20:44 +0200 |
commit | cd3e337487ee6b05f1a29931da5fca8ecab9cb9e (patch) | |
tree | 80dd5f174bf8a0b5aac3b7674a3478c3394e053f /Kernel/Arch/x86 | |
parent | 2adc5efe2b80b48c9cdb7648ad377832d93b36ae (diff) | |
download | serenity-cd3e337487ee6b05f1a29931da5fca8ecab9cb9e.zip |
Kernel: Strip null terminators from all CPUID strings, not just brand
I've noticed that the KVM hypervisor vendor ID string contained null
terminators in the serialized JSON string in /proc/cpuinfo - let's avoid
that, and err on the side of caution and strip them from all strings
built from CPUID register values. They may not be fixed width after all.
Diffstat (limited to 'Kernel/Arch/x86')
-rw-r--r-- | Kernel/Arch/x86/common/ProcessorInfo.cpp | 15 |
1 files changed, 8 insertions, 7 deletions
diff --git a/Kernel/Arch/x86/common/ProcessorInfo.cpp b/Kernel/Arch/x86/common/ProcessorInfo.cpp index c388ac6be3..2706a681eb 100644 --- a/Kernel/Arch/x86/common/ProcessorInfo.cpp +++ b/Kernel/Arch/x86/common/ProcessorInfo.cpp @@ -37,6 +37,7 @@ ProcessorInfo::ProcessorInfo(Processor const& processor) m_display_model = model; } } + static void emit_u32(StringBuilder& builder, u32 value) { builder.appendff("{:c}{:c}{:c}{:c}", @@ -44,7 +45,7 @@ static void emit_u32(StringBuilder& builder, u32 value) (value >> 8) & 0xff, (value >> 16) & 0xff, (value >> 24) & 0xff); -}; +} NonnullOwnPtr<KString> ProcessorInfo::build_vendor_id_string() { @@ -53,7 +54,8 @@ NonnullOwnPtr<KString> ProcessorInfo::build_vendor_id_string() emit_u32(builder, cpuid.ebx()); emit_u32(builder, cpuid.edx()); emit_u32(builder, cpuid.ecx()); - return KString::must_create(builder.string_view()); + // NOTE: This isn't necessarily fixed length and might have null terminators at the end. + return KString::must_create(builder.string_view().trim("\0"sv, TrimMode::Right)); } NonnullOwnPtr<KString> ProcessorInfo::build_hypervisor_vendor_id_string(Processor const& processor) @@ -66,7 +68,8 @@ NonnullOwnPtr<KString> ProcessorInfo::build_hypervisor_vendor_id_string(Processo emit_u32(builder, cpuid.ebx()); emit_u32(builder, cpuid.ecx()); emit_u32(builder, cpuid.edx()); - return KString::must_create(builder.string_view()); + // NOTE: This isn't necessarily fixed length and might have null terminators at the end. + return KString::must_create(builder.string_view().trim("\0"sv, TrimMode::Right)); } NonnullOwnPtr<KString> ProcessorInfo::build_brand_string() @@ -87,10 +90,8 @@ NonnullOwnPtr<KString> ProcessorInfo::build_brand_string() append_brand_string_part_to_builder(0); append_brand_string_part_to_builder(1); append_brand_string_part_to_builder(2); - auto string_view = builder.string_view(); - // NOTE: Unlike the vendor ID strings, the brand string isn't necessarily fixed length and might have a null terminator in it. - // Try to find it and use a substring from 0 to that index, or the full length as a fallback. - return KString::must_create(string_view.substring_view(0, string_view.find('\0').value_or(string_view.length()))); + // NOTE: This isn't necessarily fixed length and might have null terminators at the end. + return KString::must_create(builder.string_view().trim("\0"sv, TrimMode::Right)); } NonnullOwnPtr<KString> ProcessorInfo::build_features_string(Processor const& processor) |