Violation for rule 109

6.18 Structures and Unions

Moderators: misra-c, david ward

klaus83
Posts: 1
Joined: Tue Sep 07, 2010 3:27 pm
Company: PM°DM

Violation for rule 109

Postby klaus83 » Tue Sep 07, 2010 3:57 pm

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

misra-c
Posts: 547
Joined: Thu Jan 05, 2006 1:11 pm

Re: Violation for rule 109

Postby misra-c » Tue Sep 14, 2010 4:41 pm

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.
---
Posted by and on behalf of
the MISRA C Working Group

armand
Posts: 5
Joined: Wed Sep 01, 2010 4:18 pm
Company: CEA LIST

Re: Violation for rule 109

Postby armand » Mon Dec 20, 2010 9:42 am

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?

misra-c
Posts: 547
Joined: Thu Jan 05, 2006 1:11 pm

Re: Violation for rule 109

Postby misra-c » Tue Jan 11, 2011 11:55 am

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.
---
Posted by and on behalf of
the MISRA C Working Group


Return to “6.18 Structures and Unions”

Who is online

Users browsing this forum: No registered users and 0 guests