From a237aba5f578100f4f8419bbda73189b8e6dbae5 Mon Sep 17 00:00:00 2001 From: Andrew Bonventre Date: Thu, 3 Aug 2017 18:01:02 -0400 Subject: [PATCH] godoc: fix out-of-bounds panic when serving top-level files Change-Id: I0ba84bac0c97715c0bc66fdc4c33678341ef140c Reviewed-on: https://go-review.googlesource.com/53151 Reviewed-by: Chris Broadfoot Reviewed-by: Brad Fitzpatrick --- godoc/godoc.go | 11 ++++------- godoc/godoc_test.go | 2 ++ 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/godoc/godoc.go b/godoc/godoc.go index 5c6d9820..d6c27d0b 100644 --- a/godoc/godoc.go +++ b/godoc/godoc.go @@ -461,17 +461,14 @@ func pkgLinkFunc(path string) string { return "pkg/" + path } -// srcToPkgLinkFunc builds an tag linking to -// the package documentation of relpath. +// srcToPkgLinkFunc builds an tag linking to the package +// documentation of relpath. func srcToPkgLinkFunc(relpath string) string { relpath = pkgLinkFunc(relpath) - if relpath == "pkg/" { + relpath = pathpkg.Dir(relpath) + if relpath == "pkg" { return `Index` } - if i := strings.LastIndex(relpath, "/"); i != -1 { - // Remove filename after last slash. - relpath = relpath[:i] - } return fmt.Sprintf(`%s`, relpath, relpath[len("pkg/"):]) } diff --git a/godoc/godoc_test.go b/godoc/godoc_test.go index dca1c951..c1d631c1 100644 --- a/godoc/godoc_test.go +++ b/godoc/godoc_test.go @@ -313,6 +313,8 @@ func TestSrcToPkgLinkFunc(t *testing.T) { }{ {"src/", `Index`}, {"src/fmt/", `fmt`}, + {"pkg/", `Index`}, + {"pkg/LICENSE", `Index`}, } { if got := srcToPkgLinkFunc(tc.path); got != tc.want { t.Errorf("srcToPkgLinkFunc(%v) = %v; want %v", tc.path, got, tc.want)