From 1fc3346275d0457cfc154b1001b25bd0cb0c1751 Mon Sep 17 00:00:00 2001 From: Than McIntosh Date: Thu, 31 Mar 2022 07:47:19 -0400 Subject: [PATCH] debug/dwarf: better error handling in SeekPC 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 Trust: Than McIntosh Run-TryBot: Than McIntosh TryBot-Result: Gopher Robot --- src/debug/dwarf/entry.go | 2 +- src/debug/dwarf/entry_test.go | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/debug/dwarf/entry.go b/src/debug/dwarf/entry.go index 3bc6a5454e3799..98c17dc08ac1bb 100644 --- a/src/debug/dwarf/entry.go +++ b/src/debug/dwarf/entry.go @@ -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) diff --git a/src/debug/dwarf/entry_test.go b/src/debug/dwarf/entry_test.go index 393ad89f522bc6..4e96dbfc1d5640 100644 --- a/src/debug/dwarf/entry_test.go +++ b/src/debug/dwarf/entry_test.go @@ -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") + } +}