Exercise 2 of Lesson 8 looks have a potential TOCTOU issue

academy.nordicsemi.com/.../

The comparison of "increment_count + decrement_count != COMBINED_TOTAL" happens after test_mutex unlock.
It is possible that between comparison and test_mutex unlock, "increment_count" or "decrement_count" is incremented by another thread.
Then "increment_count" + "decrement_count" != "COMBINED_TOTAL".
When running in qemu, we can see  "increment_count" + "decrement_count" = 41.

It is a kind of TOCTOU.

To solve this problem, we can add a new variable sum = increment_count + decrement_count, and use sum to do comparison.

Parents Reply
  • Hi again,

    I discussed it with the author and it looks to be as you described, yes. Printing the values by fetching values of increment and decrement count outside mutex lock will still have a race condition as you said. Getting those values added into sum inside mutex will avoid that problem, which you also said.

    I will discuss it with the author if it should be updated.

    Again thank you for reporting it

    Kind regards,
    Andreas

Children
Related