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

Username
  

Password
  





  Difference between 2-10-2 and MISRA C-2012 Rule 5.3
Posted by: cxlin - 19-09-2018, 06:04 AM - Forum: 6.2 Lexical conventions (C++) - Replies (1)

Hi,

MISRA C-2012 Rule 5.3 (which is of the same title as 2-10-2) has this additional amplification about an identifier declared in an inner scope shall be distinct from *any* id declared in an outer scope.

So the following code shall be a violation for 5.3 although the inner "i" is not hiding the outer "i" (still confusing though).

Code:
void f() {
    {
        int i; // Non-compliant
    }
    int i;
}

Since 2-10-2 does not have this amplification, could you kindly help to advise whether this is also a violation for 2-10-2 or not?

Print this item

  why adding #define contsants for initializing a const is breaking rule 10.3 ?
Posted by: alaini - 14-09-2018, 07:28 AM - Forum: 8.10 The essential type model - Replies (5)

I have the folowing code :

Code:
#define CONST1 556U
#define CONST2 420U

uint16_t const MY_var1 = CONST1;
uint16_t const MY_var2 = CONST2;
uint16_t const MY_varTotal = (CONST1 + CONST2);

And FlexeLint for C/C++ (Unix) Vers. 9.00L gives me he following message:


uint16_t const MY_varTotal = (CONST1 + CONST2);
Note 9034: Expression assigned to a narrower or different essential type [MISRA 2012 Rule 10.3, required]
[/code]


I do not understand the reasoning why rule 10.3 is violated.

Print this item

  Rule 11.6. A cast between pointer to void and an arithmetic type. Value of the rule.
Posted by: l.inc - 06-09-2018, 12:18 PM - Forum: 8.11 Pointer type conversions - Replies (3)

Good day. To analyze a finding supported by this rule in a project I'd like to understand the intention of the rule. Specifically, in the context of C99 I fail to find an appropriate rationale to disallow converting between intptr_t/uintptr_t and void *:

  1. Quote:Conversion of an integer into a pointer to void may result in a pointer that is not correctly aligned, resulting in undefined behaviour.
    Would you, please, explain how this rationale is compatible with the following statement in the C90/C99 standards:
    C99. 6.2.5 Types. §27 Wrote:A pointer to void shall have the same representation and alignment requirements as a pointer to a character type.
    As character pointers contain addresses of individual bytes, they cannot be unaligned. Hence pointers to void cannot be unaligned too, right?
    Moreover, for the cases, in which the alignment problem indeed may occur, the restriction you define (rule 11.4) is, in contrast to 11.6, only advisory.
  2. Quote:Conversion of a pointer to void into an integer may produce a value that cannot be represented in the chosen integer type resulting in undefined behaviour.
    This rationale does not apply to intptr_t/uintptr_t.
  3. Quote:Conversion between any non-integer arithmetic type and pointer to void is undefined.
    This rationale looks like it would deserve a separate rule, as the limitations of these conversions for integers and non-integer arithmetic types are very different.

In addition to failing to find an appropriate rationale for disallowing conversions between intptr_t/uintptr_t and void * I see positive value in favoring these over conversions between intptr_t/uintptr_t and object pointers. One advantage is the above mentioned alignment, but also that holding addresses as pointers to void prevents accidental accesses to the object. The more specific case of mine is when pointers to objects are produced by de-serialization (for which uintptr_t is the most preferred type) and the objects are used nearly nowhere in the program but in copy operations via memcpy. Improving compliance with MISRA C:2012 requires to convert the value into an object pointer first, which creates the risk of accidental dereferencing in the code, and only then into a pointer to void to pass the value to memcpy. This allows to trade a deviation from the required rule 11.6 for the deviation of the advisory rule 11.4. But at the same time the code then seems to become less safe.

So were the conversions between intptr_t/uintptr_t and void * disallowed unintentionally or do I just miss a safer way to handle the above described situations than to deviate from 11.6?

Print this item

  Dir 4.8 if struct is only visible through a function declaration
Posted by: LMende - 29-08-2018, 12:22 PM - Forum: 7.4 Code design - Replies (2)

Hi there,
I humbly ask for help with compliance to Dir 4.8.
(Borrowing from cxlin, thx)

Code:
/* def.h */
struct SomeType
{
/* Object implementation */
}

/* other.h */
extern void usetype(someType* p);

/* no-use.c */
#include def.h
#include other.h
/* Do something. But no use of SomeType at all. */
Given a structure SomeType which implementation is visible to no-use.c.
A pointer to SomeType is only visible through the included other.h where the usetype function is declared.
Is the implementation of no-use.c compliant with Dir 4.8 or not?

Print this item

  What is the method of defining the Function Prototype
Posted by: prem_annam - 23-08-2018, 04:21 AM - Forum: General Questions - Replies (6)

Hi,

I am facing the MISRA.VAR.HIDDEN error.

When i defined the function prototype with parameters and using the same parameters name while calling the function.

Can any one give me suggestion how can it will be modified to avoid the error.

Thanks in advance.

