MISRA Rules Ambiguity

Questions and discussions about MISRA C++ not fitting under rules forums

Moderators: david ward, misra cpp

Locked
igor.gvero
Posts: 2
Joined: Tue Nov 11, 2014 7:45 pm
Company: RogueWave (Klocwork)

MISRA Rules Ambiguity

Post by igor.gvero » Tue Dec 23, 2014 8:59 pm

My name is Igor Gvero and I am a Product Manager/Owner at Klocwork. Our developers had few questions with respect to ambiguity of some rules. Questions are below.
NOTE: THESE ARE NOT SECTION 6 INQUIRIES - SO I AM POSTING THE QUESTIONS HERE

1. Concerning the Rule 5-0-15 of the MISRA C++ 2008 standard, is the following example compliant with the rule?

struct Bar
{
int32_t* dataP[2];
};

void f()
{
int32_t myarray[10];
Bar b;
b.dataP[0] = myarray;
b.dataP[1] = myarray;

int32_t c = (b.dataP[1])[3]; // Is this expression compliant with the rule 5-0-15?
}

The reason that I am asking this question is that the examples for the rule 5-0-15 seem to be contradictory about the use of pointers as arrays. For example, the following are examples from the documentation of the rule 5-0-15:

void my_fn(uint8_t * p1, uint8_t * p2[ ])
{
...
p1[ 5 ] = 0; // Non-compliant – p1 was not declared as array
...
}

uint8_t a[ 10 ];
uint8_t * p;
p = a;
p[ 5 ] = 0; // Compliant : seems to be contradictory with the previous example where it is non-compliant.

I would appreciate if you could explain the exact rule for the use of pointers as arrays in your answer.


2. Concerning the Rule 2-10-2 of the MISRA C++ 2008 standard, is operator overloading allowed for this rule? I think it should be allowed, but I would like to be sure. The rule discuss about identifiers. An operator name should not be an identifier, thus operator overloading should be allowed, but it is not clear from the rule. For example, is the following example compliant with the rule?

#include <iostream>
#include <new>

namespace myNamespace {
class myClass {
public:
static void* operator new(size_t); // Is this operator overloading compliant with rule 2-10-2?
};
}


3. Concerning the Rule 5-0-2 of the MISRA C++ 2008 standard, can one use parentheses for the operand of the sizeof operator? The rule states that “Parentheses are not required for the operand of a unary operator”. Sizeof is unary operator, thus parentheses should not be required. However, it is common practice to use parentheses with this operator (and even some MISRA examples use this style like rule 5-3-4). Here are some examples to help understand my question:

uint8_t* a = ...;

sizeof *a; // Should be compliant with the rule.
sizeof (int); // Should be compliant with the rule too since the operand is a cast.

sizeof(*a); // This is the one that I am not sure of. I think that it should be accepted but the rule seems to say otherwise.

david ward
Moderator
Moderator
Posts: 509
Joined: Wed May 05, 2004 9:05 am
Location: MIRA

Re: MISRA Rules Ambiguity

Post by david ward » Fri Jan 09, 2015 12:47 pm

Rule questions should be posted under the relevant section. Section 6 means chapter 6 of the document which contains the rules. Thus a query on rule 5-0-15 should be placed under topic "MISRA C++ > MISRA C++:2008 rules > 6.5 Expressions (C++)", a question on rule 2-10-2 under "6.2 Lexical conventions" and so on.
Dr David Ward
MISRA Project Manager

Locked

Return to “C++ General”