Skip to content

Commit

Permalink
[ruby/yarp] fix: report syntax error for invalid hex escape
Browse files Browse the repository at this point in the history
  • Loading branch information
flavorjones authored and matzbot committed Sep 1, 2023
1 parent 512f821 commit cfe1edd
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
6 changes: 6 additions & 0 deletions test/yarp/errors_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,12 @@ def test_do_not_allow_multiple_codepoints_in_a_single_character_literal
]
end

def test_invalid_hex_escape
assert_errors expression('"\\xx"'), '"\\xx"', [
["Invalid hex escape.", 1..3],
]
end

def test_do_not_allow_more_than_6_hexadecimal_digits_in_u_Unicode_character_notation
expected = StringNode(Location(), Location(), Location(), "\u0001")

Expand Down
5 changes: 3 additions & 2 deletions yarp/unescape.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,10 @@ unescape_hexadecimal_digit(const uint8_t value) {
// Scan the 1-2 digits of hexadecimal into the value. Returns the number of
// digits scanned.
static inline size_t
unescape_hexadecimal(const uint8_t *backslash, uint8_t *value, const uint8_t *end) {
unescape_hexadecimal(const uint8_t *backslash, uint8_t *value, const uint8_t *end, yp_list_t *error_list) {
*value = 0;
if (backslash + 2 >= end || !yp_char_is_hexadecimal_digit(backslash[2])) {
if (error_list) yp_diagnostic_list_append(error_list, backslash, backslash + 2, "Invalid hex escape.");
return 2;
}
*value = unescape_hexadecimal_digit(backslash[2]);
Expand Down Expand Up @@ -223,7 +224,7 @@ unescape(
// \xnn hexadecimal bit pattern, where nn is 1-2 hexadecimal digits ([0-9a-fA-F])
case 'x': {
uint8_t value;
const uint8_t *cursor = backslash + unescape_hexadecimal(backslash, &value, end);
const uint8_t *cursor = backslash + unescape_hexadecimal(backslash, &value, end, error_list);

if (dest) {
dest[(*dest_length)++] = unescape_char(value, flags);
Expand Down

0 comments on commit cfe1edd

Please sign in to comment.