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

Username
  

Password
  





  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 - No Replies

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

  Rule 9.3.1 - iteration statement followed by 'try'
Posted by: mstaron - 28-03-2025, 06:19 AM - Forum: 4.9 Statements - Replies (1)

It is possible to add the 'try' statement directly after 'while', for example:

class Exception{};
void f(int i)
{
    while(i) try {             // Non-compliant
        //...
    } catch(Exception e){
    }
}


I think this is non-compliant with the Rule 9.3.1, because 'try' is not a compound statement. However, accordingly to the standard 'try' should be always followed by a compound statement if it is inside a function. This code seems to be clear, so maybe this case could be added as an exception in the future versions of MISRA C++ standard.

Print this item

  16.6.1 clarification
Posted by: cgpzs - 26-03-2025, 02:36 PM - Forum: 4.16 Overloading - Replies (2)

Hi,

Rule 16.6.1 marks this example as non-compliant:

Code:
C operator+( int32_t rhs ) const;  // Non-compliant

However it does not provide a compliant alternative. It's unclear if rule 16.6.1 wants us to:

a) Duplicate the function and create 2 versions for each order of the parameters, in order to guarantee symmetry (even if it may not be useful for the user, potentially being dead code):

Code:
C operator+(C       lhs, int32_t rhs );
C operator+(int32_t lhs, C       rhs );

b) Implement only 1 overload, but as a non-member function. This still does not solve the problem of "operator+ must be symmetrical", i.e that "1 + c" and "c + 1" must be supported.
Code:
friend C operator+(C lhs, int32_t rhs );

Could you clarify?

Thanks!

Print this item

  Rule 8.2.8 - why aren't aliases allowed?
Posted by: cgpzs - 18-03-2025, 01:19 PM - Forum: 4.8 Expressions - Replies (1)

The rule provides this example as Non-Compliant:

Code:
    using hashPtr_t = std::uintptr_t;
    auto p2 = reinterpret_cast< hashPtr_t >( s );  // Non-compliant

However, there's nowhere in the Rule Text, Rationale or Amplification that explains why aliases are not allowed. Can you clarify?

Aliases are good for removing code duplication and ensuring the correct type is used in all contexts where it's needed; having to remove the alias and spell std::uintptr_t everywhere hurts code readability and maintainability, without any clear benefit.

Static analyzers can easily detect the usage of std::uintptr_t even if it's behind an alias.

Print this item

  Typo in Appendix C of MISRA C:2023
Posted by: Yordan Naydenov - 17-03-2025, 02:58 PM - Forum: 8.10 The essential type model - No Replies

Appendix C (C.1.1, p. 270 (hard copy) / p. 271 (pdf)*) of MISRA C:2023 (3rd Edn, 2nd Rev., April 2023) reads:
"... as the examples below show (si and ui are 32-bit signed and unsigned integers and u8 is an 8-bit unsigned char):"

However, there is a lack of correspondence between the above-introduced unsigned char object identifier of u8 and the naming of an operand of unsigned char type in the table that follows immediately (viz. uc, columns Expression and Notes). On page 273(274) (C.2.4, a distinct sub-section of the Appendix) such an unsigned char object of similar name is indeed used (u8a), in line with the exemplary non-specific object names used in code fragments throughout the Guidelines' body of text (the naming convention introduced on p. 14).

For the sake of ease of visually perceiving the types involved in the expressions and following through the implicit conversions they undergo, it is a good decision that concise, plain names such as si, ui, and uc are used, instead of operand identifiers cluttered with numeric characters (as would be in s32(i32), u32 and u8).

Apparently, this lack of correspondence between the operand names in the table and their prior introduction in the text is inherited from the previous versions of the Guidelines (MISRA C:2012 – 3rd Edn, March 2013; 3rd Edn, 1st Rev., February 2019). Never corrected by subsequent Technical Corrigenda or Amendments to those. It is now present in MISRA C:2025 as well.

