MISRA Discussion Forums
Violations of rules 11.1, 11.2, 11.3, 11.4 - Printable Version

+- MISRA Discussion Forums (https://forum.misra.org.uk)
+-- Forum: MISRA C (https://forum.misra.org.uk/forumdisplay.php?fid=4)
+--- Forum: MISRA-C: 2004 rules (https://forum.misra.org.uk/forumdisplay.php?fid=17)
+---- Forum: 6.11 Pointer Type Conversions (https://forum.misra.org.uk/forumdisplay.php?fid=38)
+---- Thread: Violations of rules 11.1, 11.2, 11.3, 11.4 (/showthread.php?tid=693)



Violations of rules 11.1, 11.2, 11.3, 11.4 - adytzul_ac - 25-11-2009

Hello.
My PC-LINT tool gives me an error of violating of rules 11.1, 11.2, 11.3, 11.4 (cast from unsigned char to pointer ) on the line 2:

line 1: unsigned char* ptr;
line 2: ptr=(unsigned char*)0x12;

The idea behind is that I want to initialize a pointer with a non-zero address in a MISRA-compliant style.

Thank you for the answer.

Best regards.


Re: Violations of rules 11.1, 11.2, 11.3, 11.4 - William Forbes - 25-11-2009

Hi,

You can cheat:
Code:
ptr = (unsigned char*)( (void*)0x12 );
This is compliant but IMHO should be a violation of the spirt or the MISRA rules!

Certain low level code will indeed violate MISRA rules, and a deviation can and should be raised.


Re: Violations of rules 11.1, 11.2, 11.3, 11.4 - adytzul_ac - 26-11-2009

Hello again.

I already tried the small "cheat" you advised me, but I get the same warning.

Best Regards.


Re: Violations of rules 11.1, 11.2, 11.3, 11.4 - jbezem - 26-11-2009

PC Lint and many other checking tools have adopted the MISRA rules after laying out their warning concepts earlier (PC Lint exists since the middle of the eighties!). Unless a tool has designed/revised its warning system to exactly match MISRA's ideas on what is good and what is not, there will be some areas not properly covered, and other areas covered too restrictively.
Have a look in the 'au-misra2.lnt' file in your distribution, and look for the messages enabled for the various MISRA rules. For instance, for rule 11.1, warning 923 is activated. The rule forbids conversions between function pointers and non-integral types. Warning 923 of Lint warns about conversions between pointers and non-pointers. For observation of only rule 11.1, Lint is too restrictive. But the mapping has been created with the premise that it's better to warn too often than not at all.
Since the Lint configuration file for MISRA is a tool and not the law, if you find discrepancies you want to avoid, create your own version of the options file, or provide deviations in the source code, at your discretion.

And for the record: This is a restriction of all tools claiminng to "support MISRA" I have seen to date. The quality of the MISRA support is dependent on the warning granularity of the original product. And for instance, the concept of 'underlying type' was introduced by MISRA 2004; several tools still have difficulties in this area, because the concept was never in their architectures until MISRA 2004 came out.

Even if it's not much concrete help for your present situation, I hope the explanation helps clarify your options.

BR,

Johan


Re: Violations of rules 11.1, 11.2, 11.3, 11.4 - adytzul_ac - 26-11-2009

Thank you very much for your answer, Johan.


Re: Violations of rules 11.1, 11.2, 11.3, 11.4 - misra-c - 20-01-2010

line 2 is indeed a violation of Rule 11.3. The rule text recognises that there are circumstances, such as accessing hardware features, in which it is necessary to cast an integer to a pointer.

The recommendation, as also made by William Forbes, is to deviate Rule 11.3 in this case. Note that a cast via (void *) is still a violation of rule 11.3