Print this item

  Proposal: Consider adding a rule for the undefined behaviour in ISO/IEC 14882:2003 3.6.2 section 3
Posted by: dg1980 - 15-08-2018, 09:16 AM - Forum: C++ General - Replies (1)

Hi,

as far as i know, there is no explicit rule to deal with that.
12-8-1 comes close but is for copy constructors only.

An implementation is permitted to perform the initialization of an object of namespace scope with static
storage duration as a static initialization even if such initialization is not required to be done statically, provided
that
— the dynamic version of the initialization does not change the value of any other object of namespace
scope with static storage duration prior to its initialization, and
— the static version of the initialization produces the same value in the initialized object as would be produced
by the dynamic initialization if all objects not required to be initialized statically were initialized
dynamically.
[Note: as a consequence, if the initialization of an object obj1 refers to an object obj2 of namespace
scope with static storage duration potentially requiring dynamic initialization and defined later in the same
translation unit, it is unspecified whether the value of obj2 used will be the value of the fully initialized
obj2 (because obj2 was statically initialized) or will be the value of obj2 merely zero-initialized. For
example,
44
ï›™ ISO/IEC ISO/IEC 14882:2003(E)
3 Basic concepts 3.6.2 Initialization of non-local objects
inline double fd() { return 1.0; }
extern double d1;
double d2 = d1; // unspecified:
// may be statically initialized to 0.0 or
// dynamically initialized to 1.0
double d1 = fd(); // may be initialized statically to 1.0
—end note]
3 It is implementation-defined whether or not the dynamic initialization (8.5, 9.4, 12.1, 12.6.1) of an object of
namespace scope is done before the first statement of main. If the initialization is deferred to some point
in time after the first statement of main, it shall occur before the first use of any function or object defined
in the same translation unit as the object to be initialized.31) [Example:
// – File 1 –
#include "a.h"
#include "b.h"
B b;
A::A(){
b.Use();
}
// – File 2 –
#include "a.h"
A a;
// – File 3 –
#include "a.h"
#include "b.h"
extern A a;
extern B b;
int main() {
a.Use();
b.Use();
}
It is implementation-defined whether either a or b is initialized before main is entered or whether the
initializations are delayed until a is first used in main. In particular, if a is initialized before main is
entered, it is not guaranteed that b will be initialized before it is used by the initialization of a, that is,
before A::A is called. If, however, a is initialized at some point after the first statement of main, b will
be initialized prior to its use in A::A. ]

Print this item

  13.1 when initializing with address of volatile variable
Posted by: michael.metivier - 08-08-2018, 03:51 AM - Forum: 8.13 Side effects - Replies (3)

13.1 indicates that an initializer list shall not contain persistent side effects, and gives an example of initializing with a volatile variable access. I believe there would be general agreement that

Code:
volatile uint16_t v1;

volatile uint16_t *a[1] = {&v1};
does not count as an "access" and is therefore compliant.

What about address of submembers of a volatile aggregate?
Code:
struct foo
{
    uint16_t bar;
};
volatile struct foo baz;
volatile uint16_t *a[1] = {&baz.bar};

Our cross-compiler defines our microconroller's memory mapped registers by address, like so:
Code:
struct foo
{
    uint16_t bar;
};
#define REG1 (*(volatile uint16_t *)0x100)
#define REG2 (*(volatile struct foo *)0x1000)

volatile uint16_t *a[2] = {&REG1, &REG2.bar};
Are either of the members of this initializer list complaint?

Print this item

  what is the method of defining infinite loop according to MISRA C-2012 compalince
Posted by: prem_annam - 06-08-2018, 04:34 AM - Forum: General Questions - Replies (1)

what is the method of defining infinite loop according to MISRA C-2012 compalince ?


Thank you.

Print this item

  Is there any MISRA rule to find this sideeffect??
Posted by: sprabhakars - 03-08-2018, 12:02 PM - Forum: 8.13 Side effects - Replies (6)

I am not sure, if this potential problem is categorized under any MISRA rules??

Code:
#include
static unsigned short var2;
int main()
{
       unsigned char var1 = 0;

       while (var1 <  var2)
       {
          var1++;
       }
       (void)printf("program completed \n ");
       return 0;
}

The variable var2 can have some value updated outside this function(may be more than 256) and causing a system crash as the while condition never fails.

I run this with PC-Lint and Parasoft's MISRA checker, both doesn't find any error with this portion of code.

Print this item

  HIS Subset of MISRA
Posted by: [email protected] - 27-07-2018, 07:17 PM - Forum: General Questions - Replies (4)

Dear colleagues,
I am trying to find in the web the MISRA subset for HIS with no success.
Does anybody know where it is possible to get these files / list of rules?

Thanks in advance,

Ricardo

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 137 online users.
» 0 Member(s) | 133 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: 624
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: 427
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: 597
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: 722
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: 436
M5-0-4 switch over unscop...
Forum: 6.5 Expressions (C++)
Last Post: misra cpp
25-09-2026, 01:00 PM
» Replies: 3
» Views: 534