>From e6e1cc117e1f08dfde86f5a3b1988e4b7206a7da Mon Sep 17 00:00:00 2001
From: Mark Wielaard <m...@klomp.org>
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_dMaIdKBLE<JDDhNy~HEHGO5v9U=0T+ODUv
zC8SmEy1cFEe3@FkZM6EI->90VG(Y=lGOCeT&0tuLp+z$p*Er0}$>V{I!^8|YFtTxx
z@X*l4>D0{rUt=35bE5|t9J_s{&GuboZD*<I?tAKLvSqui3i{=Bxx4RJ6csV<pY3qx
zc%EYYDlHYkjRXh2@TuH1=aM+;Gqyvv;&mx;T8-!69@lIn-t3a5*&*G8KFpvI38NDZ
zXRR0;(@$zf^Mz-&9d5I9*G<Ew+mP5OSua;jH^}=FDaIRU+8^bv*+6`M(70oU!0U_=
z&}eCgAm&LiE1Ztuo)19+f+2(Qu2!m#_4vb;|G-CZh`7Kh;Epf$lpotHpVZa9RxPJ`
z*!LJ1N@A_5D-K2!c50h=c|}nH2&&HIi=qJdndb5#r=*{jFDfG+GVizjpnnJPCErUm
z(~py#01%ck2asI=5SB3ogcab#=?eJBr4{72%hYuq1akuw_HYtNmI2frgB`4t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