summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRuss Cox <rsc@golang.org>2012-02-29 15:28:36 -0500
committerRuss Cox <rsc@golang.org>2012-02-29 15:28:36 -0500
commitc603b3ec6b1358a7c2b575e8f875435201c5a9cd (patch)
tree7ef9ada80e64c314262120f90846ed288500273b
parentbae94ac7cfd9387ee6b10495f368d8895297fe97 (diff)
downloadgo-c603b3ec6b1358a7c2b575e8f875435201c5a9cd.tar.gz
gc: disallow absolute import paths
They are broken and hard to make work. They have never worked: if you import "/tmp/x" from "/home/rsc/p.c" then the compiler rewrites this into import "/home/rsc/tmp/x", which is clearly wrong. Also we just disallowed the : character in import paths, so import "c:/foo" is already not allowed. Finally, in order to support absolute paths well in a build tool we'd have to provide a mechanism to instruct the compiler to resolve absolute imports by looking in some other tree (where the binaries live) and provide a mapping from absolute path to location in that tree. This CL avoids adding that complexity. This is not part of the language spec (and should not be), so no spec change is needed. If we need to make them work later, we can. R=ken2 CC=golang-dev http://codereview.appspot.com/5712043
-rw-r--r--src/cmd/gc/lex.c5
-rw-r--r--test/import5.go4
2 files changed, 9 insertions, 0 deletions
diff --git a/src/cmd/gc/lex.c b/src/cmd/gc/lex.c
index 140153a64..b393bccc4 100644
--- a/src/cmd/gc/lex.c
+++ b/src/cmd/gc/lex.c
@@ -637,6 +637,11 @@ importfile(Val *f, int line)
path = f->u.sval;
if(islocalname(path)) {
+ if(path->s[0] == '/') {
+ yyerror("import path cannot be absolute path");
+ fakeimport();
+ return;
+ }
cleanbuf = mal(strlen(pathname) + strlen(path->s) + 2);
strcpy(cleanbuf, pathname);
strcat(cleanbuf, "/");
diff --git a/test/import5.go b/test/import5.go
index 02d443b2e..6480acff9 100644
--- a/test/import5.go
+++ b/test/import5.go
@@ -49,3 +49,7 @@ import "\x80\x80" // ERROR "import path"
import `\x80\x80` // ERROR "import path"
import "\xFFFD" // ERROR "import path"
import `\xFFFD` // ERROR "import path"
+
+// Invalid local imports.
+import "/foo" // ERROR "import path cannot be absolute path"
+import "c:/foo" // ERROR "import path contains invalid character"