Welcome, Guest
You have to register before you can post on our site.

Username
  

Password
  





  Rule 8.5 - #define with function code in header files
Posted by: reito - 18-08-2015, 02:47 PM - Forum: 6.8 Declarations and Definitions - Replies (1)

Hello,
I would like to confirm the following interpretation of the rule 8.5. The rule says that "Header files should be used to declare objects, functions, typedefs, and macros. Header files shall not contain or produce definitions of objects or functions (or fragment of functions or objects) that occupy storage."

I have seen a couple of alerts in code raised when the header file contains a macro that generates the "body of one function". My understanding is that this is the case where the "macro produces definitions of objects or functions", and that this is a violation to be corrected.

In a similar way, if the header file contains a #typedef for an struct, should it be considered a violation to this rule?

Thanks in advance.

Print this item

  Rule 8.10, I don't understand the Rationale
Posted by: fpeelo - 05-08-2015, 03:45 PM - Forum: 8.8 Declarations and defnitions - Replies (6)

The Rationale for rule 8.10 says that inline functions with external linkage are not to be used, because it is not defined whether the function will in fact be inlined, or the external definition will be called.

However, this is also not defined for static functions: "Making a function an inline function suggests that calls to the function be as fast as possible. The extent to which such suggestions are effective is implementation-defined." (I'm using a draft of the C99 standard, http://www.open-std.org/JTC1/sc22/wg14/w.../n1256.pdf and this is 6.7.4.5. Footnote 121 spells it out more clearly: "For example, an implementation might never perform inline substitution...".)

So the behaviour of static inline functions is undefined in the same way as inline functions with external linkage.

It has been suggested that inline functions defined in header files should be defined with static linkage. Paragraph 6.7.4.3 of the above pdf says external linkage cannot reference indentifiers with internal linkage, but paragraph 6.7.4.6 says that any function with internal linkage can be inlined. So if an inline function is declared static, it can have local static variables. But there will be a different instance of the inline function for each .c file which includes the header with the function's definition, and each would have its own static variables! Effectively, you have a function definition with a static variable, the value of which differs depending on where it is called from. This will surely be unexpected behaviour to some programmers, and is therefore dangerous.

Was this considered when specifying that all inline functions should be static? Or is the draft wildly different from the final C99 spec?

Print this item

  Unclear why this code is not compliant with rule 11.3
Posted by: m4l490n - 04-08-2015, 09:00 PM - Forum: 8.11 Pointer type conversions - Replies (2)

Hi everybody

I'm having a very hard time understanding why the following code gives me two errors regarding rule 11.3.

Code:
#define NUM_TASKS 3

typedef struct
{
  uint32_t *CpuUtilizationPercent;
  uint32_t *Counter;
  TaskParam_t *Params;
  TaskConfig_t *Tasks;
  uint8_t NumTasks;
} PPTS_Config_t;

static uint32_t PPTS_CpuUtilizationPercent;
static uint32_t PPTS_Counter;

static TaskParam_t TaskParams[NUM_TASKS];

static TaskConfig_t TaskConfig[NUM_TASKS];

const PPTS_Config_t CnfPPTS =
{
  &PPTS_CpuUtilizationPercent,
  &PPTS_Counter,
  TaskParams,  /* MISRA-C rule 11.3 violation */
  TaskConfig,   /* MISRA-C rule 11.3 violation */
  NUM_TASKS
};
And in both cases the error is: A cast shall not be performed between a pointer to object type and a pointer to a different object type

The thing here is that both TaskParams and TaskConfig are pointers of the same type that are the struct elements Params and Tasks. I don't understand how ---TaskConfig_t TaskConfig[NUM_TASKS]--- is different from ---TaskConfig_t *Tasks---. They seem similar to me, or, am I messign something?

Thank you very much for helping and I hope this could be explained because I'm really frustrated.

Regards.

Print this item

  Does constness count as a type difference in rule 11.2?
Posted by: fpeelo - 23-07-2015, 03:55 PM - Forum: 8.11 Pointer type conversions - Replies (2)

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?

Print this item

  Any upcoming releases for MISRA C++ ?
Posted by: daniel tuchscherer - 21-07-2015, 09:53 AM - Forum: C++ General - Replies (5)

Hi,

As far as I know the latest guideline for C++ is MISRA C++ 2008. According to the current Cpp conferences and its talks about modern C++, the increasing use of embedded linux and during some research C++11 / C++14 both provide many techniques that could be useful for embedded programming of safety critical systems. Please correct me if I'm wrong. To name a few features there are std::array, range-loops, constexpr, static_assert, enum class.

Are there any upcoming MISRA C++ guidelines planned in the near future covering those (C++11, …) standards ?

Thanks Daniel

Print this item

  10.1 violated
Posted by: minnu - 08-07-2015, 05:34 AM - Forum: 6.6 Types - Replies (1)

#define NUM 9u
#define NUM2 8


/* gus32cmd is keyboard entry .numbers from 1 to 10*/

scanf("%d", &gus32cmd );

Switch(gus32cmd)
{
case:NUM
....
....
break;


case NUM2
..
..
break;

}

At lines case:NUM and case NUM2 I got following misra c error

"Rule 10.1 violated "implicit conversion of complex integer expression to a small sized integer is not allowed"
What correction I should do to avoid this error

Print this item

  14.3 and do-while within logical-and operator
Posted by: lovewar - 06-07-2015, 05:58 AM - Forum: 8.14 Control statement expressions - Replies (4)

Would the following example be compliant with rule #14.3?

Code:
do {
     // code..
} while ( (s8a < 10) && (s8a > 20) );  /* is && operator an essentially boolean controlling expression ? */

Print this item

  8.3 and parameter name omitted.
Posted by: lovewar - 01-07-2015, 07:29 AM - Forum: 8.8 Declarations and defnitions - Replies (2)

Would the following examples be compliant with rule #8.3?

Code:
-- in t1.h
extern int foo(int);

-- t2.c
#include "t1.h"

int foo(int value) {  // here(parameter name do not match)
}

Print this item

  Rule 8.6: External declaration without definition
Posted by: Polyspacer - 16-06-2015, 12:10 PM - Forum: 8.8 Declarations and defnitions - Replies (1)

I have a header file MISRA_8.6.h, in which a variable is declared:

Code:
extern signed short int Variable;

And I have the corresponding c-file MISRA_8.6.c, which uses the variable as input.
Code:
#include "MISRA_8.6.h"

signed short int A;

void main(void)
{

    /*8.6*/
    A = Variable;
    
}

The c-file defining the variable is out of scope. Is rule 8.6 violated by this setting or is another rule 8.x violated?

Print this item

  Rule 5.4: Macro identifier
Posted by: Polyspacer - 16-06-2015, 12:01 PM - Forum: 8.5 Identifers - Replies (1)

I have a question regarding rule 5.4: Which of the following #defines is violating this rule?

Code:
#define  engine_exhaust_gas_temperature_high_raw     egt_r
#define  engine_exhaust_gas_temperature_high_scaled  egt_s

#define x 0
#define x 0

#ifndef ABCDE
#define DAI_AGK_SEC_1BIT_RAM_START
#endif

#ifndef FGHIJ
#define DAI_AGK_SEC_1BIT_RAM_START
#endif

unsigned long int A;
unsigned short int Con = 0;

void main(void)
{

    /*5.4*/
    #ifdef DAI_AGK_SEC_1BIT_RAM_START
    A = Con;
    #endif
    A = x;
    
}

The 1st and 2nd #define are similar to the example given in the MISRA-C guideline. Hence, the 2nd #define violates the rule.
The 3rd and 4th #define are identical. Therefore, the 4th #define violates the rule.
The 5th and 6th #define are identical as well. Does the 6th #define violate the rule? According to the glossary, the macro name is a macro identifier...

Print this item

Search Forums

(Advanced Search)

Forum Statistics
» Members: 6,309
» Latest member: 1fuhd
» Forum threads: 1,066
» Forum posts: 2,933

Full Statistics

Online Users
There are currently 118 online users.
» 0 Member(s) | 114 Guest(s)
Bing, Facebook, Google, UptimeRobot

Latest Threads
13.2 Was TC2 correctly ap...
Forum: 8.13 Side effects
Last Post: misra-c
08-10-2026, 12:47 PM
» Replies: 1
» Views: 625
20.12 Does the stringific...
Forum: 8.20 Preprocessing directives
Last Post: misra-c
08-10-2026, 12:40 PM
» Replies: 1
» Views: 554
13.3 Should side effects ...
Forum: 8.13 Side effects
Last Post: misra-c
08-10-2026, 12:37 PM
» Replies: 1
» Views: 428
11.6 pointer to pointer t...
Forum: 8.11 Pointer type conversions
Last Post: misra-c
08-10-2026, 12:21 PM
» Replies: 2
» Views: 1,032
What is the UTLR of -1?
Forum: 8.10 The essential type model
Last Post: misra-c
08-10-2026, 12:15 PM
» Replies: 1
» Views: 598
10.3 Clarify if narrower ...
Forum: 8.10 The essential type model
Last Post: misra-c
08-10-2026, 11:57 AM
» Replies: 1
» Views: 508
It 8.12 intended to be so...
Forum: 8.8 Declarations and defnitions
Last Post: misra-c
08-10-2026, 11:50 AM
» Replies: 1
» Views: 455
Rule 0.1.1 Example f4
Forum: 4.0 Language independent issues
Last Post: cgpzs
07-10-2026, 11:07 AM
» Replies: 3
» Views: 728
Rule 6-2-2 Usage of std::...
Forum: 6.6 Statements (C++)
Last Post: misra cpp
25-09-2026, 05:43 PM
» Replies: 1
» Views: 437
M5-0-4 switch over unscop...
Forum: 6.5 Expressions (C++)
Last Post: misra cpp
25-09-2026, 01:00 PM
» Replies: 3
» Views: 539