Skip to content

Commit

Permalink
Merge pull request #15947 from nmeum/pr/clif_get_attr_empty
Browse files Browse the repository at this point in the history
clif: Don't access any data if input is empty
  • Loading branch information
leandrolanzieri committed Feb 15, 2021
2 parents ffdddc8 + 0d141bf commit 85da504
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
4 changes: 4 additions & 0 deletions sys/clif/clif.c
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,10 @@ ssize_t clif_get_attr(const char *input, size_t input_len, clif_attr_t *attr)
attr->value = NULL;
attr->key = NULL;

if (input_len == 0) {
return CLIF_NOT_FOUND;
}

/* an attribute should start with the separator */
if (*pos != LF_ATTR_SEPARATOR_C) {
DEBUG("Attribute should start with separator, found %c\n", *pos);
Expand Down
11 changes: 11 additions & 0 deletions tests/unittests/tests-clif/tests-clif.c
Original file line number Diff line number Diff line change
Expand Up @@ -285,12 +285,23 @@ static void test_clif_get_attr_missing_value(void)
TEST_ASSERT_EQUAL_INT(strlen(input), r);
}

static void test_clif_get_attr_empty(void)
{
clif_attr_t attr;

/* clif_get_attr used to access data even if input was empty.
* See: https://github.com/RIOT-OS/RIOT/pull/15947 */
int r = clif_get_attr(NULL, 0, &attr);
TEST_ASSERT_EQUAL_INT(CLIF_NOT_FOUND, r);
}

Test *tests_clif_tests(void)
{
EMB_UNIT_TESTFIXTURES(fixtures) {
new_TestFixture(test_clif_encode_links),
new_TestFixture(test_clif_decode_links),
new_TestFixture(test_clif_get_attr_missing_value),
new_TestFixture(test_clif_get_attr_empty)
};

EMB_UNIT_TESTCALLER(clif_tests, NULL, NULL, fixtures);
Expand Down

0 comments on commit 85da504

Please sign in to comment.