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

Username
  

Password
  





  Rule 6.8.4 clarification of amplification
Posted by: kth - 08-08-2025, 08:06 AM - Forum: 4.6 Basic concepts - No Replies

The amplification of rule 6.8.4 leads with this sentence to clarify to which member functions this rule applies:

Quote:This rule applies to member functions with reference or pointer return type, where, in the definition of the function, at least one
return expression explicitly designates this, *this or a subobject of *this.

The usage of the word “designates” here is ambiguous.
Does it mean the expression . . .

  1.  . . . is (or evaluates to something that is) this, *this or a subobject of *this?
  2.  . . . refers/points to (or evaluates to something that refers/points to) this, *this or a subobject of *this?
  3.  . . . contains this, *this or a subobject of *this in any shape or form?
  4. A combination of the above?

Depending on which interpretation is true, the following code examples are either
compliant or non-compliant:

Code:
class A {
std::int32_t data_;
public:
// returns a pointer to data member
std::int32_t const* Get() const { return &data_; }
};

class B {
std::int32_t* ptr_;
public:
// returns data member which is a pointer to an
// object with lifecycle independent of "this"
std::int32_t* Get() const { return ptr_; }
};

class C {
std::int32_t idx_;
public:
// returns unrelated pointer based on data member
float const* Get(std::array<float,10> const& data) const { return &data[idx_]; }
};

The intent of the rule seem to support meaning number 2, which would mean only A::Get is non-compliant.
If that is the case, we suggest to replace “designates” with a less ambiguous phrase, e.g. “references/points to” or to explain more clearly what is meant in the amplification.
In addition, clarifying examples for these cases would be helpful.

Print this item

  Rule 0.1.2 - missing exceptions and advisory ?
Posted by: kth - 07-08-2025, 10:07 PM - Forum: 4.0 Language independent issues - No Replies

Hi,

The amplification of MISRA C++:2023 rule 0.1.2 states that “This rule only applies when the function is called explicitly using function call syntax”.
Consequently, operators like “+=” are out of scope so in the following code the modification of the std::string object with “+=” is fine and the usage of append, without checking the returned reference, is a violation of this required rule:

Code:
void example1() {
    // Non-compliant use of std::string::append
    std::string my_string;
    my_string.append("foo");  // add 'foo'
    my_string.append("bar");  // add 'bar'
}

void example2() {
    // Compliant use of std::string::operator+=:
    std::string my_string;
    my_string += "foo";  // add 'foo'
    my_string += "bar";  // add 'bar'
}

void example3() {
    std::vector<int> numbers;

    numbers.push_back(1);  // Compliant to MISRA C++:2023 Rule 0.1.2
                           // as push_back ""returns"" void

    // C++ reference: https://en.cppreference.com/w/cpp/container/vector/push_back.html

    // emplace_back returns (since C++17) an iterator:
    // https://en.cppreference.com/w/cpp/container/vector/emplace_back.html

    numbers.emplace_back(2);  // Non-compliant to MISRA C++:2023 Rule 0.1.2 (since C++17)
}

The function append of std::string (std::basic_string) and the operator+= append additional characters to the end of the string. Both return a reference to “*this”. Both throw std::length_error if the operation would cause size() to exceed max_size() and provide the strong exception safety guarantee. So, there is no real difference between using “+=” or “append” in terms of safety or code quality. If one wants to write compliant code, example1 needs to be refactored to use a void cast  to ignore the result:

Code:
void example1_compliant_version() {
    std::string my_string;
    static_cast<void>(my_string.append("foo"));  // add 'foo'
    static_cast<void>(my_string.append("bar"));  // add 'bar'
}

That would implicitly force developers to always prefer operators like “+=” over functions like “append” as the alternatives make the code harder to read or lead to additional effort (deviations need to be justified).

