Home > On-Demand Archives > Q&A Sessions >

Live Q&A - MISRA C: A Focus on Writing Clear, Maintainable Code

Colin Walls - Watch Now - EOC 2024 - Duration: 26:41

Live Q&A - MISRA C: A Focus on Writing Clear, Maintainable Code
Colin Walls
Live Q&A with Colin Walls for the talk titled MISRA C: A Focus on Writing Clear, Maintainable Code
M↓ MARKDOWN HELP
italicssurround text with
*asterisks*
boldsurround text with
**two asterisks**
hyperlink
[hyperlink](https://example.com)
or just a bare URL
code
surround text with
`backticks`
strikethroughsurround text with
~~two tilde characters~~
quote
prefix with
>

nathancharlesjones
Score: 1 | 7 months ago | 1 reply

@MaxP, that's a great tip about const, to go along with the advice about static!
Another one I heard (related to Rule 16.4 about having a default statement inside every switch statement) is to always include an else statement with any if statement. My thoughts on that are mixed, but I feel like if you buy the MISRA rule, then that also must follow by the same logic.

ColinWallsSpeaker
Score: 0 | 7 months ago | no reply

I have never heard of a "compulsory else" rule, but it does make some sense.

SimonSmith
Score: 1 | 7 months ago | 2 replies

Re: question about unions in Q&A. We use this pattern below a lot with struct inside union, where we can set all bits easily via "value = 0u":

typedef union
{
uint32_t value;
struct
{
uint32_t bitX : 1;
uint32_t bitY : 1;
// etc
}
} myTypeName_t;

Another example with union inside struct where we can access the data either as bits in a structure, or as a byte array when transmitting/receiving it without needing any conversion functions:-

typedef struct
{
union
{
type_t value;
char bytes[sizeof(type_t)];
}
uint32_t crc;
}
} myTypeName_t;

Sorry about the formatting, can't get Markdown to work!

ColinWallsSpeaker
Score: 0 | 7 months ago | 2 replies

I understand the attraction of using unions in this way, but it has 2 implications. First, it is compiler dependent; the C language standard does not dictate how a structure is laid out in memory, so mapping onto another data type is "dangerous". Of course, you can do this if you carefully document the dependency. Also, it hides the fact that there is a read/OR/write process, which I would argue is making the code slightly less readable/clear.

yen
Score: 0 | 7 months ago | 2 replies

Hey Colin,
So even if the union members all have the same size individually, writing one member and reading back the other can be problematic because it is up to the compiler to decide the layout?
typedef union
{
uint32_t val;
uint8_t byteArray[4];
//byteArray[0] might be any byte of val, all dependent on the compiler implementation?
struct bitField32
{
uint32_t bit0 : 1;
//...
uint32_t bit31 : 1;
}bits;
//similarly, bits.bit0 might be any bit within val
};

Andre.Hartmann
Score: 2 | 7 months ago | 1 reply

