Page 1 of 1

chDbgAssert

Posted: Wed Nov 30, 2016 10:46 am
by thor.thau
chDbgAssert is the ChibiOS implementation alternative to using assert but the implementation still have one of the same problems.

I got this recommendation from a embedded newsletter:
The assert macro disappears when the debug switch isn't defined. But assert can take any argument, including assignment statements and function calls. It's possible that the code will run differently depending on whether the debug switch is defined or not.

Instead, define and use a slightly different version of assert:

#define assert_e(a) ((void)(a), assert(a))


I have done something like this in my own code:

Code: Select all

#if defined(NASSERT)
#define ASSERT(test)        ((void)(test))
#else
#define ASSERT(test)        ((test) ? ((void)0) : OnFail(__FILE__,__LINE__))
#endif

Re: chDbgAssert

Posted: Wed Nov 30, 2016 1:38 pm
by Giovanni
Hi,

It is written exactly for this:

1) Assertions code must be compiled and checked even with assertions disabled.
2) There are places where functions are called in assertions that should not be called in final code. That would leave dead (or harmful) code in place.

The choice is between:
A) Hunt for possible dead/harmful code by allowing persistent function calls and/or assignments.
B) Hunt for non-pure expressions in assertions by removing the whole expression code.

I prefer B, assignments and calls to non-pure functions in assertions is misleading.

Note that it is also possible to override the assert implementation without changing the code if you have different requirements.

Giovanni

Re: chDbgAssert

Posted: Wed Nov 30, 2016 2:22 pm
by thor.thau
Hi Giovanni,

Thanks for the answer. I am more and more impressed by ChibiOS and the thoughts behind the different implementations.