Skip to content

Commit

Permalink
fix: check all type cast
Browse files Browse the repository at this point in the history
Signed-off-by: Zxilly <[email protected]>
  • Loading branch information
Zxilly committed Jun 19, 2024
1 parent 42d608b commit 07ec454
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 15 deletions.
2 changes: 0 additions & 2 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,6 @@ linters-settings:
- name: cyclomatic
arguments: [ 100 ]

- name: unchecked-type-assertion
disabled: true
- name: struct-tag
disabled: true
- name: add-constant
Expand Down
17 changes: 10 additions & 7 deletions internal/dwarf/dwarf.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ func SizeForDWARFVar(
entry *dwarf.Entry,
readMemory MemoryReader,
) ([]Content, uint64, error) {
sizeOffset := entry.Val(dwarf.AttrType).(dwarf.Offset)
sizeOffset, ok := entry.Val(dwarf.AttrType).(dwarf.Offset)
if !ok {
return nil, 0, fmt.Errorf("failed to get type offset")
}

typ, err := d.Type(sizeOffset)
if err != nil {
Expand Down Expand Up @@ -82,16 +85,16 @@ func SizeForDWARFVar(
func EntryShouldIgnore(entry *dwarf.Entry) bool {
declaration := entry.Val(dwarf.AttrDeclaration)
if declaration != nil {
val := declaration.(bool)
if val {
val, ok := declaration.(bool)
if ok && val {
return true
}
}

inline := entry.Val(dwarf.AttrInline)
if inline != nil {
val := inline.(int64)
if val > 0 {
val, ok := inline.(bool)
if ok && val {
return true
}
}
Expand Down Expand Up @@ -124,8 +127,8 @@ func EntryFileReader(cu *dwarf.Entry, d *dwarf.Data) func(entry *dwarf.Entry) st
slog.Warn(fmt.Sprintf("Failed to load DWARF function file: %s", EntryPrettyPrinter(entry)))
return defaultName
}
fileIndex := fileIndexAny.(int64)
if fileIndex < 0 || int(fileIndex) >= len(files) {
fileIndex, ok := fileIndexAny.(int64)
if !ok || fileIndex < 0 || int(fileIndex) >= len(files) {
slog.Warn(fmt.Sprintf("Failed to load DWARF function file: %s", EntryPrettyPrinter(entry)))
return defaultName
}
Expand Down
10 changes: 8 additions & 2 deletions internal/dwarf/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,14 @@ func readEmbedFS(typ *dwarf.StructType, readMemory MemoryReader) ([]Content, err

ptr := readUintTo64(data)

filesPtrType := typ.Field[0].Type.(*dwarf.PtrType)
filesType := filesPtrType.Type.(*dwarf.StructType)
filesPtrType, ok := typ.Field[0].Type.(*dwarf.PtrType)
if !ok {
return nil, fmt.Errorf("unexpected type: %T", typ.Field[0].Type)
}
filesType, ok := filesPtrType.Type.(*dwarf.StructType)
if !ok {
return nil, fmt.Errorf("unexpected type: %T", filesPtrType.Type)
}

filesAddr, filesLen, err := readSlice(filesType, func(addr, size uint64) ([]byte, error) {
if addr == math.MaxUint64 {
Expand Down
27 changes: 23 additions & 4 deletions internal/knowninfo/dwarf.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,11 @@ func (k *KnownInfo) AddDwarfSubProgram(
pkg *entity.Package,
readFileName func(entry *dwarf.Entry) string,
) {
subEntryName := subEntry.Val(dwarf.AttrName).(string)
subEntryName, ok := subEntry.Val(dwarf.AttrName).(string)
if !ok {
slog.Warn(fmt.Sprintf("Failed to load DWARF function name: %s", dwarfutil.EntryPrettyPrinter(subEntry)))
return
}

ranges, err := d.Ranges(subEntry)
if err != nil {
Expand Down Expand Up @@ -136,8 +140,16 @@ func (k *KnownInfo) AddDwarfSubProgram(
}

func (k *KnownInfo) GetPackageFromDwarfCompileUnit(cuEntry *dwarf.Entry) *entity.Package {
cuLang, _ := cuEntry.Val(dwarf.AttrLanguage).(int64)
cuName, _ := cuEntry.Val(dwarf.AttrName).(string)
cuLang, ok := cuEntry.Val(dwarf.AttrLanguage).(int64)
if !ok {
slog.Warn(fmt.Sprintf("Failed to load DWARF compile unit language: %s", dwarfutil.EntryPrettyPrinter(cuEntry)))
return nil
}
cuName, ok := cuEntry.Val(dwarf.AttrName).(string)
if !ok {
slog.Warn(fmt.Sprintf("Failed to load DWARF compile unit name: %s", dwarfutil.EntryPrettyPrinter(cuEntry)))
return nil
}

var pkg *entity.Package

Expand Down Expand Up @@ -171,9 +183,16 @@ func (k *KnownInfo) GetPackageFromDwarfCompileUnit(cuEntry *dwarf.Entry) *entity
}

func (k *KnownInfo) LoadDwarfCompileUnit(d *dwarf.Data, cuEntry *dwarf.Entry, pendingEntry []*dwarf.Entry, ptrSize int) {
cuLang, _ := cuEntry.Val(dwarf.AttrLanguage).(int64)
cuLang, ok := cuEntry.Val(dwarf.AttrLanguage).(int64)
if !ok {
slog.Warn(fmt.Sprintf("Failed to load DWARF compile unit language: %s", dwarfutil.EntryPrettyPrinter(cuEntry)))
return
}

pkg := k.GetPackageFromDwarfCompileUnit(cuEntry)
if pkg == nil {
return
}

readFileName := dwarfutil.EntryFileReader(cuEntry, d)

Expand Down

0 comments on commit 07ec454

Please sign in to comment.