11.3: How many 'levels' deep?

Moderators: misra-c, david ward

Post Reply
gs
Posts: 87
Joined: Thu Nov 18, 2004 1:39 am

11.3: How many 'levels' deep?

Post by gs » Fri Oct 04, 2013 8:50 pm

While I am not entirely sure from the wording in the document, does this rule only concern itself with comparisons "one level deep"? For example, casting "cv1 type *" to "cv2 type *" is prohibited but "cv1a type * cv1b *" to "cv2a type * cv1b *" is allowed? Would this mean casting "cv1a type * cv1b *" to "cv2a type * cv2b *" is then prohibitted? [Presume each 'cv#<letter>' combination is unique.] While the text of the last two examples suggests the answer is "Yes", I'd like to double check.

Steve Montgomery
Posts: 104
Joined: Fri Sep 17, 2004 1:31 pm
Company: .
Location: Northumberland or Isle of Skye

Re: 11.3: How many 'levels' deep?

Post by Steve Montgomery » Mon Oct 14, 2013 9:14 am

I'm not sure I've understood the "cv" notation that you've used so I'll use a more verbose description in my reply.

For what it's worth, my interpretation of Rule 11.3 is that it's permitted to convert "pointer to Q1 T" into "pointer to Q2 T" where T is some type and Q1 and Q2 are sets, possibly empty, of type qualifiers. T must be the same type in both cases but Q1 and Q2 may be different. This means that it is permitted to "cast away" a const or volatile qualifier according to Rule 11.3. Protection from the undefined behaviour that might arise in such cases it provided by Rule 11.8 instead.

Rule 11.3 does not permit conversion of "pointer to Q1 T1" into "pointer to Q2 T2" where T1 and T2 are different types, regardless of whether Q1 and Q2 are the same sets of qualifiers. So if T1 is "pointer to const int" and T2 is "pointer to int" then the conversion is prohibited by 11.3.

Therefore I think the answer to the question "How many 'levels' deep?" is "one" though I guess it depends on where you start counting.

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

Re: 11.3: How many 'levels' deep?

Post by misra-c » Fri Dec 13, 2013 9:06 am

The short answer is "one".

A cast from "cv1 Type1 *" to "cv2 Type2 *" will only be compliant with this rule if Type1 and Type2 are the same, including their cv-qualifiers. However the cv-qualifiers ( cv1 and cv2 in your example ) at the top level may differ.

[Note: exceptions apply for casts between pointer to objects and pointers to char, signed char or unsigned char.]
---
Posted by and on behalf of
the MISRA C Working Group

thohae
Posts: 2
Joined: Fri Aug 09, 2013 12:26 pm
Company: ITK Engineering AG

Re: 11.3: How many 'levels' deep?

Post by thohae » Mon Nov 10, 2014 3:34 pm

Two follow-up questions:
(1) Where does rule 11.3 state or imply that the casts
including their cv-qualifiers
must match? Isn't the rule about correct alignment and removing qualifiers is prohibited by rule 11.8 while adding qualifiers is allowed (according to example 2 of rule 11.3)?
(2) Could you please add a example for
However the cv-qualifiers ( cv1 and cv2 in your example ) at the top level may differ.
?

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

Re: 11.3: How many 'levels' deep?

Post by misra-c » Fri Dec 05, 2014 9:11 am

(1) The Amplification states
"This rule applies to the unqualified types that are pointed to by pointers".

As explained in the previous answer to this question, only the qualifications at the top level of the type are ignored and therefore any lower qualifiers must be considered.
Consider a variation of the example in the document:

Code: Select all

     volatile int * const * pcpvi;     // pcpvi is a "pointer" to a const qualified object of type "pointer to volatile int".

The qualified type pointed to by pcpvi is "const pointer to volatile int"
The unqualified type pointed to by pcpvi is "pointer to volatile int"

Rule 11.3 is only concerned with the unqualified type pointed to by the pointer.
Rule 11.8 is only concerned with the removal of the top-level qualifier.

(volatile int * *) pcpvi would violate rule 11.8 since const removed.

(2) The p and q example in the document shows an example of different top level cv.

Code: Select all

       const short *p;               // cv is const
       const volatile short *q;      // cv is const volatile
       // The unqualified object type pointed to by both p and q is "short".
       
       q = ( const volatile short * ) p;  // compliant since both types point to the unqualified type of short.
---
Posted by and on behalf of
the MISRA C Working Group

nishiyama
Posts: 5
Joined: Tue Mar 13, 2018 7:58 am
Company: Denso Create

Re: 11.3: How many 'levels' deep?

Post by nishiyama » Tue Apr 24, 2018 9:46 am

Question for qualified type and unqualified type.

(1)What is unqualified type and qualified type in the following code?
(2)Is this code compliant with rules?

Code: Select all

typedef const struct a{
  int32_t av = 0;
}A;

typedef const struct b{
  int32_t bv = 0;
  int32_t bv2 = 0;
}B;

void func(A *a)
{
・・・
}

int32_t main(){
 A aa;
 B bb; 
 func((*A)&bb);
}
According to ISO/IEC 9899:2011 chapter 6.2.5(type),each unqualified type has a qualified type
corresponding to a combination of qualifiers such as const.
Therefore, it can be interpreted that const qualified unqualified type becomes qualified type.

In this code, the structure is const qualified, so it may be a comparison between qualified types.
If that is correct, this is the compliance code.

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

Re: 11.3: How many 'levels' deep?

Post by misra-c » Fri May 04, 2018 12:40 pm

There is a cast from
&bb: pointer to a const qualified object of type "struct b"
to
A*: pointer to a const qualified object of type "struct a"

The two pointers do not point to the same type as one points to "const struct b" and the other to "const struct a". Therefore it is a violation of rule 11.3.
---
Posted by and on behalf of
the MISRA C Working Group

Post Reply

Return to “8.11 Pointer type conversions”