Patrick Georgi submitted this change.

View Change

Approvals: build bot (Jenkins): Verified Michał Żygowski: Looks good to me, approved Tim Wawrzynczak: Looks good to me, approved
SMBIOS: Avoid `sizeof` on struct type

Where applicable, use the size of the associated variable.

Change-Id: Ibbac2a82893232a6f87182a6a965b84a599d633e
Signed-off-by: Angel Pons <th3fanbus@gmail.com>
Reviewed-on: https://review.coreboot.org/c/coreboot/+/55904
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
Reviewed-by: Tim Wawrzynczak <twawrzynczak@chromium.org>
Reviewed-by: Michał Żygowski <michal.zygowski@3mdeb.com>
---
M src/arch/x86/smbios.c
M src/drivers/elog/elog.c
M src/drivers/wifi/generic/smbios.c
M src/mainboard/emulation/qemu-i440fx/northbridge.c
M src/mainboard/pcengines/apu1/mainboard.c
M src/mainboard/pcengines/apu2/mainboard.c
M src/mainboard/samsung/lumpy/mainboard.c
7 files changed, 52 insertions(+), 53 deletions(-)

diff --git a/src/arch/x86/smbios.c b/src/arch/x86/smbios.c
index b21cf67..4637dc0 100644
--- a/src/arch/x86/smbios.c
+++ b/src/arch/x86/smbios.c
@@ -243,7 +243,7 @@
{
struct smbios_type17 *t = (struct smbios_type17 *)*current;

- memset(t, 0, sizeof(struct smbios_type17));
+ memset(t, 0, sizeof(*t));
t->memory_type = dimm->ddr_type;
if (dimm->configured_speed_mts != 0)
t->clock_speed = dimm->configured_speed_mts;
@@ -318,7 +318,7 @@
t->phys_memory_array_handle = type16_handle;

*handle += 1;
- t->length = sizeof(struct smbios_type17) - 2;
+ t->length = sizeof(*t) - 2;
return t->length + smbios_string_table_len(t->eos);
}

