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 <cbro@golang.org> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
This commit is contained in:
parent
4e70a1b26a
commit
a237aba5f5
|
@ -461,17 +461,14 @@ func pkgLinkFunc(path string) string {
|
||||||
return "pkg/" + path
|
return "pkg/" + path
|
||||||
}
|
}
|
||||||
|
|
||||||
// srcToPkgLinkFunc builds an <a> tag linking to
|
// srcToPkgLinkFunc builds an <a> tag linking to the package
|
||||||
// the package documentation of relpath.
|
// documentation of relpath.
|
||||||
func srcToPkgLinkFunc(relpath string) string {
|
func srcToPkgLinkFunc(relpath string) string {
|
||||||
relpath = pkgLinkFunc(relpath)
|
relpath = pkgLinkFunc(relpath)
|
||||||
if relpath == "pkg/" {
|
relpath = pathpkg.Dir(relpath)
|
||||||
|
if relpath == "pkg" {
|
||||||
return `<a href="/pkg">Index</a>`
|
return `<a href="/pkg">Index</a>`
|
||||||
}
|
}
|
||||||
if i := strings.LastIndex(relpath, "/"); i != -1 {
|
|
||||||
// Remove filename after last slash.
|
|
||||||
relpath = relpath[:i]
|
|
||||||
}
|
|
||||||
return fmt.Sprintf(`<a href="/%s">%s</a>`, relpath, relpath[len("pkg/"):])
|
return fmt.Sprintf(`<a href="/%s">%s</a>`, relpath, relpath[len("pkg/"):])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -313,6 +313,8 @@ func TestSrcToPkgLinkFunc(t *testing.T) {
|
||||||
}{
|
}{
|
||||||
{"src/", `<a href="/pkg">Index</a>`},
|
{"src/", `<a href="/pkg">Index</a>`},
|
||||||
{"src/fmt/", `<a href="/pkg/fmt">fmt</a>`},
|
{"src/fmt/", `<a href="/pkg/fmt">fmt</a>`},
|
||||||
|
{"pkg/", `<a href="/pkg">Index</a>`},
|
||||||
|
{"pkg/LICENSE", `<a href="/pkg">Index</a>`},
|
||||||
} {
|
} {
|
||||||
if got := srcToPkgLinkFunc(tc.path); got != tc.want {
|
if got := srcToPkgLinkFunc(tc.path); got != tc.want {
|
||||||
t.Errorf("srcToPkgLinkFunc(%v) = %v; want %v", tc.path, got, tc.want)
|
t.Errorf("srcToPkgLinkFunc(%v) = %v; want %v", tc.path, got, tc.want)
|
||||||
|
|
Loading…
Reference in New Issue