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 26A0F3858413 for ; Tue, 21 Sep 2021 22:54:40 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 26A0F3858413 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 (deer0x04.wildebeest.org [172.31.17.134]) (using TLSv1.2 with cipher ADH-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by gnu.wildebeest.org (Postfix) with ESMTPSA id ED42E300071F; Wed, 22 Sep 2021 00:54:37 +0200 (CEST) Received: by reform (Postfix, from userid 1000) id 7CD782E82F18; Wed, 22 Sep 2021 00:54:37 +0200 (CEST) From: Mark Wielaard To: gcc-rust@gcc.gnu.org Cc: Mark Wielaard Subject: [PATCH] Fix byte char and byte string lexing code Date: Wed, 22 Sep 2021 00:54:30 +0200 Message-Id: <20210921225430.166550-1-mark@klomp.org> X-Mailer: git-send-email 2.32.0 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-9.4 required=5.0 tests=BAYES_00, BODY_8BITS, GIT_PATCH_0, JMQ_SPF_NEUTRAL, 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: Tue, 21 Sep 2021 22:54:41 -0000 There were two warnings in lexer parse_byte_char and parse_byte_string code for arches with signed chars: rust-lex.cc: In member function ‘Rust::TokenPtr Rust::Lexer::parse_byte_char(Location)’: rust-lex.cc:1564:21: warning: comparison is always false due to limited range of data type [-Wtype-limits] 1564 | if (byte_char > 127) | ~~~~~~~~~~^~~~~ rust-lex.cc: In member function ‘Rust::TokenPtr Rust::Lexer::parse_byte_string(Location)’: rust-lex.cc:1639:27: warning: comparison is always false due to limited range of data type [-Wtype-limits] 1639 | if (output_char > 127) | ~~~~~~~~~~~~^~~~~ The fix would be to cast to an unsigned char before the comparison. But that is actually wrong, and would produce the following errors parsing a byte char or byte string: bytecharstring.rs:3:14: error: ‘byte char’ ‘�’ out of range 3 | let _bc = b'\x80'; | ^ bytecharstring.rs:4:14: error: character ‘�’ in byte string out of range 4 | let _bs = b"foo\x80bar"; | ^ Both byte chars and byte strings may contain up to \xFF (255) characters. It is utf-8 chars or strings that can only Remove the faulty check and add a new testcase bytecharstring.rs that checks byte chars and strings do accept > 127 hex char escapes, but utf-8 chars and strings reject such hex char escapes. --- https://code.wildebeest.org/git/user/mjw/gccrs/commit/?h=bytecharstring gcc/rust/lex/rust-lex.cc | 15 --------------- gcc/testsuite/rust/compile/bytecharstring.rs | 8 ++++++++ 2 files changed, 8 insertions(+), 15 deletions(-) create mode 100644 gcc/testsuite/rust/compile/bytecharstring.rs diff --git a/gcc/rust/lex/rust-lex.cc b/gcc/rust/lex/rust-lex.cc index 49b6b6d32a7..b70877be9ff 100644 --- a/gcc/rust/lex/rust-lex.cc +++ b/gcc/rust/lex/rust-lex.cc @@ -1559,13 +1559,6 @@ Lexer::parse_byte_char (Location loc) byte_char = std::get<0> (escape_length_pair); length += std::get<1> (escape_length_pair); - if (byte_char > 127) - { - rust_error_at (get_current_location (), - "% %<%c%> out of range", byte_char); - byte_char = 0; - } - current_char = peek_input (); if (current_char != '\'') @@ -1634,14 +1627,6 @@ Lexer::parse_byte_string (Location loc) else length += std::get<1> (escape_length_pair); - if (output_char > 127) - { - rust_error_at (get_current_location (), - "character %<%c%> in byte string out of range", - output_char); - output_char = 0; - } - if (output_char != 0) str += output_char; diff --git a/gcc/testsuite/rust/compile/bytecharstring.rs b/gcc/testsuite/rust/compile/bytecharstring.rs new file mode 100644 index 00000000000..9242e2c5a0b --- /dev/null +++ b/gcc/testsuite/rust/compile/bytecharstring.rs @@ -0,0 +1,8 @@ +fn main () +{ + let _bc = b'\x80'; + let _bs = b"foo\x80bar"; + + let _c = '\xef'; // { dg-error "out of range" } + let _s = "Foo\xEFBar"; // { dg-error "out of range" } +} -- 2.32.0