If one decides to rework this kind of code to a “fluent API style” that could lead to extra copies:
Code:
std::string BuildFailureMsg(std::string_view name, std::string_view reason) {
    std::string msg{};
    msg.reserve(name.size() + reason.size() + 26);  // reserve sufficient memory
    msg.append("Component ");
    msg.append(name);
    msg.append(" failed due to ");
    msg.append(reason);
    msg.append("\n");
    return msg;
}
std::string BuildFailureMsgNaiveRefactor(std::string_view name,
                                      std::string_view reason) {
    std::string msg{};
    msg.reserve(name.size() + reason.size() + 26);  // reserve sufficient memory
    return msg.append("Component ")
        .append(name)
        .append(" failed due to ")
        .append(reason)
        .append("\n"); // will create and return copy of msg (i.e. see extra "memcpy")
}
int main() {
    std::string failure_msg_01 = BuildFailureMsg("Logger", "insufficient memory");  // no copies/moves
    std::string failure_msg_02 = BuildFailureMsgNaiveRefactor("Logger", "insufficient memory");  // extra copy
}

Consequently, existing code that uses append needs to be refactored to use “+=”. A huge refactoring effort for large code bases, but without any real benefit.
Also, it might lead to the negative effect that developers that uses std::vector prefer “push_back” over “emplace_back”, as with C++17 std::vector::emplace_back returns a reference (example3). Other alternatives might lead to less efficient code. 

Our questions:
  1.       Is it really intended to promote the usage of operators over functions and (for std::vector and other std-library types like std::optional::emplace) to use push_back over emplace_back or are here just exceptions missing?
  2. As the rationale states that “it is possible to call a function without using the return value, which may be an error”, should this rule be changed to “advisory” and the title changed to “The value returned by a function should be used”.
  3. Independent of 1 and 2: should an additional exception for functions that use C++ exceptions to report errors added?

Print this item

  Rule 10.1.1
Posted by: Merge - 04-08-2025, 07:59 AM - Forum: 4.10 Declarations - Replies (1)

Hi,

We are having a discussion with one of our tool vendors about how to interpret MISRA C++ 2023 rule 10.1.1, more concretely, the impact of the implicit 'this' parameter. I won't say which party supports which interpretation for the sake of neutrality.

Example code:
https://godbolt.org/z/hxz4bzvan


Interpretation A)
Fn() has an implicit, non-const 'this' parameter, so the rule is violated (Fn() should have been declared const).


Interpretation B)
Fn() does not have any parameters, so the rule is not violated.


We would appreciate an unbiased opinion to settle this discussion.

Best regards,
Daniel "Merge" Merget

Print this item

  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: 3)
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

Search Forums

(Advanced Search)

Forum Statistics
» Members: 6,232
» Latest member: MarcLG
» Forum threads: 1,029
» Forum posts: 2,844

Full Statistics

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

Latest Threads
21.18 is a safe strncpy f...
Forum: 8.21 Standard libraries
Last Post: dunno
20-08-2025, 08:47 AM
» Replies: 1
» Views: 1,831
Rule 10.1.1
Forum: 4.10 Declarations
Last Post: misra cpp
08-08-2025, 01:21 PM
» Replies: 1
» Views: 192
Rule 6.8.4 clarification ...
Forum: 4.6 Basic concepts
Last Post: kth
08-08-2025, 08:06 AM
» Replies: 0
» Views: 105
Rule 0.1.2 - missing exce...
Forum: 4.0 Language independent issues
Last Post: kth
07-08-2025, 10:07 PM
» Replies: 0
» Views: 118
Rule 6.7.2 variable templ...
Forum: 4.6 Basic concepts
Last Post: misra cpp
01-08-2025, 11:49 AM
» Replies: 1
» Views: 161
MISRA AC SLSF:2023 AMD4 a...
Forum: MISRA AC SLSF discussions
Last Post: misra-ac
29-07-2025, 08:34 AM
» Replies: 0
» Views: 148
MISRA AC SLSF:2023 AMD4
Forum: MISRA AC resources
Last Post: misra-ac
29-07-2025, 08:10 AM
» Replies: 0
» Views: 130
A3-3-2 Contradictory exam...
Forum: AUTOSAR C++:2014 rules
Last Post: misra cpp
18-07-2025, 12:03 PM
» Replies: 3
» Views: 1,659
16.6.1 clarification
Forum: 4.16 Overloading
Last Post: misra cpp
18-07-2025, 12:01 PM
» Replies: 3
» Views: 1,529
Rule 7.0.5, example non-c...
Forum: 4.7 Standard conversions
Last Post: misra cpp
18-07-2025, 12:01 PM
» Replies: 3
» Views: 1,582