Skip to content

Commit 06881c8

Browse files
committed
Write names should not underflow size_t
If the read_name is invalid, we shouldn't try to set a write name.
1 parent ed87bc6 commit 06881c8

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

src/yarp.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1508,12 +1508,17 @@ yp_call_node_variable_call_p(yp_call_node_t *node) {
15081508
// Initialize the read name by reading the write name and chopping off the '='.
15091509
static void
15101510
yp_call_write_read_name_init(yp_string_t *read_name, yp_string_t *write_name) {
1511-
size_t length = write_name->length - 1;
1511+
if (write_name->length >= 1) {
1512+
size_t length = write_name->length - 1;
15121513

1513-
void *memory = malloc(length);
1514-
memcpy(memory, write_name->source, length);
1514+
void *memory = malloc(length);
1515+
memcpy(memory, write_name->source, length);
15151516

1516-
yp_string_owned_init(read_name, (uint8_t *) memory, length);
1517+
yp_string_owned_init(read_name, (uint8_t *) memory, length);
1518+
} else {
1519+
// We can get here if the message was missing because of a syntax error.
1520+
yp_string_constant_init(read_name, "", 0);
1521+
}
15171522
}
15181523

15191524
// Allocate and initialize a new CallAndWriteNode node.

test/yarp/errors_test.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1137,6 +1137,11 @@ def test_duplicated_parameter_names
11371137
assert_errors expected, "def foo(a = 1,b,*c);end", [["Unexpected parameter `*`", 16..17]]
11381138
end
11391139

1140+
def test_invalid_message_name
1141+
result = YARP.parse("+.@foo,+=foo")
1142+
assert_equal "", result.value.statements.body.first.write_name
1143+
end
1144+
11401145
def test_invalid_operator_write_fcall
11411146
source = "foo! += 1"
11421147
assert_errors expression(source), source, [

0 commit comments

Comments
 (0)