Please, consider appending the erratum, reported in this separate forum thread, to the dedicated MISRA C Errata list maintained on the MISRA website, to keep it up-to-date and indeed a readily available single source of reference for all misprints found in MISRA C:2023, 3rd Edn, 2nd Rev.

_____

(*) Note that the text starts to disagree with the page numbering of the two documents (the electronic one and the hard copy) beginning from p. 229 onwards! This does not make favour to coherently referencing parts of the Guidelines and may lead to confusion and misunderstanding.

() This thread is considered the most relevant one as Appendix C is referenced mostly in there, and as there are no dedicated threads specifically aimed at the appendices, or some other general means for reporting typographical and other errors.

Print this item

  A3-3-2 Contradictory examples?
Posted by: cgpzs - 17-03-2025, 09:58 AM - Forum: AUTOSAR C++:2014 rules - Replies (2)

In Rule A3-3-2, there's an example with "class A" which has a non-constexpr, user-defined constructor. 

On line 31, we have a compliant example:

Code:
A instance{}; // Compliant - constant (value) initialization

However, on line 37 it's no longer compliant:

Code:
static A a{}; // Non-compliant - A’s default c’tor is not constexpr

There seems to be a contradiction here. Either the code is doing value initialization, or it's calling a constructor, but not both. Which one is it?

In other words, it seems like both examples are doing the same thing, so both should either be compliant or non-compliant. Would you agree?

Print this item

  Adopted modal expressions in guideline summaries in MISRA C as compared to MISRA C++
Posted by: Yordan Naydenov - 17-03-2025, 09:01 AM - Forum: General Questions - No Replies

There has been a discrepancy between MISRA C and MISRA C++ for some years now* concerning the modal verbs used with respect to the guideline categorization. It would be informative for the user of these Guidelines to understand what were the original reasons behind that and why this is still the case. Is it only a matter of distinct Working Groups (which nevertheless share/d some common members), has it to do with maintaining legacy, or something else entirely?

But the more concerning question is, is it planned for future MISRA C editions to adopt the more nuanced wording of the MISRA C++ Guidelines with respect to the guideline categories of "Mandatory", "Required", "Advisory", and "Disapplied"?

Currently, established convention for each of the two documents is such that a guideline is introduced through their respective summary (requirement (MISRA C) / headline (MISRA C++) text) employing one of the following modal verbs, subject to the guideline category (M/R/A/D):

  • MISRA C : "shall", "shall", "should", "should"
  • MISRA C++ : "must", "shall", "should", n/a§

A possible equalization of phrase will ultimately provide for a greater consistency between both sets of documents for the benefit of their mutual user. It would also make it straightforward for the user to unambiguously associate a guideline with its category.

_____

(*) Since introducing the category of "mandatory" in MISRA C++:2023 (October 2023), after it had been introduced in MISRA C:2012, 3rd Edn, March 2013.

() Note that guideline re-categorization entails no special treatment in this regard (e.g. substituting one modal auxiliary for another) as it is concerned only with how a guideline are to be handled and applied in a given, concrete project: compliance level claimed for a guideline may be affected due to their revised category but the normative prescription of the guideline, reclassified or not, never alters.

() MISRA C++:2008's category of "Document" ("shall") is not considered.

(§) Note that for the relatively new category of "Disapplied", introduced back in MISRA Compliance:2016 and subsequently added both in MISRA C++:2023 and recently in MISRA C:2025, only the latter actually provides any guideline with such a category. What modal expression will be adopted by future MISRA C++ editions for this fresh category is yet to be seen (could that be "ought (not) to" instead of "should (not)", to further discrimination between guidelines of different category?).

Print this item

Rainbow Roadmap to c23 support
Posted by: ACHart - 28-02-2025, 03:23 PM - Forum: General Questions - No Replies

Are there any formal plans/estimates/roadmaps for adding c23 support to MISRA?
If work for this has already begun, how is it going?  Smile

Print this item

  Rule 6.2.1 weak linkage
