Simvik

Fix: turning the LED off reboots the device

A missing break makes one command fall through into the next. An easy embedded C problem (Static analysis and MISRA in practice), run and graded in the browser.

easy · Static analysis and MISRA in practice · find-the-bug · static-analysis · misra · switch

Bug report. Sending the LED-off command over the console turns the LED off and then reboots the device. The compiler's -Wimplicit-fallthrough warning and MISRA C Rule 16.3 ("an unconditional break shall terminate every switch-clause") both point at the cause; the warnings were disabled for this file.

Fix handle by changing one line.

Starter code

void handle(cmd_t c) {
  switch (c) {
  case CMD_LED_ON:  led(true); break;
  case CMD_LED_OFF: led(false);
  case CMD_REBOOT:  request_reboot(); break;
  case CMD_STATUS:  send_status(); break;
  default: break;
  }
}

Example tests

LED off

handle(CMD_LED_OFF);

Expected output:

led off

Learn it first

More Static analysis and MISRA in practice problems