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

Username
  

Password
  





  A18-9-4
Posted by: cgpzs - 15-10-2024, 09:58 AM - Forum: AUTOSAR C++:2014 rules - No Replies

Hi!

I wonder if this code example technically violates A18-9-4?

Code:
#include <memory>

struct Foo
{
    explicit Foo(int);
};

int main()
{
    int val{};
    auto x = std::make_unique<Foo>(val);
    ++val; // non-compliant?
}

Since internally, std::make_unique takes the input arguments as "Args&&..." and std::forward's them into the constructor of the class.

Thanks!

Print this item

  MISRA 2023 Test Suite
Posted by: grigdon - 14-10-2024, 01:27 PM - Forum: General Questions - No Replies

Hi,

Is there a MISRA 2023 test suite? I'm looking for C code with violations to be used for static analysis tool validation.

Thanks

Gerry

Print this item

  MISRA C:2023 ADD4
Posted by: david ward - 11-10-2024, 07:41 PM - Forum: MISRA resources - No Replies

This Addendum to MISRA C:2023 sets out the coverage by MISRA C:2023 against the language-independent guidance of ISO/IEC 24772-1:2019 and the C language specific guidance of ISO/IEC 24772-3:2020



Attached Files
.pdf   MISRA C 2023 ADD4.pdf (Size: 364.7 KB / Downloads: 3)
Print this item

  MISRA C:2023 ADD2
Posted by: david ward - 11-10-2024, 07:40 PM - Forum: MISRA resources - No Replies

This Addendum to MISRA C:2023 sets out the coverage by MISRA C:2023 against ISO/IEC 17961, incorporating the 2016 technical corrigendum to the latter document.



Attached Files
.pdf   MISRA C 2023 ADD2.pdf (Size: 285.63 KB / Downloads: 3)
Print this item

  Rule 7.0.2: operator const char *()
Posted by: karos - 11-10-2024, 12:33 PM - Forum: 4.7 Standard conversions - Replies (2)

Hello,

Rule 7.0.2 has an exception for converting pointer to bool, and requires an explicit operator bool() for classes. But what if a class, let's say a wrapper class for strings, provides an operator const char *? Consider the following example:

Code:
class C
{
public:
    operator const char *() const;
};

void f(C c)
{
    if (c); // C is converted twice: C -> const char * -> bool
}

Shall the commented line be considered compliant with this rule (because each of the conversion steps by itself is compliant), or not (because as a whole it is the conversion of a class to bool without using an explicit operator bool)?

Print this item

  A8-4-5: are partial moves allowed?
Posted by: cgpzs - 01-10-2024, 12:28 PM - Forum: AUTOSAR C++:2014 rules - No Replies

Are partial moves allowed by A8-4-5? If not, why not?

For example, consider the following snippet from Effective Modern C++ (Meyers), Item 25, page 169:

Code:
class Widget {
public:
   Widget(Widget&& rhs)
   : name(std::move(rhs.name)),
     p(std::move(rhs.p))
   { … }
   …

// rhs is rvalue reference
private:
   std::string name;
   std::shared_ptr<SomeDataStructure> p;
};

Print this item

  Does Directive 5.7.2 apply to #elif directives?
Posted by: rg99 - 30-09-2024, 09:29 PM - Forum: 4.5 Lexical conventions - Replies (1)

Is the following intended to be compliant with 5.7.2?

Code:
#if 1
/* some code */
#elif 0
/* some code */
#endif

Presumably this is just as offensive to the spirit of 5.7.2 as #if 0 is but an official position would be appreciated as the Directive only actually mentions #if.

Print this item

  is a cast compliant with Rule 12.4
Posted by: sowisojh - 30-09-2024, 08:25 AM - Forum: 8.12 Expressions - No Replies

Is the cast in the following code compliant with MISRA2023 Rule 12.4, assuming a machine having int as 32 bit?


Code:
#include <stdint.h>

#define USEFUL_BIT ((uint16_t)0x0020u)

uint16_t clear_useful_bit(uint16_t bitmask)
{
  uint16_t cleared_mask = bitmask;

  cleared_mask &= (uint16_t)~USEFUL_BIT;

  return cleared_mask;
}

According to Appendix D.7.5 1.1 the result of ~ is the UTRL of the result. Assuming an 32 bit integer machine this would be 4294967263u. The result of the cast to uint16_t would be perform a "repeatedly [...] subtracting one more than the maximum value that can be represented in the new type until the value is in the new type" which leads to 65503.

Is this type conversion treated as "unsigned integer wrap-around" in the sense of Rule 12.4 ?

Print this item

  Do any other forms of conditional inclusion violate Directive 5.7.2?
Posted by: rg99 - 27-09-2024, 11:21 AM - Forum: 4.5 Lexical conventions - Replies (1)

In particular, which of the following violates Directive 5.7.2?

Code:
#if 0
#endif

#if 0U
#endif

#if '\0'
#endif

#if 1 - 1
#endif

#if MACRO * 0
#endif

#if MACRO1 - MACRO2
#endif

Print this item

  Rule 0.1.9 - is zero-initialization considered "dead code"?
Posted by: cgpzs - 25-09-2024, 07:22 AM - Forum: 6.0 Language independent issues (C++) - Replies (3)

Consider the following example:

Code:
// third_party.h
struct Foo
{
   int32_t a;
   int32_t b;
};

// client.cpp
Foo create()
{
  Foo f{};     // Violates Rule 0.1.9?
  f.a = 123;
  f.b = 321;
  return f;
}

Does that violate Rule 0.1.9? While the initialization may be redundant, it serves a purpose as defensive programming. If we remove the zero-initialization, we risk having some members of "Foo" uninitialized, especially if we bump to a new version of "third_party.h" that adds a new member to the struct. It's safer to zero-initialize at the declaration, to ensure no members remain uninitialized.

It's also preferable to initialize like this instead of "Foo f{123, 321};", because we can see written in code which field gets which value. We need to wait until C++20 to get designated initializers in C++ to initialize everything in one line.

Thanks!

Print this item

Search Forums

(Advanced Search)

Forum Statistics
» Members: 6,161
» Latest member: Navya Gangaiah
» Forum threads: 996
» Forum posts: 2,745

Full Statistics

Online Users
There are currently 221 online users.
» 0 Member(s) | 218 Guest(s)
Bing, DuckDuckGo, Google

Latest Threads
MISRA AC EC guidelines
Forum: MISRA AC SLSF discussions
Last Post: misra-ac
Yesterday, 08:21 AM
» Replies: 4
» Views: 15,155
News on future releases
Forum: MISRA AC SLSF discussions
Last Post: misra-ac
Yesterday, 08:05 AM
» Replies: 1
» Views: 5,531
Signal naming convention ...
Forum: MISRA AC SLSF discussions
Last Post: misra-ac
Yesterday, 07:57 AM
» Replies: 1
» Views: 7,055
A18-9-4
Forum: AUTOSAR C++:2014 rules
Last Post: cgpzs
15-10-2024, 09:58 AM
» Replies: 0
» Views: 61
Rule 7.0.2: operator cons...
Forum: 4.7 Standard conversions
Last Post: karos
14-10-2024, 08:52 PM
» Replies: 2
» Views: 175
MISRA 2023 Test Suite
Forum: General Questions
Last Post: grigdon
14-10-2024, 01:27 PM
» Replies: 0
» Views: 69
MISRA C:2023 ADD4
Forum: MISRA resources
Last Post: david ward
11-10-2024, 07:41 PM
» Replies: 0
» Views: 82
MISRA C:2023 ADD2
Forum: MISRA resources
Last Post: david ward
11-10-2024, 07:40 PM
» Replies: 0
» Views: 75
Rule 0.1.9 - is zero-init...
Forum: 6.0 Language independent issues (C++)
Last Post: misra cpp
11-10-2024, 02:46 PM
» Replies: 3
» Views: 469
Does Directive 5.7.2 appl...
Forum: 4.5 Lexical conventions
Last Post: misra cpp
04-10-2024, 02:48 PM
» Replies: 1
» Views: 202