Commit 10aede26 authored by Elias Naur's avatar Elias Naur

misc/android: fix detection of GOROOT tests

strings.HasPrefix is not good enough to determine whether a path
is a subdirectory of another because it does not respect path
boundaries. filepath.Rel is good eonugh as long as we filter out results
that use parent directories, "..".

Hopefully fix the android emulator builders on the subrepositories.

Change-Id: I17ee7e0028c0b0b26a6c5f67629f53c9a660c6e5
Reviewed-on: https://go-review.googlesource.com/c/go/+/167117
Run-TryBot: Elias Naur <mail@eliasnaur.com>
Reviewed-by: default avatarBrad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
parent d6891bd4
...@@ -196,27 +196,23 @@ func subdir() (pkgpath string, underGoRoot bool, err error) { ...@@ -196,27 +196,23 @@ func subdir() (pkgpath string, underGoRoot bool, err error) {
if err != nil { if err != nil {
return "", false, err return "", false, err
} }
if strings.HasPrefix(cwd, goroot) { if subdir, err := filepath.Rel(goroot, cwd); err == nil {
subdir, err := filepath.Rel(goroot, cwd) if !strings.Contains(subdir, "..") {
if err != nil {
return "", false, err
}
return subdir, true, nil return subdir, true, nil
} }
}
for _, p := range filepath.SplitList(build.Default.GOPATH) { for _, p := range filepath.SplitList(build.Default.GOPATH) {
pabs, err := filepath.EvalSymlinks(p) pabs, err := filepath.EvalSymlinks(p)
if err != nil { if err != nil {
return "", false, err return "", false, err
} }
if !strings.HasPrefix(cwd, pabs) { if subdir, err := filepath.Rel(pabs, cwd); err == nil {
continue if !strings.Contains(subdir, "..") {
}
subdir, err := filepath.Rel(pabs, cwd)
if err == nil {
return subdir, false, nil return subdir, false, nil
} }
} }
}
return "", false, fmt.Errorf("the current path %q is not in either GOROOT(%q) or GOPATH(%q)", return "", false, fmt.Errorf("the current path %q is not in either GOROOT(%q) or GOPATH(%q)",
cwd, runtime.GOROOT(), build.Default.GOPATH) cwd, runtime.GOROOT(), build.Default.GOPATH)
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment