Simvik

Fix: settings that say they saved, but did not

A settings save ignores the return values of the flash erase and write. An easy embedded C problem (Static analysis and MISRA in practice), run and graded in…

easy · Static analysis and MISRA in practice · find-the-bug · static-analysis · misra · error-handling · flash

Analyser findings (suppressed). "Rule 17.7: the value returned by a function having non-void return type shall be used", on both flash calls. The suppression said "flash writes do not fail".

In the field, flash erases fail when the supply voltage is low, and the device then reported "saved" while keeping the old settings, or none at all. Fix config_save by changing two lines, so it returns false as soon as the erase or the write fails. Do not write after a failed erase.

Starter code

bool config_save(const config_t *c) {
  flash_erase(CFG_ADDR);
  flash_write(CFG_ADDR, c, sizeof *c);
  return true;
}

Example tests

A normal save, then a failed write

save(0, 0);
save(0, 1);

Expected output:

saved (1 write)
save failed (1 write)

Learn it first

More Static analysis and MISRA in practice problems