Parser: Fix destroying invalid data in nested pairs

This commit is contained in:
Laria 2023-07-01 16:59:23 +02:00
parent 35a3c808c4
commit 159004c2e4
2 changed files with 30 additions and 6 deletions

View file

@ -2108,13 +2108,30 @@ error:
return false;
}
static struct fragment
empty_fragment(void)
{
return (struct fragment) { // Something we can safely deinit
.type = FRAG_BLANK,
};
}
static struct fragment *
new_empty_fragment(struct apfl_allocator allocator)
{
struct fragment *fragment = ALLOC_OBJ(allocator, struct fragment);
if (fragment == NULL) {
return NULL;
}
*fragment = empty_fragment();
return fragment;
}
static enum parse_fragment_result
parse_fragment(apfl_parser_ptr p, struct fragment *fragment, bool need, enum parse_fragment_flags flags)
{
*fragment = (struct fragment) { // Something we can safely deinit
.type = FRAG_EXPAND,
.expand = NULL,
};
*fragment = empty_fragment();
struct fragment *lhs = NULL;
struct fragment *rhs = NULL;
@ -2242,8 +2259,8 @@ parse_fragment(apfl_parser_ptr p, struct fragment *fragment, bool need, enum par
case APFL_TOK_DOUBLE_COLON:
case APFL_TOK_COLON:
if (
((lhs = ALLOC_OBJ(p->allocator, struct fragment)) == NULL)
|| ((rhs = ALLOC_OBJ(p->allocator, struct fragment)) == NULL)
((lhs = new_empty_fragment(p->allocator)) == NULL)
|| ((rhs = new_empty_fragment(p->allocator)) == NULL)
) {
p->error = apfl_error_simple(APFL_ERR_MALLOC_FAILED);
return PF_ERROR;

View file

@ -1584,6 +1584,12 @@ TEST(err_unexpected_assignment_member_access, t) {
destroy_parser_test(pt);
}
TEST(err_unexpected_dot_in_nested_pairs, t) {
struct parser_test *pt = new_parser_test(t, "1::2::.3");
expect_error_of_type(pt, APFL_ERR_UNEXPECTED_TOKEN);
destroy_parser_test(pt);
}
TESTS_BEGIN
ADDTEST(empty),
ADDTEST(hello_world),
@ -1626,4 +1632,5 @@ TESTS_BEGIN
ADDTEST(err_unexpected_expression_in_member_assignable),
ADDTEST(err_unexpected_constant_in_member_assignable),
ADDTEST(err_unexpected_assignment_member_access),
ADDTEST(err_unexpected_dot_in_nested_pairs),
TESTS_END