Skip to content

Commit fdcad08

Browse files
bors[bot]Mark Wielaard
and
Mark Wielaard
authored
Merge #695
695: Fix raw byte string parsing of zero and out of range bytes r=philberty a=CohenArthur 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. Patch: https://code.wildebeest.org/git/user/mjw/gccrs/commit/?h=parse-raw-byte-string Email: https://gcc.gnu.org/pipermail/gcc-rust/2021-September/000227.html Co-authored-by: Mark Wielaard <[email protected]>
2 parents 28f527c + e6e1cc1 commit fdcad08

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

gcc/rust/lex/rust-lex.cc

+15-5
Original file line numberDiff line numberDiff line change
@@ -1423,29 +1423,31 @@ Lexer::parse_partial_hex_escape ()
14231423
char hexNum[3] = {0, 0, 0};
14241424

14251425
// first hex char
1426-
skip_input ();
1427-
current_char = peek_input ();
1426+
current_char = peek_input (1);
14281427
int additional_length_offset = 1;
14291428

14301429
if (!is_x_digit (current_char))
14311430
{
14321431
rust_error_at (get_current_location (),
14331432
"invalid character %<\\x%c%> in \\x sequence",
14341433
current_char);
1434+
return std::make_pair (0, 0);
14351435
}
14361436
hexNum[0] = current_char;
14371437

14381438
// second hex char
14391439
skip_input ();
1440-
current_char = peek_input ();
1440+
current_char = peek_input (1);
14411441
additional_length_offset++;
14421442

14431443
if (!is_x_digit (current_char))
14441444
{
14451445
rust_error_at (get_current_location (),
1446-
"invalid character %<\\x%c%> in \\x sequence",
1446+
"invalid character %<\\x%c%c%> in \\x sequence", hexNum[0],
14471447
current_char);
1448+
return std::make_pair (0, 1);
14481449
}
1450+
skip_input ();
14491451
hexNum[1] = current_char;
14501452

14511453
long hexLong = std::strtol (hexNum, nullptr, 16);
@@ -1627,7 +1629,7 @@ Lexer::parse_byte_string (Location loc)
16271629
else
16281630
length += std::get<1> (escape_length_pair);
16291631

1630-
if (output_char != 0)
1632+
if (output_char != 0 || !std::get<2> (escape_length_pair))
16311633
str += output_char;
16321634

16331635
continue;
@@ -1722,6 +1724,14 @@ Lexer::parse_raw_byte_string (Location loc)
17221724
}
17231725
}
17241726

1727+
if ((unsigned char) current_char > 127)
1728+
{
1729+
rust_error_at (get_current_location (),
1730+
"character %<%c%> in raw byte string out of range",
1731+
current_char);
1732+
current_char = 0;
1733+
}
1734+
17251735
length++;
17261736

17271737
str += current_char;
3.16 KB
Binary file not shown.

0 commit comments

Comments
 (0)