mirror of https://github.com/golang/go.git
godoc: fix potential index out-of-bounds error
R=golang-dev, bradfitz, dsymonds CC=golang-dev https://golang.org/cl/5683072
This commit is contained in:
parent
c2cd0d09c2
commit
d74680ea1c
|
|
@ -139,14 +139,16 @@ func (m *Mapping) Fprint(w io.Writer) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const sep = string(filepath.Separator)
|
||||||
|
|
||||||
// splitFirst splits a path at the first path separator and returns
|
// splitFirst splits a path at the first path separator and returns
|
||||||
// the path's head (the top-most directory specified by the path) and
|
// the path's head (the top-most directory specified by the path) and
|
||||||
// its tail (the rest of the path). If there is no path separator,
|
// its tail (the rest of the path). If there is no path separator,
|
||||||
// splitFirst returns path as head, and the the empty string as tail.
|
// splitFirst returns path as head, and the empty string as tail.
|
||||||
// Specifically, splitFirst("foo") == splitFirst("foo/").
|
// Specifically, splitFirst("foo") == splitFirst("foo/").
|
||||||
//
|
//
|
||||||
func splitFirst(path string) (head, tail string) {
|
func splitFirst(path string) (head, tail string) {
|
||||||
if i := strings.Index(path, string(filepath.Separator)); i > 0 {
|
if i := strings.Index(path, sep); i > 0 {
|
||||||
// 0 < i < len(path)
|
// 0 < i < len(path)
|
||||||
return path[0:i], path[i+1:]
|
return path[0:i], path[i+1:]
|
||||||
}
|
}
|
||||||
|
|
@ -179,7 +181,7 @@ func (m *Mapping) ToAbsolute(spath string) string {
|
||||||
func (m *Mapping) ToRelative(fpath string) string {
|
func (m *Mapping) ToRelative(fpath string) string {
|
||||||
for _, e := range m.list {
|
for _, e := range m.list {
|
||||||
// if fpath has prefix e.path, the next character must be a separator (was issue 3096)
|
// if fpath has prefix e.path, the next character must be a separator (was issue 3096)
|
||||||
if strings.HasPrefix(fpath, e.path) && fpath[len(e.path)] == filepath.Separator {
|
if strings.HasPrefix(fpath, e.path+sep) {
|
||||||
spath := filepath.ToSlash(fpath)
|
spath := filepath.ToSlash(fpath)
|
||||||
// /absolute/prefix/foo -> prefix/foo
|
// /absolute/prefix/foo -> prefix/foo
|
||||||
return path.Join(e.prefix, spath[len(e.path):]) // Join will remove a trailing '/'
|
return path.Join(e.prefix, spath[len(e.path):]) // Join will remove a trailing '/'
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue