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

Username
  

Password
  





  Rule 1.3 - Taking address of near auto variable
Posted by: Francois - 30-04-2018, 08:41 AM - Forum: 8.1 A standard C environment - Replies (6)

Hi all,

PC-Lint warn me that i'm using the address of a near auto variable.

My code looks like this:

Code:
/* static var */
static int16_t MySignVar = 0;

StatusType tMyfunc(void)
{
   uint16_t MyUnSignVar = 0U;
   StatusType ReturnValue;
  
   ReturnValue = tSomefunt(Parameter_1, &MyUnSignVar ); /* Note 934: Taking address of near auto variable... [MISRA 2012 Rule 1.3, required]... */
  
   /* MyUnSignVar is always positive */
   MySignVar += (int16_t)MyUnSignVar ;
  
   return (ReturnValue);
}

Could you help me to handle this issue?

Print this item

  MISRA 20.2
Posted by: ankitshah413 - 26-04-2018, 02:18 PM - Forum: 6.20 Standard Libraries - Replies (1)

Hello,

Why the following code is the violation of MISRA 20.2 ?

Code:
#define abs(a)   (((a) < (0)) ? -(a) : (a))

As far as I understand from the topics , 20.2 does not concern with preprocessor identifier then why I get an violation for the same.

Print this item

  Misra Compliant Math Library
Posted by: dejanpan - 23-04-2018, 05:17 AM - Forum: General Questions - Replies (3)

Hi there,
we are looking for a certified (ISO 26262) and possibly Misra compliant Math library. Both C or C++ would do. We would need it to run on both Arm and Intel architectures which for instance eliminates Intel MKL (which is apparently getting certified).

We have looked through the open source implementations, in particular Eigen, Armadillo, or Blaze but all of them throw too many Misra defects.

We would heavily appreciate any suggestions or if none of the libraries are available - what do automotive folks then normally do?


We are looking for the following features in the library:

1. static memory representation:
dense matrices (probably column major for efficient vector representations)
sparse matrices
symmetric matrices
upper/lower triangular matrices
diagonal matrices
block representations

2. basic math operations (in a memory efficient manner):
addition
subtraction
multiplications:
matrix-matrix
matrix-vector
matrix-scalar
vector-vector (inner, outer products)
vector-scalar
transposition
integer powers
able to apply basic scalar math operations on the entire matrix

3. basic solvers:
norms:
froebenius
l2
l1
infinity
matrix inversion
linear system solvers (e.g. Ax = b)

Print this item

  Rule 6-6-5 A function shall have a single point of exit at the end of function.
Posted by: Renan Lavarec - 19-04-2018, 04:24 PM - Forum: 6.6 Statements (C++) - Replies (2)

Hello,

We are interested about id 0000023 but in C++.

We would like to be MISRA compatible.
But forbid multiple return in some case make the code less understandable.
Further more with the use of RAII, it is pretty safe to have multiple return inside a function.
This rule inherited from C where multiple return is highly unsafe is about to evolve in new version of MISRA C++ ?
If not, this is a "Required" rules, could it be added to a "deviation permits" ?


Example:
If we transform this code using "early return pattern" method which is not MISRA on rule 6-6-5.

Code:
int CheckIsOk()
{

if (currentObject == NULL)
{
    return E_ERR_1;
}
if (!IsClassValid(id))
{
    return E_ERR_2;
}

currentObject->test();

return E_OK;
}

into MISRA

Code:
int CheckIsOk()
{
int err = E_OK;
if (err == E_OK && currentObject == NULL)
{
    err = E_ERR_1;
}
if (err == E_OK && !IsClassValid(id))
{
    err = E_ERR_2;
}

currentObject->test(); // Developer miss to add "err == E_OK && "


return err;
}

If you miss to add a test somewhere like for testing "again" currentObject before to use it, you'll get a possible crash....
It increase complexity of code and make it unsecured.
Readability is something very important to get trusty code.

You can also transform it to another form

Code:
int CheckIsOk()
{
    int err = E_OK;
    if (currentObject)
    {
        if (IsClassValid(id))
        {
            currentObject->test();
        }
        else
        {
            err = E_ERR_2;
        }
    }
    else
    {
        err = E_ERR_1;
    }

    // "Difficulties" to add code here
    if( err == E_OK)
    {
        // Do something
    }

    return err;
}

But the readability is less good than "early return pattern", because you "jump" from "if" to "if" and the last part to execute some code is subject to errors (or after any else), if no test are added before the return statement to test the error state.


ps:
In MISRA 2008 document chapter 6-6-5, there is a synthax mistake on fn3, there is missing {}

Print this item

  In Example suit R_05_01_2.c
Posted by: swasti - 17-04-2018, 11:15 AM - Forum: 8.5 Identifers - Replies (1)

In Example suit R_05_01_2.c
Kindly explain why the following code is non-compliant?
int32_t ABC;
int32_t ABC = 0; /* Non-compliant */

Print this item

  How do explicit casts of arguments affect 21.15 (amendment 1)?
Posted by: abgs - 09-04-2018, 07:45 PM - Forum: 8.21 Standard libraries - Replies (1)

