From patchwork Mon Sep 17 05:15:51 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Lance Taylor X-Patchwork-Id: 184284 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) by ozlabs.org (Postfix) with SMTP id 4C9E72C0090 for ; Mon, 17 Sep 2012 15:16:21 +1000 (EST) Comment: DKIM? See http://www.dkim.org DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d=gcc.gnu.org; s=default; x=1348463783; h=Comment: DomainKey-Signature:Received:Received:Received:Received:Received: Received:Received:From:To:Subject:Date:Message-ID:User-Agent: MIME-Version:Content-Type:Mailing-List:Precedence:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:Sender: Delivered-To; bh=jUhDV4YJ8p44eP61Rb17lU8ShRE=; b=HOo5rsx5dil6wlr JNXF9S4BAtUxxqy/cdLDI2VZP4+mM1pMLihyBkibRiGDqME7QlxIQQ1hB5MK+cr9 uoFO+PystvTB2Ech5Jm9WZE8Umc5WoXJhoay4uEcNhdC52tsndShBicQrekYT+lF opAZHRPmo7ID0sNpyO31GW9V5B/k= Comment: DomainKeys? See http://antispam.yahoo.com/domainkeys DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=default; d=gcc.gnu.org; h=Received:Received:X-SWARE-Spam-Status:X-Spam-Check-By:Received:Received:X-Google-DKIM-Signature:Received:Received:Received:From:To:Subject:Date:Message-ID:User-Agent:MIME-Version:Content-Type:X-Gm-Message-State:X-IsSubscribed:Mailing-List:Precedence:List-Id:List-Unsubscribe:List-Archive:List-Post:List-Help:Sender:Delivered-To; b=iVpg56Bj37C6j5ebqklkYe0Zs3Tn/7w3D2/W3OWvnXAqGhLS09CfMjCgPYjj82 y9fjpUN03fB0eKW0IBkXVuUbx0AccetIxlvHxbQaUERidkeqlZN2qFX7nUr3yEoK tAEMacE1UsQgsbwZ4f8VIRYyDGgpjocLhmYgtG3W7rVhM=; Received: (qmail 12369 invoked by alias); 17 Sep 2012 05:16:10 -0000 Received: (qmail 12359 invoked by uid 22791); 17 Sep 2012 05:16:08 -0000 X-SWARE-Spam-Status: No, hits=-4.6 required=5.0 tests=AWL, BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, KHOP_RCVD_TRUST, RCVD_IN_DNSWL_LOW, RCVD_IN_HOSTKARMA_YE, RP_MATCHES_RCVD, T_TVD_MIME_NO_HEADERS X-Spam-Check-By: sourceware.org Received: from mail-pb0-f47.google.com (HELO mail-pb0-f47.google.com) (209.85.160.47) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Mon, 17 Sep 2012 05:15:55 +0000 Received: by pbcwy7 with SMTP id wy7so8638276pbc.20 for ; Sun, 16 Sep 2012 22:15:54 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=from:to:subject:date:message-id:user-agent:mime-version :content-type:x-gm-message-state; bh=EjUTntndpYXm8PnCTDrlPlQ75hRvY0OxTUM/RkU679w=; b=ejAWCNxI8qhYXuuA1r5fA+xkGfTKqlclzA0YFhbngQLtwF5h4x3OIOuofvTVX9dbRL raC1Wz2HGo3hjxC56cK4+eBOrMBWg6fNVz23XvBwA0ShpIaI56oPDt5zcFFp599pyHus YzjS4pcWnS3pO6G3hVSe/EGyoHOEc4491Iif1hdaPybwHmK4HFCBkBll5Ny080YBeM0r X1WNiR0iipj4S3CVMQTQlkBTIRZ8XTJkYoqMpX7EzvsF9i7Sar0EYgW8bD5njXpEuVRw U+t7joNiYRUa1I6Im0Rx7zEAXecZj9+sa3oKNrvAt1ElQhuiTXX/lhVevrIGrLwNK7mM xw7w== Received: by 10.66.75.74 with SMTP id a10mr18105209paw.46.1347858954356; Sun, 16 Sep 2012 22:15:54 -0700 (PDT) Received: by 10.66.75.74 with SMTP id a10mr18105199paw.46.1347858954227; Sun, 16 Sep 2012 22:15:54 -0700 (PDT) Received: from coign.google.com (adsl-71-133-8-30.dsl.pltn13.pacbell.net. [71.133.8.30]) by mx.google.com with ESMTPS id mu8sm6350834pbc.49.2012.09.16.22.15.53 (version=TLSv1/SSLv3 cipher=OTHER); Sun, 16 Sep 2012 22:15:53 -0700 (PDT) From: Ian Lance Taylor To: gcc-patches@gcc.gnu.org, gofrontend-dev@googlegroups.com Subject: Go patch committed: Detect invalid import statements Date: Sun, 16 Sep 2012 22:15:51 -0700 Message-ID: User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) MIME-Version: 1.0 X-Gm-Message-State: ALoCoQmOLal+ZvmJmnvnizr6SM+VwkLhZELees9m0ijaFL/sl23iiZzn303ASQ51mRZnZhPNqCYxYVAeB/pJgL6rzr31p6B/yUddOiGWAkxyeixXNN3lBX/hHmzw+cwn7w2RYywkZQ19af9Q8mlacctbgRVx+OQNnx8O2j9qStRVq7R8J8T+B72LmLd8B1jqWuy4YQ0Z8+FU X-IsSubscribed: yes Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org This patch to the Go compiler checks for invalid import filenames, including checks for cases that are technically valid but likely to be wrong. This corresponds to the tests done by the gc Go compiler. Bootstrapped and ran Go testsuite on x86_64-unknown-linux-gnu. Committed to mainline. Will commit to 4.7 branch when it is open again. Ian 2012-09-16 Ian Lance Taylor * Make-lang.in (go/gogo.o): Depend on filenames.h. diff -r 773bab2333b6 go/gogo.cc --- a/go/gogo.cc Mon Sep 10 21:59:01 2012 -0700 +++ b/go/gogo.cc Sun Sep 16 22:05:25 2012 -0700 @@ -6,6 +6,8 @@ #include "go-system.h" +#include "filenames.h" + #include "go-c.h" #include "go-dump.h" #include "lex.h" @@ -385,6 +387,57 @@ bool is_local_name_exported, Location location) { + if (filename.empty()) + { + error_at(location, "import path is empty"); + return; + } + + const char *pf = filename.data(); + const char *pend = pf + filename.length(); + while (pf < pend) + { + unsigned int c; + int adv = Lex::fetch_char(pf, &c); + if (adv == 0) + { + error_at(location, "import path contains invalid UTF-8 sequence"); + return; + } + if (c == '\0') + { + error_at(location, "import path contains NUL"); + return; + } + if (c < 0x20 || c == 0x7f) + { + error_at(location, "import path contains control character"); + return; + } + if (c == '\\') + { + error_at(location, "import path contains backslash; use slash"); + return; + } + if (Lex::is_unicode_space(c)) + { + error_at(location, "import path contains space character"); + return; + } + if (c < 0x7f && strchr("!\"#$%&'()*,:;<=>?[]^`{|}", c) != NULL) + { + error_at(location, "import path contains invalid character '%c'", c); + return; + } + pf += adv; + } + + if (IS_ABSOLUTE_PATH(filename.c_str())) + { + error_at(location, "import path cannot be absolute path"); + return; + } + if (filename == "unsafe") { this->import_unsafe(local_name, is_local_name_exported, location); diff -r 773bab2333b6 go/lex.cc --- a/go/lex.cc Mon Sep 10 21:59:01 2012 -0700 +++ b/go/lex.cc Sun Sep 16 22:05:25 2012 -0700 @@ -1705,6 +1705,27 @@ unsigned int stride; }; +// A table of whitespace characters--Unicode code points classified as +// "Space", "C" locale whitespace characters, the "next line" control +// character (0085), the line separator (2028), the paragraph +// separator (2029), and the "zero-width non-break space" (feff). + +static const Unicode_range unicode_space[] = +{ + { 0x0009, 0x000d, 1 }, + { 0x0020, 0x0020, 1 }, + { 0x0085, 0x0085, 1 }, + { 0x00a0, 0x00a0, 1 }, + { 0x1680, 0x1680, 1 }, + { 0x180e, 0x180e, 1 }, + { 0x2000, 0x200a, 1 }, + { 0x2028, 0x2029, 1 }, + { 0x202f, 0x202f, 1 }, + { 0x205f, 0x205f, 1 }, + { 0x3000, 0x3000, 1 }, + { 0xfeff, 0xfeff, 1 }, +}; + // A table of Unicode digits--Unicode code points classified as // "Digit". @@ -2294,6 +2315,15 @@ } } +// Return whether C is a space character. + +bool +Lex::is_unicode_space(unsigned int c) +{ + return Lex::is_in_unicode_range(c, unicode_space, + ARRAY_SIZE(unicode_space)); +} + // Return whether C is a Unicode digit--a Unicode code point // classified as "Digit". diff -r 773bab2333b6 go/lex.h --- a/go/lex.h Mon Sep 10 21:59:01 2012 -0700 +++ b/go/lex.h Sun Sep 16 22:05:25 2012 -0700 @@ -375,6 +375,10 @@ static int fetch_char(const char* str, unsigned int *value); + // Return whether C is a Unicode or "C" locale space character. + static bool + is_unicode_space(unsigned int c); + private: ssize_t get_line(); diff -r 773bab2333b6 go/parse.cc --- a/go/parse.cc Mon Sep 10 21:59:01 2012 -0700 +++ b/go/parse.cc Sun Sep 16 22:05:25 2012 -0700 @@ -5337,7 +5337,8 @@ if (!token->is_string()) { - error_at(this->location(), "missing import package name"); + error_at(this->location(), "import statement not a string"); + this->advance_token(); return; } Index: gcc/go/Make-lang.in =================================================================== --- gcc/go/Make-lang.in (revision 191279) +++ gcc/go/Make-lang.in (working copy) @@ -289,10 +289,11 @@ go/gogo-tree.o: go/gofrontend/gogo-tree. convert.h output.h $(DIAGNOSTIC_H) $(GO_TYPES_H) \ $(GO_EXPRESSIONS_H) $(GO_STATEMENTS_H) $(GO_RUNTIME_H) \ go/gofrontend/backend.h $(GO_GOGO_H) -go/gogo.o: go/gofrontend/gogo.cc $(GO_SYSTEM_H) $(GO_C_H) \ - go/gofrontend/go-dump.h $(GO_LEX_H) $(GO_TYPES_H) $(GO_STATEMENTS_H) \ - $(GO_EXPRESSIONS_H) go/gofrontend/dataflow.h $(GO_RUNTIME_H) \ - $(GO_IMPORT_H) $(GO_EXPORT_H) go/gofrontend/backend.h $(GO_GOGO_H) +go/gogo.o: go/gofrontend/gogo.cc $(GO_SYSTEM_H) \ + $(srcdir)/../include/filenames.h $(GO_C_H) go/gofrontend/go-dump.h \ + $(GO_LEX_H) $(GO_TYPES_H) $(GO_STATEMENTS_H) $(GO_EXPRESSIONS_H) \ + go/gofrontend/dataflow.h $(GO_RUNTIME_H) $(GO_IMPORT_H) \ + $(GO_EXPORT_H) go/gofrontend/backend.h $(GO_GOGO_H) go/import.o: go/gofrontend/import.cc $(GO_SYSTEM_H) \ $(srcdir)/../include/filenames.h $(srcdir)/../include/simple-object.h \ $(GO_C_H) $(GO_GOGO_H) $(GO_LEX_H) $(GO_TYPES_H) $(GO_EXPORT_H) \