From 1c9dff6dad33573f75c7b495d6d90d96c6085eaf Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Tue, 2 Feb 2016 12:10:28 -0800 Subject: [PATCH] Guard memcpy calls in ts_parser__accept --- src/runtime/parser.c | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/runtime/parser.c b/src/runtime/parser.c index ec2926dd..d002e47f 100644 --- a/src/runtime/parser.c +++ b/src/runtime/parser.c @@ -602,13 +602,16 @@ static ParseActionResult ts_parser__accept(TSParser *self, int head) { if (!new_children) return FailedToUpdateStackHead; - memcpy(new_children, pop_result->trees, - leading_extra_count * sizeof(TSTree *)); - memcpy(new_children + leading_extra_count, root->children, - root->child_count * sizeof(TSTree *)); - memcpy(new_children + leading_extra_count + root->child_count, - pop_result->trees + leading_extra_count + 1, - trailing_extra_count * sizeof(TSTree *)); + if (leading_extra_count > 0) + memcpy(new_children, pop_result->trees, + leading_extra_count * sizeof(TSTree *)); + if (root->child_count > 0) + memcpy(new_children + leading_extra_count, root->children, + root->child_count * sizeof(TSTree *)); + if (trailing_extra_count > 0) + memcpy(new_children + leading_extra_count + root->child_count, + pop_result->trees + leading_extra_count + 1, + trailing_extra_count * sizeof(TSTree *)); size_t new_count = root->child_count + leading_extra_count + trailing_extra_count; ts_tree_set_children(root, new_count, new_children);