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

Username
  

Password
  





  MISRA AC SLSF:2023 AMD4 available
Posted by: misra-ac - 29-07-2025, 08:34 AM - Forum: MISRA AC SLSF discussions - No Replies

MISRA AC SLSF:2023 Amendment 4 (which contains modifications that bring the guidelines up to date for MATLAB release R2025a) is now available as a free download from the "MISRA AC resources" section of this Bulletin Board.

Print this item

  MISRA AC SLSF:2023 AMD4
Posted by: misra-ac - 29-07-2025, 08:10 AM - Forum: MISRA AC resources - No Replies

This Amendment to MISRA AC SLSF:2023 contains modifications that bring the guidelines up to date for MATLAB release R2025a. It includes required statuses of new diagnostics and revisions to the statuses of some library blocks and configurations.



Attached Files
.pdf   MISRA AC SLSF 2023 AMD 4.pdf (Size: 560.85 KB / Downloads: 4)
Print this item

  Rule 6.7.2 variable templates
Posted by: gdavis - 24-07-2025, 06:55 PM - Forum: 4.6 Basic concepts - Replies (1)

Is the following considered to be compliant:

Code:
template <typename T>
T var = 42;


By itself it seems not to be in the spirit of the rule since there's no "const" in the declaration.  But what if the only instantiation is something like:

Code:
int foo() { return var<const int>; }

Thank you.

Print this item

  Rule 0.1.1
Posted by: Merge - 30-06-2025, 12:59 PM - Forum: 4.0 Language independent issues - Replies (3)

Hi,

I was made aware of the following by a colleague.
In one of the official examples for rule 0.1.1 MISRA C++ 2023 states, quote:

Code:
m = 10; // Non-compliant - when looping, overwrites incremented value

However, I would argue that during the last loop iteration, the value is being read (and modified) by the increment before being returned. In the rule amplification, it is stated that an object is considered observed if it has been observed in any iteration, which I would therefore consider being the case here.

In other words, `m` is observed in this line, so it should not be a violation as per the rule amplification. Am I missing something?

I'll admit that the example is not the cleanest code for expressing the particular functionality (e.g., statement could be moved out of the loop), but we have to keep in mind that this is merely a simple example for illustration purposes and a real-world example might look comparatively more complex.

I would appreciate a second opinion :-)

Best regards,
Daniel "Merge" Merget

Print this item

  9.4.2 assert(false) in default clause, compliant?
Posted by: cgpzs - 03-06-2025, 07:01 PM - Forum: 4.9 Statements - Replies (1)

Hi,

MISRA C++:2023 Rule 9.4.2 provides a number of requirements on switch statements, in particular that every branch shall be unconditionally terminated.

Assuming that assertions are always enabled (also in Release builds), would this code be compliant with 9.4.2?

Code:
enum class Foo
{
  a,
  b
};

int get(Foo f)
{
  switch(f)
  {
    case Foo::a: { return 123; }
    case Foo::b: { return 321; }
    default: { assert(false && "Unreachable"); }
  }
}

assert(false) leads to calling the assert_handler function, which is marked [[noreturn]], so it would fall under requirement 5f of the rule. Would you agree?

Thanks!

Print this item

  MISRA C++ 2008 Rule 6-1-1 Clarification
Posted by: mshawa - 27-05-2025, 08:10 PM - Forum: 6.6 Statements (C++) - Replies (1)

Given the headline text of MISRA C++ 2008 Rule 6-1-1, MISRA C++ 2023 Rule 9.6.2 seems to be the equivalent rule in this case. MISRA C++ 2023 Rule 9.6.2 includes the following non-compliant example: 

Code:
switch(i)
{
    case 0:
      if(x < y)
        goto L3;            // Non-compliant
      break;

    case 1:
    L3:
      break;
}

This example seems to echo an amplification made in the MISRA C 2012 Rule 15.3 (that has the same headline text as MISRA C++ 2008 Rule 6-1-1), which states: 
  • Quote:For the purposes of this rule, a switch-clause that does not consist of a compound statement is treated as if it were a block.


It would be greatly appreciated if you could clarify whether the above example was also intended to violate MISRA C++ Rule 6-1-1.

Print this item

  11.9 and function calls and returned pointers
Posted by: danielmarjamaki2 - 26-05-2025, 07:22 AM - Forum: 8.11 Pointer type conversions - Replies (1)

Could you please clarify to me if this violates 11.9:

Code:
int* f1(void) {
    return 0;  // 11.9 ?
}

