Repository navigation
boot: first MISRA C:2012 fixes from cppcheck - #2995
Draft
JayToltTech wants to merge 4 commits into
Draft
JayToltTech wants to merge 4 commits into
JayToltTech wants to merge 4 commits into
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 aconst k_timeout_t. cppcheck can't resolveK_MSEC()'s.ticksdesignator inside a loop condition, so the addon reported amisra-configerror 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). TheUSBD_*_DEFINE()macros expand to statics, so storage duration doesn't change.boot: boot_serial:bufandencoded_bufinboot_serial_output()are sized from one constant built from type sizes. cppcheck can't evaluatesizeof(buf)whenbuf's own size usessizeof(*bs_hdr), which gave the secondmisra-configerror. Sizes are unchanged.cppcheck 2.22 with the MISRA addon, nrf52840dk,
usb_cdc_acm_log_recovery.confplusCONFIG_LOG_ALWAYS_RUNTIME=y(see the issue for why), everything underboot/: 798 findings onmain(f30be48) and 771 with this, and the twomisra-configerrors 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.confand the default config. Sim passes. Not run on hardware.None of our inline
cppcheck-suppressmarkers are in here, see question 3 in the issue.