What you need to be aware of, is that a union containing uint32_t and uint8_t [4] is not portable between big- and little endian platforms. byteArray[0] might not be any byte ofval`, but should either be the highest or lowest byte.

Despite that, I have used unions with uint32_t and the corresponding bitfields many times and at least with my compilers it always worked (e.g. using the same code on PC and microcontroller for data exchange). Your mileage may vary.

yen
Score: 0 | 7 months ago | no reply

Thanks for the info! Yeah I understand depending on endianness byteArray[0] could be MSB or LSB. I was also thinking since it is up to the compiler they can potentially do something unconventional (especially the non-standard ones), like making byteArray[0] be byte 1 or 2 of val.

ColinWallsSpeaker
Score: 1 | 7 months ago | no reply

In a word: yes.

SimonSmith
Score: 0 | 7 months ago | 1 reply

Yes, we know it may not be the most portable implementation, but it works well and we don't intend to change the toolchain.

ColinWallsSpeaker
Score: 1 | 7 months ago | no reply

That is absolutely fine, so long as it is fully documented.

Robert Hancock
Score: 0 | 7 months ago | no reply

One thing to be aware with that type of union usage is that although C allows it as implementation-defined behavior, in C++ it is explicitly undefined behavior to access a union other than through the data member most recently written to.

yen
Score: 0 | 7 months ago | 1 reply

Thanks again for the feedback Colin! One way I have seen unions being used would be as an event parameter for communication among tasks/threads.
Depending on the event type, different members of the event parameters are accessed (sorry about the formatting):
struct eventDetails
{
event_enum eventType;
union eventParams
{
uint32_t paramForEventA;
uint8_t paramForEventB;
//other params
};
};
Would love to know your thoughts on this.

ColinWallsSpeaker
Score: 0 | 7 months ago | 1 reply

As I have commented in another response here, structure [and union] layout is compiler-dependent and that can lead to issues down the line. Even if the code works now, a change of compiler in future might cause serious problems.

yen
Score: 0 | 7 months ago | 1 reply

Hey Colin,
Can you elaborate on this please? I thought even though the layout is compiler-dependent, as long as the access is fine (write/read to same member), changing the compilers shouldn't cause problems right?

Using the above code snippet as example, say task 1 sends the event detail to task 2. If the eventType is EventA, task 1 sets the value needed for paramForEventA (i.e., details.params.paramForEventA = someValue). When task 2 gets notified of the event, it first checks the eventType and knows to access paramForEventA (i.e., receivedVal = details.param.paramForEventA). Would implementations like this be problematic if a change of compiler is needed?

Thanks again for your time!

ColinWallsSpeaker
Score: 1 | 7 months ago | 1 reply

I think you would get away with it in this example, as [if I understand it correctly] there is a union of a 1-byte data item with a 4-byte one and it doesn't matter which byte of the 4-byte one is shared.

yen
Score: 0 | 7 months ago | no reply

Ah I think I understand the comment about compiler-dependent better. Thanks for clarifying! I do have a follow-up though, but will reply under the corresponding thread.

Insta_Jun
Score: 0 | 7 months ago | 1 reply

Hello Collin, what is a good way to learn MISRA standard so that we can implement this guideline in our daily coding style? Is that suggestible for you that we just look through the whole directives and rules of the MISRA? Or do you have any other suggestions from your experience? My personal learning practice of the MISRA or other guidelines like CERT C is read one or two rules every day.

ColinWallsSpeaker
Score: 0 | 7 months ago | no reply

I think that the approach you suggest makes sense. Running code through a MISRA checker and studying the failures is another way of gaining insight. I am sure that someone, somewhere offers training.

Nathan3
Score: 1 | 7 months ago | 1 reply

Following and the Q&A question about unions: how would you manage a message structure that can contain different types of data (some simple types, other being structures) ?
I tend to use a struct with a type enum and a data union. I am interesting to see how this topic can be tackled without union.

ColinWallsSpeaker
Score: 0 | 7 months ago | no reply

I cannot give a definitive answer without seeing the code/data in more detail. However, broadly speaking, if you have a bunch of bits, which arrives most likely as a sequence of 32-bit words, and you need to extract field from them, it is far better to code the "bit bashing" yourself than rely on structure/union layouts that are compiler dependent.

DrK
Score: 0 | 7 months ago | 1 reply

rule 1.2 : Is there few common examples that illustrate this rule?

ColinWallsSpeaker
Score: 0 | 7 months ago | no reply

A number of compilers offer extensions as additional keywords. Examples include interrupt, packed and unpacked.

Andre.Hartmann
Score: 0 | 7 months ago | 1 reply

Thanks Colin, that was (again) a great talk!
I have one addition regarding rule 5.3: that is not only about global and local variables (which could be distinguished by naming schemes), but also local variables within different scopes:

void bar() {
    int foo = 0;
    if (foo) {
        int foo = 1;
    }
}

I even think I saw cppcheck complaining about such shadowing, but I was not able to reproduce that now.

ColinWallsSpeaker
Score: 0 | 7 months ago | no reply

That is correct. The rule actually refers to "inner scope" and "outer scope", which I guess can mean global and local, but can also refer to nested blocks.

JGrunklee
Score: 1 | 7 months ago | 1 reply

Thanks Colin! Regarding Rule 17.8 ("don't modify function parameters") - it is common practice to use pointer types as "output" parameters. Do you/MISRA consider this practice maintainable? On a related note, how does MISRA C feel about side-effects in general?

ColinWallsSpeaker
Score: 0 | 7 months ago | no reply

An interesting question. For the most part, MISRA C warns against the intentional use of side-effects. However, this leaves open the question of how a function can return more than one data item. Use of pointer parameters is an obvious way to achieve this and I believe that it is MISRA compliant. Alternatives would be to return a structure or a pointer to a static data item [structure or array].

SimonSmith
Score: 0 | 7 months ago | 1 reply

Thanks Colin for a good talk.

ColinWallsSpeaker
Score: 0 | 7 months ago | no reply

Thank you for the feedback.

Carlos.Amaya
Score: 0 | 7 months ago | 1 reply

Thank you Colin for this concise and informative presentation. I really liked the examples you used to clarify the rules by MISRA. Thank you for sharing!

ColinWallsSpeaker
Score: 0 | 7 months ago | no reply

Thank you for the feedback.

MaxP
Score: 1 | 7 months ago | 1 reply

Thanks Colin, very informative overview of the MISRA C. I'd like to add that to better enforce 17.8 it is possible to declare parameters as "const":

int times2( int n );  // prototype stays the same

int times( int const n )  // n is const int
{
  n *= 2;   // <- error since n is const
  return n;
}
ColinWallsSpeaker
Score: 0 | 7 months ago | no reply

That is a very good suggestion. Thanks.

10:04:36 From Nathan to Everyone:
	Is there any alternative to MISRA that is being used in other industries ?
10:05:08 From Antonio to Everyone:
	Thanks for your talk Colin.
	
	Do you have any experiences with coworkers that are not motivated enough to follow the MISRA rules? Do you have any advice to give them a motivation booster?
10:07:38 From BobF to Everyone:
	>>>> Already answered
	One of the reasons why MISRA may not have been adopted widely is the ‘Motor Industry’ reference in the acronym. 
	<<<<
	
	These rules are very important and not just for ‘Safety + Security Critical’ applications. Has a revised name ever been considered?
10:09:02 From Nick Tillich to Everyone:
	Do you have a preferred tool for performing MISRA static analysis checks?  The tool my company uses currently is powerful but can be unintuitive to use, especially for new engineers.
10:09:07 From Thomas Schaertel to Everyone:
	I know the Imagecraft compilers had MISRA checking build in (you could just activate different rules by checkboxes). Do you recommend any (external) checker?
10:12:20 From Scott H to Everyone:
	I tend to use the Barr Embedded C code standard / style guide, which aligns with MISRA  in a few ways. 
	
	Are there any other standards or style guides you've come across that stand out as useful for code safety?
10:14:40 From Thomas Schaertel to Everyone:
	Replying to "I know the Imagecraf..."
	
	Thanks!
10:15:36 From Andreas Jarosch to Everyone:
	What potential do you see of AI as a ckecker? Have you seen it  in action already?
10:15:57 From Scott H to Everyone:
	Replying to "I tend to use the Ba..."
	
	Thanks
10:19:38 From Jui Yen to Everyone:
	Thanks for your time Colin! I have two questions about two of the MISRA C rules:
	How does one get around "A project should not contain unused macro declarations"? Specifically for header guards.
	What about the advisory of unions should not be used?
10:23:38 From BobF to Everyone:
	COMMENT Re: Assembly Programming .... Least it's more readable than microCode at the Processor Controller level !!
10:24:28 From Eric to Everyone:
	How can one make sure that aggressive optimizations of further compiler versions don't break my code? Is there some kind of MISRA compliance test for compilers?
10:25:08 From Jui Yen to Everyone:
	Replying to "Thanks for your time..."
	
	Thanks again for the feedback!
10:25:11 From SimonSmith to Everyone:
	I use unions :)
10:26:55 From Tom to Everyone:
	Replying to "I use unions :)"
	Do you have examples?
10:27:23 From Stephane to Everyone:
	Thank you Colin!
10:27:35 From Eric to Everyone:
	Thank you!
10:27:36 From René Andrés Ayoroa to Everyone:
	Thank you, Colin.
10:27:37 From Lyden Smith to Everyone:
	Thank you!
10:27:38 From Nick Tillich to Everyone:
	Thank you!
10:27:50 From Andreas Jarosch to Everyone:
	Thank you very much!!!
10:27:56 From Scott H to Everyone:
	Cheers

OUR SPONSORS

OUR PARTNERS