Posted by: hahn - 28-02-2025, 09:33 AM - Forum: 4.6 Basic concepts - Replies (1)

Dear MISRA group,

I have a question regarding the interpretation of the one-definition-rule in the presence of weak linkage (i.e. __attribute__((weak))).
In particular, is it considered a violation of the odr rule/rule 6.2.1 if a function with "strong" linkage "overrides" a function with "weak" linkage (as typically found in (standard) libraries)?

A special case within this discussion are global allocation and deallocation functions. The C++ standard calls them explicitly "replaceable".
Implementation-wise in any standard library I know, this is expressed by the weak linkage attribute.
Since the standard allows these few functions to be replaced explicitly, I conclude this does not constitute a violation of the one-definition-rule.

But how about any other weak-linkage function?

Thanks in advance!

Print this item

  Rule 3-4-1 and lifetimes, initialization, and side effects
Posted by: aromauld - 12-02-2025, 07:06 PM - Forum: 6.3 Basic concepts (C++) - Replies (1)

We have some interesting cases for Rule 3-4-1 which we aren't sure whether they are violations or not. Strictly adhering to the rule of reduced visibility could cause issues regarding lifetimes, initialization, and side effects of a constructor/destructor.

For instance, in the following example, is the intent to require changes to the code so that 'old_value' is not visible after the if body, or is `old_value` considered non-violating?

Code:
void fn(int a) {
    int old_value = a;
    a += 4;
    if (a > 10) {
        a = old_value;
    }
    // ...
}

Similarly, in cases where an object's constructor or destructor contains side effects, such as with a mutex, is the current visibility of 'merge_thread_lock' intended to be a violation?

Code:
void fn(int a) {
    std::unique_lock<std::shared_mutex> merge_thread_lock(global_mutex);
    if (global) {
      global = a;
      merge_thread_lock.unlock();
    }
    // ...
}

Print this item

Search Forums

(Advanced Search)

Forum Statistics
» Members: 6,204
» Latest member: bipepex189
» Forum threads: 1,017
» Forum posts: 2,796

Full Statistics

Online Users
There are currently 226 online users.
» 0 Member(s) | 224 Guest(s)
Bing, Google

Latest Threads
Rule 7.0.5, example non-c...
Forum: 4.7 Standard conversions
Last Post: cgpzs
17-04-2025, 12:10 PM
» Replies: 0
» Views: 132
A3-3-2 Contradictory exam...
Forum: AUTOSAR C++:2014 rules
Last Post: cgpzs
31-03-2025, 09:30 AM
» Replies: 2
» Views: 256
16.6.1 clarification
Forum: 4.16 Overloading
Last Post: cgpzs
31-03-2025, 09:29 AM
» Replies: 2
» Views: 256
Rule 9.3.1 - iteration st...
Forum: 4.9 Statements
Last Post: misra cpp
28-03-2025, 01:17 PM
» Replies: 1
» Views: 166
Rule 8.2.8 - why aren't a...
Forum: 4.8 Expressions
Last Post: misra cpp
28-03-2025, 01:05 PM
» Replies: 1
» Views: 173
Typo in Appendix C of MIS...
Forum: 8.10 The essential type model
Last Post: Yordan Naydenov
17-03-2025, 02:58 PM
» Replies: 0
» Views: 147
Adopted modal expressions...
Forum: General Questions
Last Post: Yordan Naydenov
17-03-2025, 09:01 AM
» Replies: 0
» Views: 215
Roadmap to c23 support
Forum: General Questions
Last Post: ACHart
28-02-2025, 03:23 PM
» Replies: 0
» Views: 196
Rule 6.2.1 weak linkage
Forum: 4.6 Basic concepts
Last Post: misra cpp
28-02-2025, 01:04 PM
» Replies: 1
» Views: 243
A8-4-5: Should have an ex...
Forum: AUTOSAR C++:2014 rules
Last Post: misra cpp
21-02-2025, 12:58 PM
» Replies: 3
» Views: 658