chDbgAssert

This forum is dedicated to feedback, discussions about ongoing or future developments, ideas and suggestions regarding the ChibiOS projects are welcome. This forum is NOT for support.
Post Reply
thor.thau
Posts: 17
Joined: Thu Jul 07, 2016 12:37 pm
Has thanked: 2 times
Been thanked: 7 times

chDbgAssert

Post 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
User avatar
Giovanni
Site Admin
Posts: 14892
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: chDbgAssert

Post 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
thor.thau
Posts: 17
Joined: Thu Jul 07, 2016 12:37 pm
Has thanked: 2 times
Been thanked: 7 times

Re: chDbgAssert

Post by thor.thau »

Hi Giovanni,

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