Skip to content

boot: first MISRA C:2012 fixes from cppcheck - #2995

Draft
JayToltTech wants to merge 4 commits into
mcu-tools:mainfrom
Tolt-Technologies:style/misra-cppcheck-findings
Draft

JayToltTech wants to merge 4 commits into
mcu-tools:mainfrom
Tolt-Technologies:style/misra-cppcheck-findings

Conversation

@JayToltTech

@JayToltTech JayToltTech commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

Draft for discussion in #2994, not for merge yet.

First four MISRA C:2012 fixes from our fork, rebased on main. No functional change. Kept deliberately small, since the last attempt at this (#1639) was one big sweep and went stale.

  • bootutil: image: U suffix on the image magic constants (7.2, 10.4).
  • boot: zephyr: serial_adapter: binds the CDC ACM poll timeout to a const k_timeout_t. cppcheck can't resolve K_MSEC()'s .ticks designator inside a loop condition, so the addon reported a misra-config error for the file, its way of saying the results there may be incomplete.
  • boot: zephyr: usbd_cdc_serial: descriptors move to the functions that use them (8.9), the configuration attributes are picked by the preprocessor instead of a mixed-type conditional (10.4), and error checks compare against 0 (14.4). The USBD_*_DEFINE() macros expand to statics, so storage duration doesn't change.
  • boot: boot_serial: buf and encoded_buf in boot_serial_output() are sized from one constant built from type sizes. cppcheck can't evaluate sizeof(buf) when buf's own size uses sizeof(*bs_hdr), which gave the second misra-config error. Sizes are unchanged.

cppcheck 2.22 with the MISRA addon, nrf52840dk, usb_cdc_acm_log_recovery.conf plus CONFIG_LOG_ALWAYS_RUNTIME=y (see the issue for why), everything under boot/: 798 findings on main (f30be48) and 771 with this, and the two misra-config errors are gone. By rule: 7.2 12 -> 3, 8.9 24 -> 18, 10.3 9 -> 8, 10.4 214 -> 213, 14.4 64 -> 54.

Builds for nrf52840dk with usb_cdc_acm_recovery.conf, usb_cdc_acm_log_recovery.conf and the default config. Sim passes. Not run on hardware.

None of our inline cppcheck-suppress markers are in here, see question 3 in the issue.

IMAGE_MAGIC, IMAGE_MAGIC_V1 and IMAGE_MAGIC_NONE don't fit in an int,
so C already gives them an unsigned type, but that is easy to miss and
static analysers report every comparison against the uint32_t
ih_magic field as mixing signed and unsigned operands (MISRA C:2012
rules 7.2 and 10.4).

Add the U suffix so the type is explicit. The values, and the type the
compiler picks for them, are unchanged.

Signed-off-by: Jay Beavers <jay@tolttechnologies.com>
The CDC ACM wait passes K_MSEC(BOOT_CDC_ACM_POLL_MS) straight into the
loop condition. K_MSEC() expands to a k_timeout_t compound literal
with a .ticks designator, and cppcheck can't resolve that designator
inside a loop condition: its MISRA addon stops with "misra-config:
Variable 'ticks' is unknown" and skips the rest of the file, so a
clean report for serial_adapter.c doesn't mean the file was checked.

Bind the timeout to a const k_timeout_t before the loop. The wait is
unchanged.

Signed-off-by: Jay Beavers <jay@tolttechnologies.com>
Static analysis of the CDC ACM setup reports findings that come from
how the file is laid out rather than from what it does:

- The string, configuration and configuration-name descriptors are file
  scope but each is used by one function (MISRA C:2012 rule 8.9). The
  USBD_*_DEFINE() macros expand to static definitions, so they move
  into the functions that use them with their storage duration, and the
  pointers the USB stack keeps to them, unchanged.
- boot_usbd_attributes is a uint8_t chosen by a conditional whose
  operands have different essential types (rule 10.4), and the
  self-powered flag is recovered from it with a mask. Both values are
  now selected by the preprocessor, and usbd_self_powered() gets a bool.
- Error checks test int return values as conditions (rule 14.4). They
  now compare against 0.

No functional change.

Signed-off-by: Jay Beavers <jay@tolttechnologies.com>
encoded_buf was sized from sizeof(buf), and buf from an expression
using sizeof(*bs_hdr) and sizeof(crc). cppcheck can't evaluate
sizeof() of an array whose dimension uses sizeof() of a variable or
struct, so its MISRA addon reported boot_serial_output() with a
misra-config error ("Unknown array size"), meaning its results for
the file may be incomplete.

Give both arrays one constant built from the type sizes instead. The
sizes are unchanged: struct nmgr_hdr for *bs_hdr, and uint16_t for
crc and totlen.

Signed-off-by: Jay Beavers <jay@tolttechnologies.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant