Let me start by saying that I have no experience with MISRA-C ++, but a lot of MISRA-C.
MISRA-C has some concerns about type safety, which also apply to MISRA-C ++. One of these problems is that no implicit type advertising campaigns should occur. This is an urgent problem, implicit advertising campaigns are difficult to understand and lead to errors. Most C and C ++ programmers, surprisingly, do not even know how implicit promotions work. To educate programmers about this and protect against such errors, there are many MISRA rules regarding implicit type conversions / promotions.
One of these rules applies the suffix 'u' to all integer literals. The rationale for this rule is to find out that large literals close to the maximum int value are unsigned, for example, the literal type 0x80000000 not obvious to the reader. (I personally find this rule redundant and somewhat erroneous, since all implicit conversion threats are already covered by other rules.)
There is another rule indicating that the pointer checks that NULL must be explicit. You are not allowed to write if(ptr) , you must write if(ptr!=NULL) . Justification is the readability and type safety.
And, apparently, this rule indicates that pointers should not be compared with a null literal. I donβt understand the reasons for this, presumably they are afraid that you will mix pointers and simple integer variables. Apparently, they decided to deviate from the desire of Bjarna Straustrup to de-mystify null pointers in C ++. According to Stroustrup, NULL and 0 are always equivalent in C ++ (although C ++ 11 will have the nullptr keyword resolving this mess once and for all).
None of the above rules has anything to do with the code in your example! You are comparing a reference to a null literal, which is completely safe. The MISRA controller may complain about the lack of a 'u' suffix, but your checker did not.
My findings:
- The MISRA-C ++ controller is faulty and gives the wrong errors.
- The specific MISRA-C ++ rule against literal zero does not seem to make any sense. You must raise the deviation in your MISRA implementation against this rule and completely ignore the rule until someone can justify it.
source share