MISRA Discussion Forums
Violation for rule 109 - 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.18 Structures and Unions (https://forum.misra.org.uk/forumdisplay.php?fid=48)
+---- Thread: Violation for rule 109 (/showthread.php?tid=771)



Violation for rule 109 - klaus83 - 07-09-2010

Hello everybody,

Does anybody know if rule 109: "Overlapping storage shall not be used" is violated by having two pointer variables pointing on the same address??
My automated MISRA checker only looks for union declarations and definitions when checking this rule.


Thanks,
Klaus


Re: Violation for rule 109 - misra-c - 14-09-2010

The MISRA C Rule you refer to is from the 1998 version. It was replaced in 2004 by a new version, MISRA C2, and this is the only version that should be used for new projects.

The original Rule 109 has been split into two rules in MISRA C2, rules 18.2 and 18.3 so the answer will be given in relation to these rules. Rule 18.2 requires that objects are not assigned to overlapping objects and Rule 18.3 requires that an area of memory isn't used for unrelated purposes.

The existence of two pointers that point to the same address does not in itself violate either of the MISRA C2 rules. If the pointers are used to copy an object to an overlapping object then the rule is violated. Similarly if the pointers are used to access the memory for completely different purposes, the rule is violated although it is difficult for a tool to be able to check this case.


Re: Violation for rule 109 - armand - 20-12-2010

May I ask for some more details about rule 18.2: what precisely does the word "assign" include? Is it memory copy functions such as memcpy, memmove, memccpy, strcpy, strncpy, bcopy ? Does it also deal with standard assignments within unions? Any other operations?


Re: Violation for rule 109 - misra-c - 11-01-2011

Rule 18.2 references two undefined behaviours that are described in the C90 standard. Undefined 34 relates to assignment of overlapping objects. Undefined 55 relates to copying memory using library functions other than memmove (this function is designed to permit overlapping memory areas to be copied). The rule therefore covers both assignment and copying using library functions.

The standard defines assignment between overlapping objects only when both objects overlap exactly and have the compatible types, ignoring type qualifiers. Therefore, assigning one member of a union to another member of the same union would seem to be well defined and is therefore permitted by Rule 18.2, although unions are not permitted by Rule 18.4.