From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from gnu.wildebeest.org (wildebeest.demon.nl [212.238.236.112]) by sourceware.org (Postfix) with ESMTPS id 7E3243858C60 for ; Wed, 29 Sep 2021 20:21:05 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 7E3243858C60 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=klomp.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=klomp.org Received: from reform (deer0x00.wildebeest.org [172.31.17.130]) (using TLSv1.2 with cipher ADH-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by gnu.wildebeest.org (Postfix) with ESMTPSA id 44CA33000A0A for ; Wed, 29 Sep 2021 22:21:03 +0200 (CEST) Received: by reform (Postfix, from userid 1000) id 29B592E82FBF; Wed, 29 Sep 2021 22:21:02 +0200 (CEST) From: Mark Wielaard To: gcc-rust@gcc.gnu.org Subject: Date: Wed, 29 Sep 2021 22:20:37 +0200 Message-Id: <20210929202037.523300-1-mark@klomp.org> X-Mailer: git-send-email 2.32.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-10.7 required=5.0 tests=BAYES_00, GIT_PATCH_0, JMQ_SPF_NEUTRAL, KAM_BLANKSUBJECT, KAM_DMARC_STATUS, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: gcc-rust@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: gcc-rust mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 29 Sep 2021 20:21:08 -0000 >From e6e1cc117e1f08dfde86f5a3b1988e4b7206a7da Mon Sep 17 00:00:00 2001 From: Mark Wielaard Date: Wed, 29 Sep 2021 22:03:34 +0200 Subject: [PATCH] Fix raw byte string parsing of zero and out of range bytes Allow \0 escape in raw byte string and reject non-ascii byte values. Change parse_partial_hex_escapes to not skip bad characters to provide better error messages. Add rawbytestring.rs testcase to check string, raw string, byte string and raw byte string parsing. --- https://code.wildebeest.org/git/user/mjw/gccrs/commit/?h=parse-raw-byte-string gcc/rust/lex/rust-lex.cc | 20 +++++++++++++++----- gcc/testsuite/rust/compile/rawbytestring.rs | Bin 0 -> 3234 bytes 2 files changed, 15 insertions(+), 5 deletions(-) create mode 100644 gcc/testsuite/rust/compile/rawbytestring.rs diff --git a/gcc/rust/lex/rust-lex.cc b/gcc/rust/lex/rust-lex.cc index b70877be9ff..bbddea04d0c 100644 --- a/gcc/rust/lex/rust-lex.cc +++ b/gcc/rust/lex/rust-lex.cc @@ -1423,8 +1423,7 @@ Lexer::parse_partial_hex_escape () char hexNum[3] = {0, 0, 0}; // first hex char - skip_input (); - current_char = peek_input (); + current_char = peek_input (1); int additional_length_offset = 1; if (!is_x_digit (current_char)) @@ -1432,20 +1431,23 @@ Lexer::parse_partial_hex_escape () rust_error_at (get_current_location (), "invalid character %<\\x%c%> in \\x sequence", current_char); + return std::make_pair (0, 0); } hexNum[0] = current_char; // second hex char skip_input (); - current_char = peek_input (); + current_char = peek_input (1); additional_length_offset++; if (!is_x_digit (current_char)) { rust_error_at (get_current_location (), - "invalid character %<\\x%c%> in \\x sequence", + "invalid character %<\\x%c%c%> in \\x sequence", hexNum[0], current_char); + return std::make_pair (0, 1); } + skip_input (); hexNum[1] = current_char; long hexLong = std::strtol (hexNum, nullptr, 16); @@ -1627,7 +1629,7 @@ Lexer::parse_byte_string (Location loc) else length += std::get<1> (escape_length_pair); - if (output_char != 0) + if (output_char != 0 || !std::get<2> (escape_length_pair)) str += output_char; continue; @@ -1722,6 +1724,14 @@ Lexer::parse_raw_byte_string (Location loc) } } + if ((unsigned char) current_char > 127) + { + rust_error_at (get_current_location (), + "character %<%c%> in raw byte string out of range", + current_char); + current_char = 0; + } + length++; str += current_char; diff --git a/gcc/testsuite/rust/compile/rawbytestring.rs b/gcc/testsuite/rust/compile/rawbytestring.rs new file mode 100644 index 0000000000000000000000000000000000000000..9c6b762a7fd378206a3bfe21db5b708890f5466f GIT binary patch literal 3234 zcmbVOO>fgc5amjL#mG4T6)1rVl`5`1a^M^Zc^f;m2zI;c($Wfvf5=~A-pqd4PU65Z z<9Tmp-n`vS-O~56Y3cQwv*$CS<&wUX59E5=v|Go4UDeZ9>ni$0wkR&M$OnWLi=tR8 zvhYe0>#n0kp8Z~u3yqU0ZIOclm4066_dMaIdKBLE90VG(Y=lGOCeT&0tuLp+z$p*Er0}$>V{I!^8|YFtTxx z@X*l4>D0{rUt=35bE5|t9J_s{&GuboZD*@|GH+hjmxkvqUg|FRiNY&& zP6(%e4anlh3W@7JWKOd9E)p`E*!Jfr70=|kILq??taYC%vd4tW9O052FwtHy(W1l^5)-!Q6rkeY2pcOb5 zC5~Q^#`Cf!S&N9GM~?nelacMDHe;2ejYcV-D%(M~7r|5FJ&7hOIQ$Oo!_o8>9i>^x zV>X-Uc!7Jfq5+jI?0J=nn!sj`v1wMcU}PH?O>D8bJ*^GcSU|ak{Lxs!g8aAiFN}Pz A0RR91 literal 0 HcmV?d00001 -- 2.32.0