summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMike Dalessio <mike.dalessio@gmail.com>2023-09-01 11:01:19 -0400
committergit <svn-admin@ruby-lang.org>2023-09-01 17:04:37 +0000
commitcfe1edddbf0512f029eaf8c6d68194d476120c16 (patch)
tree920dab3b172326a9c2e9e4a2fe72dd5e47232398
parent512f8217cb378c289b7d79cdf033715afcf82667 (diff)
[ruby/yarp] fix: report syntax error for invalid hex escape
Closes https://github.com/ruby/yarp/pull/1367 https://github.com/ruby/yarp/commit/b1ab54f526
-rw-r--r--test/yarp/errors_test.rb6
-rw-r--r--yarp/unescape.c5
2 files changed, 9 insertions, 2 deletions
diff --git a/test/yarp/errors_test.rb b/test/yarp/errors_test.rb
index caf5db650a..2af3c605e4 100644
--- a/test/yarp/errors_test.rb
+++ b/test/yarp/errors_test.rb
@@ -603,6 +603,12 @@ module YARP
]
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")
diff --git a/yarp/unescape.c b/yarp/unescape.c
index 84f0095da1..830c5996ae 100644
--- a/yarp/unescape.c
+++ b/yarp/unescape.c
@@ -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]);
@@ -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);