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

chDbgAssert

Postby thor.thau » Wed Nov 30, 2016 10:46 am

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
Thor Thau
Embedded software developer

User avatar
Giovanni
Site Admin
Posts: 14455
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1076 times
Been thanked: 922 times
Contact:

Re: chDbgAssert

Postby Giovanni » Wed Nov 30, 2016 1:38 pm

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

Postby thor.thau » Wed Nov 30, 2016 2:22 pm

Hi Giovanni,

Thanks for the answer. I am more and more impressed by ChibiOS and the thoughts behind the different implementations.
Thor Thau
Embedded software developer


Return to “Development and Feedback”

Who is online

Users browsing this forum: No registered users and 18 guests