Simvik

Fix: the modem is not reset when it should be

A counter inside the right side of && only counts some of the time. A medium embedded C problem (Static analysis and MISRA in practice), run and graded in…

medium · Static analysis and MISRA in practice · find-the-bug · static-analysis · misra · short-circuit

Bug report. A connected sensor polls its cellular link. Requirement: after DOWN_LIMIT consecutive polls with the link down, reset the modem, but only when the modem reports it is ready to accept a reset. In the field, when the link drops while the modem is still busy, the reset comes much later than three polls.

The analyser reported MISRA C Rule 13.5 ("the right operand of && shall not contain persistent side effects") on this line, and it was suppressed. Fix link_poll, changing at most two lines, so that every down poll is counted.

Starter code

void link_poll(link_t *l, bool up, bool modem_ready) {
  if (up) {
    l->down_polls = 0;
    return;
  }
  if (modem_ready && ++l->down_polls >= DOWN_LIMIT) {
    modem_reset();
    l->resets++;
    l->down_polls = 0;
  }
}

Example tests

Down while the modem is busy, then it becomes ready

run("nnnd");

Expected output:

reset resets=1 down=0

Learn it first

More Static analysis and MISRA in practice problems