Skip to content

Commit

Permalink
debug/dwarf: better error handling in SeekPC
Browse files Browse the repository at this point in the history
The dwarf.Reader "SeekPC" method was not properly handling the case
of a truncated/empty unit (something that has header information
but an empty abbrev table and no DIEs). Add some guards to handle
this case.

Fixes #52045.

Change-Id: I978163eca3b610f7528058693b840931e90d3f63
Reviewed-on: https://go-review.googlesource.com/c/go/+/397054
Reviewed-by: Ian Lance Taylor <[email protected]>
Trust: Than McIntosh <[email protected]>
Run-TryBot: Than McIntosh <[email protected]>
TryBot-Result: Gopher Robot <[email protected]>
  • Loading branch information
thanm committed Apr 1, 2022
1 parent 2e8dc8f commit 1fc3346
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/debug/dwarf/entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -974,7 +974,7 @@ func (r *Reader) SeekPC(pc uint64) (*Entry, error) {
u := &r.d.unit[unit]
r.b = makeBuf(r.d, u, "info", u.off, u.data)
e, err := r.Next()
if err != nil {
if err != nil || e == nil || e.Tag == 0 {
return nil, err
}
ranges, err := r.d.Ranges(e)
Expand Down
15 changes: 15 additions & 0 deletions src/debug/dwarf/entry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -427,3 +427,18 @@ func TestIssue51758(t *testing.T) {
}
}
}

func TestIssue52045(t *testing.T) {
var abbrev, aranges, frame, line, pubnames, ranges, str []byte
info := []byte{0x7, 0x0, 0x0, 0x0, 0x2, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}

// A hand-crafted input corresponding to a minimal-size
// .debug_info (header only, no DIEs) and an empty abbrev table.
data0, _ := New(abbrev, aranges, frame, info, line, pubnames, ranges, str)
reader0 := data0.Reader()
entry0, _ := reader0.SeekPC(0x0)
// main goal is to make sure we can get here without crashing
if entry0 != nil {
t.Errorf("got non-nil entry0, wanted nil")
}
}

0 comments on commit 1fc3346

Please sign in to comment.