From 7ae42e3a8b8a2b058054123e1bf798a76cd0f932 Mon Sep 17 00:00:00 2001 From: Pontus Leitzler Date: Mon, 2 Mar 2020 15:28:51 +0100 Subject: [PATCH] internal/lsp/debug: add nil checks in debug page There are missing nil checks in a few places when using the gopls debug page. It will result in panics when trying to access a debug page that no longer exist (for example a closed session). This change adds nil checks and updates the template to not render non-existing debug address. Change-Id: Ic9163f3727fd8c51122cbd4ab4374fc4d55477c3 Reviewed-on: https://go-review.googlesource.com/c/tools/+/221699 Reviewed-by: Robert Findley Run-TryBot: Robert Findley TryBot-Result: Gobot Gobot --- internal/lsp/debug/serve.go | 40 +++++++++++++++++++++++++++++-------- 1 file changed, 32 insertions(+), 8 deletions(-) diff --git a/internal/lsp/debug/serve.go b/internal/lsp/debug/serve.go index 102f08eff83..28ed7b96c50 100644 --- a/internal/lsp/debug/serve.go +++ b/internal/lsp/debug/serve.go @@ -276,11 +276,15 @@ func (i *Instance) getCache(r *http.Request) interface{} { i.State.mu.Lock() defer i.State.mu.Unlock() id := path.Base(r.URL.Path) + c, ok := i.State.caches.find(id).(Cache) + if !ok { + return nil + } result := struct { Cache Sessions []Session }{ - Cache: i.State.caches.find(id).(Cache), + Cache: c, } // now find all the views that belong to this session @@ -297,11 +301,15 @@ func (i *Instance) getSession(r *http.Request) interface{} { i.State.mu.Lock() defer i.State.mu.Unlock() id := path.Base(r.URL.Path) + s, ok := i.State.sessions.find(id).(Session) + if !ok { + return nil + } result := struct { Session Views []View }{ - Session: i.State.sessions.find(id).(Session), + Session: s, } // now find all the views that belong to this session for _, vd := range i.State.views.objs { @@ -317,21 +325,33 @@ func (i Instance) getClient(r *http.Request) interface{} { i.State.mu.Lock() defer i.State.mu.Unlock() id := path.Base(r.URL.Path) - return i.State.clients.find(id).(Client) + c, ok := i.State.clients.find(id).(Client) + if !ok { + return nil + } + return c } func (i Instance) getServer(r *http.Request) interface{} { i.State.mu.Lock() defer i.State.mu.Unlock() id := path.Base(r.URL.Path) - return i.State.servers.find(id).(Server) + s, ok := i.State.servers.find(id).(Server) + if !ok { + return nil + } + return s } func (i Instance) getView(r *http.Request) interface{} { i.State.mu.Lock() defer i.State.mu.Unlock() id := path.Base(r.URL.Path) - return i.State.views.find(id).(View) + v, ok := i.State.views.find(id).(View) + if !ok { + return nil + } + return v } func (i *Instance) getFile(r *http.Request) interface{} { @@ -339,7 +359,11 @@ func (i *Instance) getFile(r *http.Request) interface{} { defer i.State.mu.Unlock() hash := path.Base(r.URL.Path) sid := path.Base(path.Dir(r.URL.Path)) - return i.State.sessions.find(sid).(Session).File(hash) + s, ok := i.State.sessions.find(sid).(Session) + if !ok { + return nil + } + return s.File(hash) } func (i *Instance) getInfo(r *http.Request) interface{} { @@ -710,7 +734,7 @@ var clientTmpl = template.Must(template.Must(baseTemplate.Clone()).Parse(` {{define "title"}}Client {{.ID}}{{end}} {{define "body"}} Using session: {{template "sessionlink" .Session.ID}}
-Debug this client at: {{localAddress .DebugAddress}}
+{{if .DebugAddress}}Debug this client at: {{localAddress .DebugAddress}}
{{end}} Logfile: {{.Logfile}}
Gopls Path: {{.GoplsPath}}
{{end}} @@ -719,7 +743,7 @@ Gopls Path: {{.GoplsPath}}
var serverTmpl = template.Must(template.Must(baseTemplate.Clone()).Parse(` {{define "title"}}Server {{.ID}}{{end}} {{define "body"}} -Debug this server at: {{localAddress .DebugAddress}}
+{{if .DebugAddress}}Debug this server at: {{localAddress .DebugAddress}}
{{end}} Logfile: {{.Logfile}}
Gopls Path: {{.GoplsPath}}
{{end}}