And does this violate 11.9:

Code:
void f2(int* ptr);

void f3(void) {
    f2(0);
}

Do you consider returns and function calls to be "assignments" otherwise it seems to me that the Amplification says these are compliant?

The last example in the PDF does suggest that function calls should be considered to be "assignments" however there is no non-compliant example code that I can rely on.

Thank you

Print this item

  9.4.2 - Requirement about the default clause
Posted by: cgpzs - 20-05-2025, 10:03 AM - Forum: 4.9 Statements - Replies (1)

Hi,

Rule 9.4.2 requires every switch statement to have a default clause, to match handling of if/else. However, unlike an if/else statement, this has a very serious implication: it prevents the compiler from warning about explicitly unhandled enumerators:

Code:
enum class Enum
{
  A,
  B,
  C  // newly added
};

// some client code, far away from the Enum declaration
int process(Enum e)
{
  switch(e)
  {
    case Enum::A: { return 123; }
    case Enum::B: { return 321; }
    // Oops, client code does not realize they should handle Enum::C, compiler didn't warn!
    default: { return 0; }
  }
}

One could put a runtime assertion in the `default` clause, but it only shifts the problem to the right. A problem that would have been caught at compile time can now only be caught at runtime. We should catch as many defects as possible at compile time.

One can also enable -Wswitch-enum, but this is not always desirable. Sometimes one explicitly wants the switch to be non-exhaustive, and can express that via the "default" clause. With the warning, one needs to introduce non-compliant inline suppressions with compiler-specific pragmas. Or one must explicitly list all the unwanted labels and fall-through the default case, polluting the code with non-interesting code paths.

Here we are balancing two risks:

1. The risk of not handling a valid enumerator (as above).
2. The risk of not handling an non-enumerator int casted into an Enum (as per the Rationale), in the spirit of defensive programming.

Questions: has Risk 1 been considered by MISRA? Is there evidence suggesting that Risk 2 is more likely to happen than Risk 1, and we should therefore prioritize it instead?

Thanks!

Print this item

  21.2.2 Why shall std::strerror not be used?
Posted by: cgpzs - 13-05-2025, 06:26 PM - Forum: 4.21 Language support library - Replies (3)

The rationale of the rule says:

> Also, some string handling functions only report errors through the use of errno

But that't not the case for std:: strerror, it does not report any errors through errno, nor is it possible for it to "read beyond the bounds of an object passed as a parameter", so it's perfectly safe to use it.

An alternative is to use std::make_error_condition(static_cast<std::errc>(errno)).message(), but it seems like overkill just to achieve the same thing, making the code far less readable.

It's also worth noting that MISRA does not ban the usage of errno (besides the restriction from 22.4.1), which is required for using POSIX functions that don't have any equivalent in the Standard Library.

Would you agree?

Print this item

  Rule 7.0.5, example non-compliant with 7.0.6?
Posted by: cgpzs - 17-04-2025, 12:10 PM - Forum: 4.7 Standard conversions - Replies (3)

Rule 7.0.5 presents this example:

Code:
u8a += static_cast< uint32_t >( u8b ) // Compliant - u8a -> unsigned int

Yes, compliant with 7.0.5. But this code then violates 7.0.6, since there is an implicit narrowing conversion from uint32_t to uint8_t in the assignment, right?

Code:
u8a = (uint32_t)(u8a) + (uint32_t)(u8b);

So, how are we supposed to write this type of code? Like this?

Code:
u8a = static_cast<std::uint8_t>(static_cast<std::uint32_t>(u8a) + static_cast<std::uint32_t>(u8b));

Does that really make the code safer?

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 103 online users.
» 1 Member(s) | 98 Guest(s)
Applebot, Bing, Facebook, UptimeRobot, JillB

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: 619
20.12 Does the stringific...
Forum: 8.20 Preprocessing directives
Last Post: misra-c
08-10-2026, 12:40 PM
» Replies: 1
» Views: 546
13.3 Should side effects ...
Forum: 8.13 Side effects
Last Post: misra-c
08-10-2026, 12:37 PM
» Replies: 1
» Views: 417
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,020
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: 588
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: 497
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: 442
Rule 0.1.1 Example f4
Forum: 4.0 Language independent issues
Last Post: cgpzs
07-10-2026, 11:07 AM
» Replies: 3
» Views: 709
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: 428
M5-0-4 switch over unscop...
Forum: 6.5 Expressions (C++)
Last Post: misra cpp
25-09-2026, 01:00 PM
» Replies: 3
» Views: 515