Yield NULL from read_expression() on invalid input

This commit is contained in:
Camden Dixie O'Brien 2024-10-24 20:57:29 +01:00
parent e0070bfb1e
commit 10803109db
2 changed files with 21 additions and 2 deletions

View File

@ -123,6 +123,9 @@ const expression_t *
read_expression(memory_pool_t *pool, const char *input, int len)
{
expression_t *expression;
parse_expression(pool, input, len, &expression);
return expression;
const int used = parse_expression(pool, input, len, &expression);
if (used == len)
return expression;
else
return NULL;
}

View File

@ -166,6 +166,20 @@ static void paren_1_plus_2_times_3_close_paren_parses_as_left_nested(void)
ASSERT_EQUAL(3, operand1->number);
}
static void garbage_input_yeilds_null(void)
{
init_memory_pool(&pool);
const expression_t *result = read_expression(&pool, "arwizxhu", 8);
ASSERT_NULL(result);
}
static void valid_expression_followed_by_garbage_yeilds_null(void)
{
init_memory_pool(&pool);
const expression_t *result = read_expression(&pool, "5+2jvkre", 8);
ASSERT_NULL(result);
}
int main(void)
{
TESTING_BEGIN();
@ -180,5 +194,7 @@ int main(void)
RUN_TEST(input_1_times_2_times_3_is_parsed_as_a_left_nested_application);
RUN_TEST(input_1_plus_2_times_3_is_parsed_as_a_right_nested_application);
RUN_TEST(paren_1_plus_2_times_3_close_paren_parses_as_left_nested);
RUN_TEST(garbage_input_yeilds_null);
RUN_TEST(valid_expression_followed_by_garbage_yeilds_null);
TESTING_END();
}