@@ -380,9 +380,9 @@
static int smbios_write_type0(unsigned long *current, int handle)
{
struct smbios_type0 *t = (struct smbios_type0 *)*current;
- int len = sizeof(struct smbios_type0);
+ int len = sizeof(*t);

- memset(t, 0, sizeof(struct smbios_type0));
+ memset(t, 0, sizeof(*t));
t->type = SMBIOS_BIOS_INFORMATION;
t->handle = handle;
t->length = len - 2;
@@ -512,9 +512,9 @@
static int smbios_write_type1(unsigned long *current, int handle)
{
struct smbios_type1 *t = (struct smbios_type1 *)*current;
- int len = sizeof(struct smbios_type1);
+ int len = sizeof(*t);

- memset(t, 0, sizeof(struct smbios_type1));
+ memset(t, 0, sizeof(*t));
t->type = SMBIOS_SYSTEM_INFORMATION;
t->handle = handle;
t->length = len - 2;
@@ -535,9 +535,9 @@
static int smbios_write_type2(unsigned long *current, int handle, const int chassis_handle)
{
struct smbios_type2 *t = (struct smbios_type2 *)*current;
- int len = sizeof(struct smbios_type2);
+ int len = sizeof(*t);

- memset(t, 0, sizeof(struct smbios_type2));
+ memset(t, 0, sizeof(*t));
t->type = SMBIOS_BOARD_INFORMATION;
t->handle = handle;
t->length = len - 2;
@@ -559,9 +559,9 @@
static int smbios_write_type3(unsigned long *current, int handle)
{
struct smbios_type3 *t = (struct smbios_type3 *)*current;
- int len = sizeof(struct smbios_type3);
+ int len = sizeof(*t);

- memset(t, 0, sizeof(struct smbios_type3));
+ memset(t, 0, sizeof(*t));
t->type = SMBIOS_SYSTEM_ENCLOSURE;
t->handle = handle;
t->length = len - 2;
@@ -585,7 +585,7 @@
unsigned int cpu_voltage;
struct cpuid_result res;
struct smbios_type4 *t = (struct smbios_type4 *)*current;
- int len = sizeof(struct smbios_type4);
+ int len = sizeof(*t);
uint16_t characteristics = 0;
static unsigned int cnt = 0;
char buf[8];
@@ -597,7 +597,7 @@
if (cpu_have_cpuid())
res = cpuid(1);

- memset(t, 0, sizeof(struct smbios_type4));
+ memset(t, 0, sizeof(*t));
t->type = SMBIOS_PROCESSOR_INFORMATION;
t->handle = handle;
t->length = len - 2;
@@ -698,10 +698,10 @@
const size_t cache_size)
{
struct smbios_type7 *t = (struct smbios_type7 *)*current;
- int len = sizeof(struct smbios_type7);
+ int len = sizeof(*t);
char buf[8];

- memset(t, 0, sizeof(struct smbios_type7));
+ memset(t, 0, sizeof(*t));
t->type = SMBIOS_CACHE_INFORMATION;
t->handle = handle;
t->length = len - 2;
@@ -906,10 +906,10 @@

for (i = 0; i < num_ports; i++, port++) {
struct smbios_type8 *t = (struct smbios_type8 *)*current;
- memset(t, 0, sizeof(struct smbios_type8));
+ memset(t, 0, sizeof(*t));
t->type = SMBIOS_PORT_CONNECTOR_INFORMATION;
t->handle = *handle;
- t->length = sizeof(struct smbios_type8) - 2;
+ t->length = sizeof(*t) - 2;
t->internal_reference_designator =
smbios_add_string(t->eos, port->internal_reference_designator);
t->internal_connector_type = port->internal_connector_type;
@@ -932,9 +932,9 @@
const u16 id, u8 slot_char1, u8 slot_char2, u8 bus, u8 dev_func)
{
struct smbios_type9 *t = (struct smbios_type9 *)*current;
- int len = sizeof(struct smbios_type9);
+ int len = sizeof(*t);

- memset(t, 0, sizeof(struct smbios_type9));
+ memset(t, 0, sizeof(*t));
t->type = SMBIOS_SYSTEM_SLOTS;
t->handle = *handle;
t->length = len - 2;
@@ -1075,10 +1075,10 @@
if (meminfo == NULL)
return 0; /* can't find mem info in cbmem */

- memset(t, 0, sizeof(struct smbios_type19));
+ memset(t, 0, sizeof(*t));

t->type = SMBIOS_MEMORY_ARRAY_MAPPED_ADDRESS;
- t->length = sizeof(struct smbios_type19) - 2;
+ t->length = sizeof(*t) - 2;
t->handle = *handle;
t->memory_array_handle = type16;

@@ -1118,9 +1118,9 @@
static int smbios_write_type32(unsigned long *current, int handle)
{
struct smbios_type32 *t = (struct smbios_type32 *)*current;
- int len = sizeof(struct smbios_type32);
+ int len = sizeof(*t);

- memset(t, 0, sizeof(struct smbios_type32));
+ memset(t, 0, sizeof(*t));
t->type = SMBIOS_SYSTEM_BOOT_INFORMATION;
t->handle = handle;
t->length = len - 2;
@@ -1135,9 +1135,9 @@
const u8 irq)
{
struct smbios_type38 *t = (struct smbios_type38 *)*current;
- int len = sizeof(struct smbios_type38);
+ int len = sizeof(*t);

- memset(t, 0, sizeof(struct smbios_type38));
+ memset(t, 0, sizeof(*t));
t->type = SMBIOS_IPMI_DEVICE_INFORMATION;
t->handle = *handle;
t->length = len - 2;
@@ -1160,9 +1160,9 @@
u8 bus, u8 device, u8 function, u8 device_type)
{
struct smbios_type41 *t = (struct smbios_type41 *)*current;
- int len = sizeof(struct smbios_type41);
+ int len = sizeof(*t);

- memset(t, 0, sizeof(struct smbios_type41));
+ memset(t, 0, sizeof(*t));
t->type = SMBIOS_ONBOARD_DEVICES_EXTENDED_INFORMATION;
t->handle = *handle;
t->length = len - 2;
@@ -1184,9 +1184,9 @@
static int smbios_write_type127(unsigned long *current, int handle)
{
struct smbios_type127 *t = (struct smbios_type127 *)*current;
- int len = sizeof(struct smbios_type127);
+ int len = sizeof(*t);

- memset(t, 0, sizeof(struct smbios_type127));
+ memset(t, 0, sizeof(*t));
t->type = SMBIOS_END_OF_TABLE;
t->handle = handle;
t->length = len - 2;
@@ -1306,11 +1306,11 @@
printk(BIOS_DEBUG, "%s: %08lx\n", __func__, current);

se = (struct smbios_entry *)current;
- current += sizeof(struct smbios_entry);
+ current += sizeof(*se);
current = ALIGN_UP(current, 16);

se3 = (struct smbios_entry30 *)current;
- current += sizeof(struct smbios_entry30);
+ current += sizeof(*se3);
current = ALIGN_UP(current, 16);

tables = current;
@@ -1342,9 +1342,9 @@
update_max(len, max_struct_size, smbios_write_type127(&current, handle++));

/* Install SMBIOS 2.1 entry point */
- memset(se, 0, sizeof(struct smbios_entry));
+ memset(se, 0, sizeof(*se));
memcpy(se->anchor, "_SM_", 4);
- se->length = sizeof(struct smbios_entry);
+ se->length = sizeof(*se);
se->major_version = 3;
se->minor_version = 0;
se->max_struct_size = max_struct_size;
@@ -1354,21 +1354,20 @@
se->struct_table_address = (u32)tables;
se->struct_table_length = len;

- se->intermediate_checksum = smbios_checksum((u8 *)se + 0x10,
- sizeof(struct smbios_entry) - 0x10);
- se->checksum = smbios_checksum((u8 *)se, sizeof(struct smbios_entry));
+ se->intermediate_checksum = smbios_checksum((u8 *)se + 0x10, sizeof(*se) - 0x10);
+ se->checksum = smbios_checksum((u8 *)se, sizeof(*se));

/* Install SMBIOS 3.0 entry point */
- memset(se3, 0, sizeof(struct smbios_entry30));
+ memset(se3, 0, sizeof(*se3));
memcpy(se3->anchor, "_SM3_", 5);
- se3->length = sizeof(struct smbios_entry30);
+ se3->length = sizeof(*se3);
se3->major_version = 3;
se3->minor_version = 0;

se3->struct_table_address = (u64)tables;
se3->struct_table_length = len;

- se3->checksum = smbios_checksum((u8 *)se3, sizeof(struct smbios_entry30));
+ se3->checksum = smbios_checksum((u8 *)se3, sizeof(*se3));

return current;
}
diff --git a/src/drivers/elog/elog.c b/src/drivers/elog/elog.c
index 4769559..6e31686 100644
--- a/src/drivers/elog/elog.c
+++ b/src/drivers/elog/elog.c
@@ -594,7 +594,7 @@
int elog_smbios_write_type15(unsigned long *current, int handle)
{
struct smbios_type15 *t = (struct smbios_type15 *)*current;
- int len = sizeof(struct smbios_type15);
+ int len = sizeof(*t);
uintptr_t log_address;

size_t elog_size = region_device_sz(&elog_state.nv_dev);
diff --git a/src/drivers/wifi/generic/smbios.c b/src/drivers/wifi/generic/smbios.c
index a1a8e4f..b819116 100644
--- a/src/drivers/wifi/generic/smbios.c
+++ b/src/drivers/wifi/generic/smbios.c
@@ -18,9 +18,9 @@
} __packed;

struct smbios_type_intel_wifi *t = (struct smbios_type_intel_wifi *)*current;
- int len = sizeof(struct smbios_type_intel_wifi);
+ int len = sizeof(*t);

- memset(t, 0, sizeof(struct smbios_type_intel_wifi));
+ memset(t, 0, sizeof(*t));
t->type = 0x85;
t->length = len - 2;
t->handle = *handle;
diff --git a/src/mainboard/emulation/qemu-i440fx/northbridge.c b/src/mainboard/emulation/qemu-i440fx/northbridge.c
index 8579b8f..01d036a 100644
--- a/src/mainboard/emulation/qemu-i440fx/northbridge.c
+++ b/src/mainboard/emulation/qemu-i440fx/northbridge.c
@@ -166,9 +166,9 @@
static int qemu_get_smbios_data16(int handle, unsigned long *current)
{
struct smbios_type16 *t = (struct smbios_type16 *)*current;
- int len = sizeof(struct smbios_type16);
+ int len = sizeof(*t);

- memset(t, 0, sizeof(struct smbios_type16));
+ memset(t, 0, sizeof(*t));
t->type = SMBIOS_PHYS_MEMORY_ARRAY;
t->handle = handle;
t->length = len - 2;
@@ -185,11 +185,11 @@
struct smbios_type17 *t = (struct smbios_type17 *)*current;
int len;

- memset(t, 0, sizeof(struct smbios_type17));
+ memset(t, 0, sizeof(*t));
t->type = SMBIOS_MEMORY_DEVICE;
t->handle = handle;
t->phys_memory_array_handle = parent_handle;
- t->length = sizeof(struct smbios_type17) - 2;
+ t->length = sizeof(*t) - 2;
t->size = qemu_get_memory_size() / 1024;
t->data_width = 64;
t->total_width = 64;
diff --git a/src/mainboard/pcengines/apu1/mainboard.c b/src/mainboard/pcengines/apu1/mainboard.c
index cb5d4fb..05c496d 100644
--- a/src/mainboard/pcengines/apu1/mainboard.c
+++ b/src/mainboard/pcengines/apu1/mainboard.c
@@ -173,7 +173,7 @@
int len;

t = (struct smbios_type16 *)*current;
- len = sizeof(struct smbios_type16);
+ len = sizeof(*t);
memset(t, 0, len);
max_capacity = get_spd_offset() ? 4 : 2; /* 4GB or 2GB variant */

@@ -199,10 +199,10 @@
int len;

t = (struct smbios_type17 *)*current;
- memset(t, 0, sizeof(struct smbios_type17));
+ memset(t, 0, sizeof(*t));

t->type = SMBIOS_MEMORY_DEVICE;
- t->length = sizeof(struct smbios_type17) - 2;
+ t->length = sizeof(*t) - 2;
t->handle = *handle + 1;
t->phys_memory_array_handle = *handle;
t->memory_error_information_handle = 0xfffe;
diff --git a/src/mainboard/pcengines/apu2/mainboard.c b/src/mainboard/pcengines/apu2/mainboard.c
index 56976e5..bcf2536 100644
--- a/src/mainboard/pcengines/apu2/mainboard.c
+++ b/src/mainboard/pcengines/apu2/mainboard.c
@@ -155,7 +155,7 @@
int len = 0;

t = (struct smbios_type16 *)*current;
- len = sizeof(struct smbios_type16);
+ len = sizeof(*t);
memset(t, 0, len);
max_capacity = get_spd_offset() ? 4 : 2; /* 4GB or 2GB variant */

@@ -182,10 +182,10 @@
int len;

t = (struct smbios_type17 *)*current;
- memset(t, 0, sizeof(struct smbios_type17));
+ memset(t, 0, sizeof(*t));

t->type = SMBIOS_MEMORY_DEVICE;
- t->length = sizeof(struct smbios_type17) - 2;
+ t->length = sizeof(*t) - 2;
t->handle = *handle + 1;
t->phys_memory_array_handle = *handle;
t->memory_error_information_handle = 0xfffe;
diff --git a/src/mainboard/samsung/lumpy/mainboard.c b/src/mainboard/samsung/lumpy/mainboard.c
index cdeaf72..c2899b8 100644
--- a/src/mainboard/samsung/lumpy/mainboard.c
+++ b/src/mainboard/samsung/lumpy/mainboard.c
@@ -29,9 +29,9 @@
const char *name, u8 irq, u8 addr)
{
struct smbios_type41 *t = (struct smbios_type41 *)*current;
- int len = sizeof(struct smbios_type41);
+ int len = sizeof(*t);

- memset(t, 0, sizeof(struct smbios_type41));
+ memset(t, 0, sizeof(*t));
t->type = SMBIOS_ONBOARD_DEVICES_EXTENDED_INFORMATION;
t->handle = *handle;
t->length = len - 2;

To view, visit change 55904. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: coreboot
Gerrit-Branch: master
Gerrit-Change-Id: Ibbac2a82893232a6f87182a6a965b84a599d633e
Gerrit-Change-Number: 55904
Gerrit-PatchSet: 2
Gerrit-Owner: Angel Pons <th3fanbus@gmail.com>
Gerrit-Reviewer: Arthur Heymans <arthur@aheymans.xyz>
Gerrit-Reviewer: Michał Żygowski <michal.zygowski@3mdeb.com>
Gerrit-Reviewer: Nico Huber <nico.h@gmx.de>
Gerrit-Reviewer: Patrick Georgi <pgeorgi@google.com>
Gerrit-Reviewer: Patrick Rudolph <siro@das-labor.org>
Gerrit-Reviewer: Piotr Król <piotr.krol@3mdeb.com>
Gerrit-Reviewer: Tim Wawrzynczak <twawrzynczak@chromium.org>
Gerrit-Reviewer: Werner Zeh <werner.zeh@siemens.com>
Gerrit-Reviewer: build bot (Jenkins) <no-reply@coreboot.org>
Gerrit-CC: Paul Menzel <paulepanter@mailbox.org>
Gerrit-MessageType: merged