2 questions for rule 18.1

6.18 Structures and Unions

Moderators: misra-c, david ward

Post Reply
pmhill
Posts: 4
Joined: Wed Apr 08, 2009 9:17 am
Company: University of Leeds

2 questions for rule 18.1

Post by pmhill » Wed Nov 25, 2009 9:22 am

The rule applies to structure definitions that are incomplete, but I see from exemplar suite file mc2_1801.c the following unexpected violation:

struct {
int8_t a;
int8_t b [ ]; /* Not Compliant - constraint error 6.5.2.1 */
} mc2_1801_st = { 1, { 2, 3, 4 } };

Are you including as "incomplete" any structure that has a flexible array member?

Apart from this, what other coding practice is left that might cause a problem?

If the type is incomplete, a compile error will result from any attempt to create an object of that type or refer to its members or to compute sizeof, etc.

So I cannot think of any legal C code that would be a violation of this rule.

jbezem
Posts: 20
Joined: Mon Aug 01, 2005 8:24 pm
Company: JB Enterprises
Location: Germany
Contact:

Re: 2 questions for rule 18.1

Post by jbezem » Thu Nov 26, 2009 9:09 am

Flexible array members are a feature of C99 and therefore not allowed according to the MISRA Standard (the current version of MSRA, 2004, is explicitly restricted to C89/C95). From a C89 point of view, the type is incomplete, and therefore in violation of 18.1.
If your compiler is silently (or by instruction) allowing certain/all C99 features to be used, your compiler will not consider the type itself incomplete, even if it is from the C89 viewpoint.

HTH,

Johan
Johan Bezem
Email: [email protected]
Tel: +49 172 5463210
Web: http://www.bezem.de/

misra-c
Posts: 548
Joined: Thu Jan 05, 2006 1:11 pm

Re: 2 questions for rule 18.1

Post by misra-c » Wed Jan 20, 2010 12:36 pm

The example cited was added to the exemplar suite because at least one commercial C90 compiler was found to accept flexible array members even though they are not part of the C90 language standard.

The exemplar suite for Rule 18.1 also includes an example of references to types that are incomplete which in turn lead to undefined behaviour.
---
Posted by and on behalf of
the MISRA C Working Group

Post Reply

Return to “6.18 Structures and Unions”