Skip to content

Commit

Permalink
Solve dmi_memory_device_extended_size WORD issue
Browse files Browse the repository at this point in the history
In case 17:
  dmi_memory_device_extended_size(sect_n, WORD(data + 0x1C)); may cause memory capcity read error. This because WORD and DWORD have different byte size, the DWORD is double long of WORD.
  After SMBIOS v2.8.0, the structures assume to be little-endian order. So redefine the WORD and DWORD in types.h.

Signed-off-by: Zhongze Hu <[email protected]>
  • Loading branch information
joshuazzhu committed Nov 16, 2022
1 parent 50e9593 commit a6c522c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/dmidecode.c
Original file line number Diff line number Diff line change
Expand Up @@ -5378,7 +5378,7 @@ xmlNode *dmi_decode(xmlNode *prnt_n, dmi_codes_major *dmiMajor, struct dmi_heade
dmi_memory_device_width(sect_n, "TotalWidth", WORD(data + 0x08));
dmi_memory_device_width(sect_n, "DataWidth", WORD(data + 0x0A));
if (h->length >= 0x20 && WORD(data + 0x0C) == 0x7FFF) {
dmi_memory_device_extended_size(sect_n, WORD(data + 0x1C));
dmi_memory_device_extended_size(sect_n, DWORD(data + 0x1C));
} else {
dmi_memory_device_size(sect_n, WORD(data + 0x0C));
}
Expand Down
22 changes: 10 additions & 12 deletions src/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,20 +69,18 @@ static inline u64 U64(u32 low, u32 high)
}
#endif

#ifdef ALIGNMENT_WORKAROUND
# ifdef BIGENDIAN
# define WORD(x) (u16)((x)[1]+((x)[0]<<8))
# define DWORD(x) (u32)((x)[3]+((x)[2]<<8)+((x)[1]<<16)+((x)[0]<<24))
# define QWORD(x) (U64(DWORD(x+4), DWORD(x)))
# else /* BIGENDIAN */
# define WORD(x) (u16)((x)[0]+((x)[1]<<8))
# define DWORD(x) (u32)((x)[0]+((x)[1]<<8)+((x)[2]<<16)+((x)[3]<<24))
# define QWORD(x) (U64(DWORD(x), DWORD(x+4)))
# endif /* BIGENDIAN */
#else /* ALIGNMENT_WORKAROUND */
/*
* Per SMBIOS v2.8.0 and later, all structures assume a little-endian
* ordering convention.
*/
#if defined(ALIGNMENT_WORKAROUND) || defined(BIGENDIAN)
#define WORD(x) (u16)((x)[0] + ((x)[1] << 8))
#define DWORD(x) (u32)((x)[0] + ((x)[1] << 8) + ((x)[2] << 16) + ((x)[3] << 24))
#define QWORD(x) (U64(DWORD(x), DWORD(x + 4)))
#else /* ALIGNMENT_WORKAROUND || BIGENDIAN */
#define WORD(x) (u16)(*(const u16 *)(x))
#define DWORD(x) (u32)(*(const u32 *)(x))
#define QWORD(x) (*(const u64 *)(x))
#endif /* ALIGNMENT_WORKAROUND */
#endif /* ALIGNMENT_WORKAROUND || BIGENDIAN */

#endif

0 comments on commit a6c522c

Please sign in to comment.