Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sys/ztimer64: fix ztimer64_remove() not properly clearing timer struct #17720

Merged
merged 2 commits into from
Mar 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions sys/ztimer64/ztimer64.c
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,12 @@ static int _add_entry_to_list(ztimer64_clock_t *clock, ztimer64_base_t *entry)
}
}

static void _clear(ztimer64_base_t *entry)
{
entry->next = NULL;
entry->target = 0;
}

static int _del_entry_from_list(ztimer64_clock_t *clock, ztimer64_base_t *entry)
{
DEBUG("_del_entry_from_list()\n");
Expand All @@ -137,7 +143,7 @@ static int _del_entry_from_list(ztimer64_clock_t *clock, ztimer64_base_t *entry)
if (clock->first == entry) {
/* special case: removing first entry */
clock->first = entry->next;
entry->next = 0;
_clear(entry);
return 1;
}
else {
Expand All @@ -151,7 +157,7 @@ static int _del_entry_from_list(ztimer64_clock_t *clock, ztimer64_base_t *entry)
pos = pos->next;
}

entry->next = 0;
_clear(entry);

return 0;
}
Expand Down Expand Up @@ -238,9 +244,9 @@ void ztimer64_handler(void *arg)
ztimer64_t *timer = (ztimer64_t *)clock->first;
/* remove timer from list */
clock->first = clock->first->next;

/* clear timer struct so it can be re-set */
timer->base.next = NULL;
timer->base.target = 0;
_clear(&timer->base);

/* shoot callback */
timer->callback(timer->arg);
Expand Down
23 changes: 23 additions & 0 deletions tests/unittests/tests-ztimer64/tests-ztimer64-core.c
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,28 @@ static void test_ztimer64_set_uninitialized(void)
ztimer64_set(z64, &timer, 0);
}

static void test_ztimer64_remove_clear(void)
{
/* regression test, testing if a removed timer passes `!(is_set(t)` */
ztimer_mock_t zmock;
ztimer_clock_t *z = &zmock.super;
ztimer64_clock_t z64mock;
ztimer64_clock_t *z64 = &z64mock;

memset(&zmock, '\0', sizeof(ztimer_mock_t));
memset(&z64mock, '\0', sizeof(ztimer64_clock_t));
/* ztimer base clock is already extended to 32bit */
ztimer_mock_init(&zmock, 32);
ztimer64_clock_init(z64, z);

ztimer64_t timer = { .base.target = 1 };

ztimer64_set(z64, &timer, 100000000LLU);
TEST_ASSERT(ztimer64_is_set(&timer));
ztimer64_remove(z64, &timer);
TEST_ASSERT(!ztimer64_is_set(&timer));
}

Test *tests_ztimer64_tests(void)
{
EMB_UNIT_TESTFIXTURES(fixtures) {
Expand All @@ -225,6 +247,7 @@ Test *tests_ztimer64_tests(void)
new_TestFixture(test_ztimer64_set_at),
new_TestFixture(test_ztimer64_checkpoint),
new_TestFixture(test_ztimer64_set_uninitialized),
new_TestFixture(test_ztimer64_remove_clear),
};

EMB_UNIT_TESTCALLER(ztimer64_tests, setup, NULL, fixtures);
Expand Down