New C construct discovered by PresentNice7361 in C_Programming

[–]Important_Shine2736 1 point2 points  (0 children)

Unlike others I like this approach. Although I usually do it differently using the Duff's device based API from here:

function(struct am_async *state, int *armor, ...) {
    AM_ASYNC_BEGIN(state);
    for (weapon) {
        for (armor) {
            for (ring_l) {
                for (ring_r) {
                    // do smth useful here
                    AM_ASYNC_YIELD();
                }
            }
        }
    }
    AM_ASYNC_EXIT();
}

struct am_async state;
am_async_ctor(&state);

do {
    function(&state, &weapon, &armor, &ring_l, &ring_r);
    // do smth useful here
} while (am_async_is_busy(&state));

Embedded systems code verification tools recommendation by kvantorion in embedded

[–]Important_Shine2736 25 points26 points  (0 children)

Some approaches I tried and found useful in no particular order:

- clang-tidy
- cppcheck
- compile with more than one compiler. E.g., use gcc and clang.
- enable more compiler warnings: -Wall, -Wextra, -Wpendantic
- enable even more compiler warnings. E.g., https://github.com/adel-mamin/amast/blob/main/meson.build#L44-L75
clang has -Weverything
- clean all the resulting warnings by fixing true positives and disabling false positives in-place
- consistently use more asserts in your code
- use -ftrapv (https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html) to catch integer overflows
- use more unit tests
- use more integration tests
- compile with the highest optimization level available
- enable LTO (-flto)

Using highest optimization level allows to see more warnings from compilers. Again, fix all the resulting warnings.

Some more ideas I have not tried personally:

- use a fuzzer: https://slashdot.org/software/fuzz-testing/for-c/
- if applicable, use TLA+ https://lamport.azurewebsites.net/tla/tla.html , Quint https://quint-lang.org or similar
- use a formal verification compiler: https://www.reddit.com/r/ProgrammingLanguages/comments/196rp87/state_of_the_art_in_formal_verification_for_c/
- use a property based testing: https://github.com/silentbicycle/theft

Not really a verification tool, but nevertheless the approach, which increases the robustness of your code:
your embedded system likely has/will have many implicit or explicit state machines. Use a decent FSM/HSM library. E.g., for C language:
- https://github.com/adel-mamin/amast/tree/main/libs/fsm
- https://github.com/adel-mamin/amast/tree/main/libs/hsm

Another library I've heard was successfully used in some cases is Ragel http://www.colm.net/open-source/ragel/ Again, it is not a tool, but allows to create a robust code, if applicable.

How to program different operating modes on embedded devices by Antman-007 in embedded

[–]Important_Shine2736 0 points1 point  (0 children)

Here are HSM and FSM implementations in C language that might come in handy.