When rule 21.15 of MISRA C 2012 Amendment 1 refers to the types that pointer arguments are pointers to, how are explicit casts interpreted? Are the types that they are pointers to considered before or after the cast?

Code:
void f(signed char* a, int* b, size_t n) {
    memcmp(a, a, n); // ok - pointers to same type
    memcmp(b, b, n); // ok - pointers to same type
    memcmp((int*)b, (int*)b, n); // ok - same type and cast has no effect
    memcmp((int*)a, (int*)a, n); // ok? - cast to different pointer type, arguments match before and after but with different matching types
    memcmp(a, b, n); // violation - pointers to different types
    memcmp((int*)a, b, n); // violation? - types only match due to pointer cast
    memcmp((float*)a, (float*)b, n); // violation? - different pointer types cast to third common pointer type
    memcmp((short*)a, a, n); // violation? - original pointer types match before cast
}

Print this item

  CERT C EXP46-C considered Out of Scope by MISRA C:2012 Addendum 3
Posted by: jlerch - 06-04-2018, 09:25 AM - Forum: General Questions - Replies (2)

MISRA C:2012 Addendum 3 defines the coverage classification "Out of Scope" as "Aspects of behaviour are out of scope for C99 and are related to C11." Considering this definition, why is EXP46-C classified "Out of Scope"? It seems, there is no dependency of the rule to C11 at all.

I would actually argue that EXP46-C is covered by MISRA C:2012 essential type model explicitly and strong.

Print this item

  CERT C EXP35-C considered Out of Scope by MISRA C:2012 Addendum 3
Posted by: jlerch - 06-04-2018, 07:45 AM - Forum: General Questions - Replies (1)

MISRA C:2012 Addendum 3 defines the coverage classification "Out of Scope" as "Aspects of behaviour are out of scope for C99 and are related to C11." Considering this definition, why is EXP35-C classified "Out of Scope"? The very first example I see in EXP35-C (section 4.5.1 in CERT C 2016 edition) is a non-compliant example for C99. CERT C actually states that the example would work fine in C11, but fails in C99.

Print this item

  Rule 21.6 and snprintf()
Posted by: dejanpan - 05-04-2018, 03:01 AM - Forum: General Questions - Replies (4)

We are using snprintf() function (https://linux.die.net/man/3/snprintf) in a framework that lets you program autonomous driving cars (our product).
The function is currently being used only for logging of console data on the secondary, non-real time computer.


However, both PCLint and Coverity static code analysis tools mark it as non-compliant:

Rule 21.6
The Standard Library input/output functions shall not be used
C90 [Unspecified 2–5, 16–18; Undefined 77–89; Implementation 53–68]
C99 [Unspecified 3–6, 34–37; Undefined 138–166, 186; Implementation J.3.12(14–32)] [Koenig 74]

Category: Required
Analysis: Decidable, Single Translation Unit
Applies to: C90, C99

Amplification
This rule applies to the functions that are specified as being provided by and, in C99,
their wide-character equivalents specified in Sections 7.24.2 and 7.24.3 of the C99 Standard as being
provided by .

None of these identifiers shall be used and no macro with one of these names shall be expanded.

Rationale
Streams and file I/O have unspecified, undefined and implementation-defined behaviours associated
with them.

See also
Rule 22.1, Rule 22.3, Rule 22.4, Rule 22.5, Rule 22. 6



We have a following rebuttal:
The problem is that if the source and destination string buffers in a snprintf() call are the same, the behavior is undefined.

These buffers are passed as char pointers to

Code:
snprintf()
. Checking that two pointer variables can never be the same in a program (in all possible executions) is called alias analysis. It is a static code analysis that static code analysis tools can do, but it usually is imprecise (can have false positives). So it is easier for them to ban the use of
Code:
snprintf()
altogether.

We claim that we can use snprintf() ONLY IF we ALWAYS check that the source and destination buffers are NOT the same before calling snprintf().

Code:
if source != destination:
  snprintf(...)
else:
  raise error

Pardon pseudo code but if I use actual C code it won't let me post this question.

Additionally, based on the rule's Rationale text it could be argued that the snprintf() call is OK since it's not a stream or file output call.

I would love to hear some thoughts from MISRA members.

Print this item

  Dir 4.10: Is a different form of the include guard considered a violation of the directive?
Posted by: GerlindeKettl - 29-03-2018, 12:36 PM - Forum: 7.4 Code design - Replies (1)

The example of Directive 4.10 "Precautions shall be taken in order to prevent the contents of a header file being included more than once" says that in order to facilitate checking, the contents of the header should be protected from being included more than once using one of the following two forms:

Code:
#if !defined ( identifier )
#define identifier
/* Contents of file */
#endif



#ifndef identifier
#define identifier
/* Contents of file */
#endif
I got software from a third party which uses the first form, but the parentheses are a bit differently placed:

Code:
#if ( !defined identifier )
As I consider this include guard correct from a functional point of view, is this nevertheless a violation of Dir 4.10 because it does not use one of the two forms listed in the example?

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 157 online users.
» 0 Member(s) | 153 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: 723
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: 536