From fb7eb04f428a09c3b6f3d7ad37d6646c6030c4a1 Mon Sep 17 00:00:00 2001 From: Gabe Black Date: Fri, 7 May 2021 15:38:19 -0700 Subject: [PATCH] arch-x86: Fix x86 build. Variation of fix by Maximilian Stein: https://gem5-review.googlesource.com/c/public/gem5/+/45185 Placate gcc, which thinks we're doing something wrong when using strncpy. Also ensure that fixed size buffers for strings are filled completely, rather than just filling in the part which holds the string we want. Change-Id: I59f51f6a56a3a3e2632eb89ecb6db709fbbd165d Reviewed-on: https://gem5-review.googlesource.com/c/public/gem5/+/45205 Maintainer: Gabe Black Maintainer: Jason Lowe-Power Reviewed-by: Jason Lowe-Power Tested-by: kokoro --- src/arch/x86/bios/acpi.cc | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/src/arch/x86/bios/acpi.cc b/src/arch/x86/bios/acpi.cc index 53b6e4dd8e..9a5d32ed17 100644 --- a/src/arch/x86/bios/acpi.cc +++ b/src/arch/x86/bios/acpi.cc @@ -37,6 +37,7 @@ #include "arch/x86/bios/acpi.hh" +#include #include #include @@ -52,6 +53,26 @@ namespace X86ISA namespace ACPI { +static void +fillCopy(void *dest, size_t dest_size, const void *src, size_t src_size) +{ + src_size = std::min(src_size, dest_size); + const size_t zero_size = dest_size - src_size; + + uint8_t *pos = (uint8_t *)dest; + + std::memcpy(pos, src, src_size); + pos += src_size; + + std::memset(pos, 0, zero_size); +} + +static void +fillCopy(void *dest, size_t dest_size, const std::string &src) +{ + fillCopy(dest, dest_size, src.c_str(), src.length()); +} + const char RSDP::signature[] = "RSD PTR "; static uint8_t @@ -93,7 +114,7 @@ RSDP::write(PortProxy& phys_proxy, Allocator& alloc) const static_assert(sizeof(signature) - 1 == sizeof(data->signature), "signature length mismatch"); std::memcpy(data->signature, signature, sizeof(data->signature)); - std::strncpy(data->oemID, params().oem_id.c_str(), sizeof(data->oemID)); + fillCopy(data->oemID, sizeof(data->oemID), params().oem_id); data->revision = params().revision; data->length = mem.size(); @@ -133,12 +154,11 @@ SysDescTable::writeBuf(PortProxy& phys_proxy, Allocator& alloc, // Fill in the header. auto& p = params(); Mem* header = (Mem*)mem.data(); - std::strncpy(header->signature, signature, sizeof(header->signature)); + fillCopy(header->signature, sizeof(header->signature), signature); header->length = mem.size(); header->revision = revision; - std::strncpy(header->oemID, p.oem_id.c_str(), sizeof(header->oemID)); - std::strncpy(header->oemTableID, p.oem_table_id.c_str(), - sizeof(header->oemTableID)); + fillCopy(header->oemID, sizeof(header->oemID), p.oem_id); + fillCopy(header->oemTableID, sizeof(header->oemTableID), p.oem_table_id); header->oemRevision = p.oem_revision; header->creatorID = p.creator_id; header->creatorRevision = p.creator_revision;