From 710d0540e27f57b2589552965418b95b88187fa7 Mon Sep 17 00:00:00 2001 From: Shenghou Ma Date: Fri, 17 Feb 2012 11:29:34 -0500 Subject: [PATCH] cmd/dist: make dir check in defaulttarg() more robust 1, strip last path separator from $GOROOT The user might define GOROOT=/path/to/go/, but then the dir check in defaulttarg() will always complain the current dir is not within $GOROOT/src/. 2, resolve symlinks in the default goroot Or if getcwd() returns a fully-resolved path, the check in defaulttarg() will always fail. R=rsc CC=golang-dev https://golang.org/cl/5649073 --- src/cmd/dist/build.c | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/src/cmd/dist/build.c b/src/cmd/dist/build.c index f31c83ea7a..6cb33ab10f 100644 --- a/src/cmd/dist/build.c +++ b/src/cmd/dist/build.c @@ -77,8 +77,12 @@ init(void) binit(&b); xgetenv(&b, "GOROOT"); - if(b.len > 0) + if(b.len > 0) { + // if not "/", then strip trailing path separator + if(b.len >= 2 && b.p[b.len - 1] == slash[0]) + b.len--; goroot = btake(&b); + } xgetenv(&b, "GOBIN"); if(b.len == 0) @@ -1373,20 +1377,30 @@ static char* defaulttarg(void) { char *p; - Buf pwd, src; + Buf pwd, src, real_src; binit(&pwd); binit(&src); + binit(&real_src); + // xgetwd might return a path with symlinks fully resolved, and if + // there happens to be symlinks in goroot, then the hasprefix test + // will never succeed. Instead, we use xrealwd to get a canonical + // goroot/src before the comparison to avoid this problem. xgetwd(&pwd); p = btake(&pwd); bpathf(&src, "%s/src/", goroot); - if(!hasprefix(p, bstr(&src))) - fatal("current directory %s is not under %s", p, bstr(&src)); - p += src.len; + xrealwd(&real_src, bstr(&src)); + if(!hasprefix(p, bstr(&real_src))) + fatal("current directory %s is not under %s", p, bstr(&real_src)); + p += real_src.len; + // guard againt xrealwd return the directory without the trailing / + if(*p == slash[0]) + p++; bfree(&pwd); bfree(&src); + bfree(&real_src); return p; } -- 2.48.1