Skip to content

Commit

Permalink
统一检查数组越界访问问题
Browse files Browse the repository at this point in the history
  • Loading branch information
yumaojun03 committed Jul 7, 2020
1 parent 1d36bff commit 9e30378
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 63 deletions.
2 changes: 1 addition & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func checkDecodeErr(t string, err error) {
}

func usage() {
fmt.Fprintf(os.Stderr, `dmidecode version: 0.1.2
fmt.Fprintf(os.Stderr, `dmidecode version: 0.1.3
Usage: dmidecode [-h] [-d] [-t type]
Options:
Expand Down
11 changes: 5 additions & 6 deletions parser/bios/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,24 @@ import (

// Parse 参考 https://www.dmtf.org/sites/default/files/standards/documents/DSP0134_3.1.1.pdf
func Parse(s *smbios.Structure) (*Information, error) {
data := s.Formatted
sas := smbios.U16(data[0x02:0x04])
sas := s.U16(0x02, 0x04)

bi := &Information{
Header: s.Header,
Vendor: s.GetString(0x0),
BIOSVersion: s.GetString(0x1),
ReleaseDate: s.GetString(0x4),
StartingAddressSegment: sas,
RomSize: RomSize(64 * (data[0x05] + 1)),
RomSize: RomSize(64 * (s.GetByte(0x05) + 1)),
RuntimeSize: RuntimeSize((uint(0x10000) - uint(sas)) << 4),
Characteristics: Characteristics(smbios.U64(data[0x06:0x08])),
Characteristics: Characteristics(s.U64(0x06, 0x08)),
}

if s.Header.Length >= 0x08 {
bi.CharacteristicsExt1 = Ext1(data[0x08])
bi.CharacteristicsExt1 = Ext1(s.GetByte(0x08))
}
if s.Header.Length >= 0x09 {
bi.CharacteristicsExt2 = Ext2(data[0x09])
bi.CharacteristicsExt2 = Ext2(s.GetByte(0x09))
}

return bi, nil
Expand Down
24 changes: 11 additions & 13 deletions parser/chassis/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,22 @@ import (

// Parse 解析底座信息
func Parse(s *smbios.Structure) (*Information, error) {
data := s.Formatted

info := &Information{
Manufacturer: s.GetString(0x0),
Type: Type(data[0x01] & 127),
Lock: Lock(data[0x01] >> 7),
Type: Type(s.GetByte(0x01) & 127),
Lock: Lock(s.GetByte(0x01) >> 7),
Version: s.GetString(0x2),
SerialNumber: s.GetString(0x3),
AssetTag: s.GetString(0x4),
BootUpState: State(data[0x05]),
PowerSupplyState: State(data[0x06]),
ThermalState: State(data[0x07]),
SecurityStatus: SecurityStatus(data[0x08]),
OEMdefined: smbios.U16(data[0x09 : 0x09+4]),
Height: Height(data[0xD]),
NumberOfPowerCords: data[0xE],
ContainedElementCount: data[0xF],
ContainedElementRecordLength: data[0x10],
BootUpState: State(s.GetByte(0x05)),
PowerSupplyState: State(s.GetByte(0x06)),
ThermalState: State(s.GetByte(0x07)),
SecurityStatus: SecurityStatus(s.GetByte(0x08)),
OEMdefined: s.U16(0x09, 0xd),
Height: Height(s.GetByte(0xd)),
NumberOfPowerCords: s.GetByte(0xe),
ContainedElementCount: s.GetByte(0xf),
ContainedElementRecordLength: s.GetByte(0x10),
// TODO: 7.4.4
//ci.ContainedElements:
SKUNumber: s.GetString(4),
Expand Down
18 changes: 6 additions & 12 deletions parser/onboard/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,14 @@ func ParseType41(s *smbios.Structure) (*ExtendedInformation, error) {
return nil, fmt.Errorf("ParseType41 only parse type 41 data, but now: %d", s.Type())
}

data := s.Formatted

info := &ExtendedInformation{
ReferenceDesignation: s.GetString(0x0),
DeviceType: ExtendedInformationType(data[0x01] & 127),
DeviceStatus: DeviceStatus(data[0x01] >> 7),
DeviceTypeInstance: data[0x02],
SegmentGroupNumber: smbios.U16(data[0x03:0x05]),
BusNumber: data[0x05],
DeviceFunctionNumber: data[0x06],
}

if len(s.Formatted) >= 0x06 {
info.DeviceFunctionNumber = data[0x06]
DeviceType: ExtendedInformationType(s.GetByte(0x01) & 127),
DeviceStatus: DeviceStatus(s.GetByte(0x01) >> 7),
DeviceTypeInstance: s.GetByte(0x02),
SegmentGroupNumber: s.U16(0x03, 0x05),
BusNumber: s.GetByte(0x05),
DeviceFunctionNumber: s.GetByte(0x06),
}

return info, nil
Expand Down
7 changes: 3 additions & 4 deletions parser/port/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@ import (

// Parse 解析port信息
func Parse(s *smbios.Structure) (*Information, error) {
data := s.Formatted
info := &Information{
Header: s.Header,
InternalReferenceDesignator: s.GetString(0x0),
InternalConnectorType: ConnectorType(data[0x01]),
InternalConnectorType: ConnectorType(s.GetByte(0x01)),
ExternalReferenceDesignator: s.GetString(0x2),
ExternalConnectorType: ConnectorType(data[0x03]),
Type: PortType(data[0x04]),
ExternalConnectorType: ConnectorType(s.GetByte(0x03)),
Type: PortType(s.GetByte(0x04)),
}
return info, nil
}
20 changes: 9 additions & 11 deletions parser/processor/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,17 @@ func ParseProcessor(s *smbios.Structure) (*Processor, error) {

// ParseCache 缓存信息
func ParseCache(s *smbios.Structure) (*Cache, error) {
data := s.Formatted

cache := &Cache{
SocketDesignation: s.GetString(0x0),
Configuration: NewCacheConfiguration(smbios.U16(data[0x01:0x03])),
MaximumCacheSize: NewCacheSize(smbios.U16(data[0x03:0x05])),
InstalledSize: NewCacheSize(smbios.U16(data[0x05:0x07])),
SupportedSRAMType: CacheSRAMType(smbios.U16(data[0x07:0x09])),
CurrentSRAMType: CacheSRAMType(smbios.U16(data[0x09:0x0B])),
CacheSpeed: CacheSpeed(data[0x0B]),
ErrorCorrectionType: CacheErrorCorrectionType(data[0xC]),
SystemCacheType: CacheSystemCacheType(data[0xD]),
Associativity: CacheAssociativity(data[0xE]),
Configuration: NewCacheConfiguration(s.U16(0x01, 0x03)),
MaximumCacheSize: NewCacheSize(s.U16(0x03, 0x05)),
InstalledSize: NewCacheSize(s.U16(0x05, 0x07)),
SupportedSRAMType: CacheSRAMType(s.U16(0x07, 0x09)),
CurrentSRAMType: CacheSRAMType(s.U16(0x09, 0x0b)),
CacheSpeed: CacheSpeed(s.GetByte(0x0b)),
ErrorCorrectionType: CacheErrorCorrectionType(s.GetByte(0xc)),
SystemCacheType: CacheSystemCacheType(s.GetByte(0xd)),
Associativity: CacheAssociativity(s.GetByte(0xe)),
}

return cache, nil
Expand Down
25 changes: 13 additions & 12 deletions parser/slot/parse.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
package slot

import "github.com/yumaojun03/dmidecode/smbios"
import (
"github.com/yumaojun03/dmidecode/smbios"
)

// Parse 解析slot信息
func Parse(s *smbios.Structure) (*SystemSlot, error) {
data := s.Formatted
info := &SystemSlot{
Designation: s.GetString(0x0),
Type: Type(data[0x01]),
DataBusWidth: DataBusWidth(data[0x02]),
CurrentUsage: Usage(data[0x03]),
Length: Length(data[0x04]),
ID: ID(smbios.U16(data[0x05:0x07])),
Characteristics1: Characteristics1(data[0x07]),
Characteristics2: Characteristics2(data[0x08]),
SegmentGroupNumber: SegmengGroupNumber(smbios.U16(data[0x09:0x0B])),
BusNumber: Number(data[0x0B]),
DeviceFunctionNumber: Number(data[0x0C]),
Type: Type(s.GetByte(0x01)),
DataBusWidth: DataBusWidth(s.GetByte(0x02)),
CurrentUsage: Usage(s.GetByte(0x03)),
Length: Length(s.GetByte(0x04)),
ID: ID(s.U16(0x05, 0x07)),
Characteristics1: Characteristics1(s.GetByte(0x07)),
Characteristics2: Characteristics2(s.GetByte(0x08)),
SegmentGroupNumber: SegmengGroupNumber(s.U16(0x09, 0x0b)),
BusNumber: Number(s.GetByte(0x0b)),
DeviceFunctionNumber: Number(s.GetByte(0x0c)),
}

return info, nil
Expand Down
6 changes: 2 additions & 4 deletions parser/system/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,14 @@ import (

// Parse 解析smbios struct数据
func Parse(s *smbios.Structure) (*Information, error) {
data := s.Formatted

return &Information{
Header: s.Header,
Manufacturer: s.GetString(0x0),
ProductName: s.GetString(0x1),
Version: s.GetString(0x2),
SerialNumber: s.GetString(0x3),
UUID: uuid(data[0x04:0x14], s.GetString(2)),
WakeUpType: WakeUpType(data[0x14]),
UUID: uuid(s.GetBytes(0x04, 0x14), s.GetString(2)),
WakeUpType: WakeUpType(s.GetByte(0x14)),
SKUNumber: s.GetString(0x15),
Family: s.GetString(0x16),
}, nil
Expand Down

0 comments on commit 9e30378

Please sign in to comment.