Does constness count as a type difference in rule 11.2?

Moderators: misra-c, david ward

Post Reply
fpeelo
Posts: 15
Joined: Fri Jul 20, 2007 3:31 pm

Does constness count as a type difference in rule 11.2?

Post by fpeelo » Thu Jul 23, 2015 4:55 pm

I have an incomplete struct type, as per Directive 4.8. I declare types for pointers to the hidden struct:

typedef struct s_EthBuf *TEthBuf;
typedef struct s_EthBuf const *CTEthBuf;

The actual type, and an instance of the type, are declared in the translation unit that also defines the functions which operate on it.

Some functions need to modify the contents of the object, so they take a parameter of type TEthBuf. Some do not; they take a parameter of type CTEthBuf.

PC-Lint tells me that assigning a TEthBuf pointer to a variable of type CTEthBuf, or const TEthBuf, or passing it to a function which takes CTEthBuf as its parameter type, violates rule 11.2.

So: if I have struct s_EthBuf X then the following get the warning the 11.2 is violated:
CTEthBuf a = X;
const TEthBuf b = X;
foo(x)
- where foo is declared as void foo(CTEthBuf buf);

But it's OK to do
TEthBuf c = X;

But the type is the same, it's just that foo() doesn't change anything in the object.

Is PC-Lint correct here, when it believes that the types are different from the point of view of rule 11.2?

fpeelo
Posts: 15
Joined: Fri Jul 20, 2007 3:31 pm

Re: Does constness count as a type difference in rule 11.2?

Post by fpeelo » Fri Jul 24, 2015 2:39 pm

fpeelo wrote: typedef struct s_EthBuf *TEthBuf;
typedef struct s_EthBuf const *CTEthBuf;
Sorry, that latter type should have been

typedef const struct s_EthBuf *CTEthBuf;

It's for rule 8.13

But whichever way around it's put, PC-Lint still recognises TEthBuf and CTEthBuf as distinct types, so a function with a parameter TEthBuf cannot pass it to a function which takes CTEthBuf, or const struct s_EthBuf *

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

Re: Does constness count as a type difference in rule 11.2?

Post by misra-c » Fri Aug 14, 2015 11:43 am

This response refers to code in the following form. It is assumed that X is a pointer as mentioned in the wording.

Code: Select all

struct s_EthBuf;
typedef struct s_EthBuf *TEthBuf;
typedef const struct s_EthBuf *CTEthBuf;

extern void foo(CTEthBuf buf);

void fn ( struct s_EthBuf *X )
{
   CTEthBuf a = X;
   const TEthBuf b = X;
   TEthBuf c = X;

   foo(X);
}
Where pointers are involved, Rule 11.2 applies to the unqualified types that are pointed to by the pointer. Therefore none of these examples violate rule 11.2.

The destination pointer must contain all the type qualifications of the source type, otherwise the result is a constraint error in C99. See section 8.11 of the MISRA-C guidelines. There is no restriction on the destination having more type qualifiers than the source.
---
Posted by and on behalf of
the MISRA C Working Group

Post Reply

Return to “8.11 Pointer type conversions”