473,831 Members | 2,230 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Misra Rule 18.2. An object shall not be assigned to an overlapping object

Hello

I would like to create a test case that violates the following MISRA
C:2004 rule .

Misra Rule 18.2. An object shall not be assigned to an overlapping
object.
"The behaviour is undefined when two objects are created which have
some overlap in memory and one is copied to the other"

The sample I have created is
---------------------------------------------------------------
#include <string.h>

typedef char char_t;

char_t main(void)
{

char_t Buffer[]= "is this a violation?";
memcpy(&Buffer[5], &Buffer[10], 10); /*Violation, memory over lapped,
result will equal "is thviolation?tio n?"*/

return(Buffer[5]);
}

-------------------------------------------------------------------------------------
However the rule relates to copying one object to another different
object where the two objects have some memory overlapping. It could
possibly be related to using unions.

Can anyone think of a way I can violate this rule??

Mar 29 '06 #1
5 6504
"ha********@goo glemail.com" <ha********@goo glemail.com> wrote:
I would like to create a test case that violates the following MISRA
C:2004 rule .

Misra Rule 18.2. An object shall not be assigned to an overlapping
object.
"The behaviour is undefined when two objects are created which have
some overlap in memory and one is copied to the other"

The sample I have created is
---------------------------------------------------------------
#include <string.h>

typedef char char_t;

char_t main(void)
{

char_t Buffer[]= "is this a violation?";
memcpy(&Buffer[5], &Buffer[10], 10); /*Violation, memory over lapped,
result will equal "is thviolation?tio n?"*/
No. _May_ equal that. The result is, as Misra states correctly,
undefined. Anything may happen; getting the result you expect is a valid
result, not copying anything is a valid result, copying something else
is a valid result, crashing the program is equally valid. If you don't
want that, use memmove(), not memcpy().
However the rule relates to copying one object to another different
object where the two objects have some memory overlapping. It could
possibly be related to using unions.
Possibly. If you want to know what Misra had in mind when they wrote
that rule, ask Misra.
Can anyone think of a way I can violate this rule??


*Shrug* Copy one union member to another?

Richard
Mar 29 '06 #2
Can anyone think of a way I can violate this rule??


*Shrug* Copy one union member to another?

Yeah that is the only way I can think of doing such a task. That makes
this Misra rule redundant because the next rule states "Unions shall
not be used"

I was just trying to find out if this could be done without the use of
Unions.

Mar 29 '06 #3
ha********@goog lemail.com wrote:
Hello

I would like to create a test case that violates the following MISRA
C:2004 rule .

Misra Rule 18.2. An object shall not be assigned to an overlapping
object.
"The behaviour is undefined when two objects are created which have
some overlap in memory and one is copied to the other"

The sample I have created is
---------------------------------------------------------------
#include <string.h>

typedef char char_t;
Why?
char_t main(void)
{
int main(void).

char_t Buffer[]= "is this a violation?";
memcpy(&Buffer[5], &Buffer[10], 10); /*Violation, memory over lapped,
result will equal "is thviolation?tio n?"*/

return(Buffer[5]);
}

-------------------------------------------------------------------------------------
However the rule relates to copying one object to another different
object where the two objects have some memory overlapping. It could
possibly be related to using unions.

Can anyone think of a way I can violate this rule??

How about this, assuming sizeof(int) = 4:

char stuff[6];

int* p1 = (int*)stuff;
int* p2 = (int*)&stuff[2];

*p1 = 42;

*p2 = *p1;

--
Ian Collins.
Mar 29 '06 #4

Ian Collins wrote:
ha********@goog lemail.com wrote:
Hello

I would like to create a test case that violates the following MISRA
C:2004 rule .

Misra Rule 18.2. An object shall not be assigned to an overlapping
object.
"The behaviour is undefined when two objects are created which have
some overlap in memory and one is copied to the other"

The sample I have created is
---------------------------------------------------------------
#include <string.h>

typedef char char_t;

Why?


I am testing how well PC-LINT performs in detecting Violations of The
MISRA C2004 Guidlines. I have to create violations for every rule and
find out what Lint has to say about them. Therefore a simple typedef
such as

typedef char char_t

is required or an earlier rule will be violated.

Mar 29 '06 #5
"ha********@goo glemail.com" <ha********@goo glemail.com> wrote:
Ian Collins wrote:
ha********@goog lemail.com wrote:
typedef char char_t;

Why?


I am testing how well PC-LINT performs in detecting Violations of The
MISRA C2004 Guidlines. I have to create violations for every rule and
find out what Lint has to say about them. Therefore a simple typedef
such as

typedef char char_t

is required or an earlier rule will be violated.


*Boggle* MISRA C does not let you use normal C integer types, but
_requires_ them to be put behind an obfuscatory and completely useless
typedef? It defies belief... but if so, that ruleset is just wrong.

Richard
Mar 31 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
25429
by: sneill | last post by:
How is it possible to take the value of a variable (in this case, MODE_CREATE, MODE_UPDATE, etc) and use that as an object property name? In the following example I want 'oIcon' object to have the properties: mode1, mode2, and mode3. This seems simple but I can't quite figure it out... Any ideas anyone?
34
5968
by: Chris Hills | last post by:
MISRA is looking at doing a MISRA-C++ This is because they were asked to do it by a lot of people in industry. They are looking for some people to make up the team (based in the UK) The team will be made up of experienced industrial high-integrity C++ sw engineers. They are looking at meeting once a month for about a year. -- \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
145
6360
by: Sidney Cadot | last post by:
Hi all, In a discussion with Tak-Shing Chan the question came up whether the as-if rule can cover I/O functions. Basically, he maintains it can, and I think it doesn't. Consider two programs: /*** a.c ***/
7
3160
by: Edward Yang | last post by:
A few days ago I started a thread "I think C# is forcing us to write more (redundant) code" and got many replies (more than what I had expected). But after reading all the replies I think my question about local variable initialization is still not solved. And some of the replies forked into talking about out parameters. And the thread is becoming way too deep. So I open a new thread here. My question in the previous thead has turned...
9
2476
by: ziman137 | last post by:
Hi all, The results from following codes got me a bit confused. #include <stdio.h> #include <iostream> using namespace std; struct A {
13
3168
by: Mike S | last post by:
I came across the following paragraph in the "Semantics" section for simple assignment in N1124 (C99 draft) and I'm wondering if I'm interpreting it right: 6.5.16.1p3: If the value being stored in an object is read from another object that overlaps in any way the storage of the first object, then the overlap shall be exact and the two objects shall
18
2573
by: Chris Hills | last post by:
The MISRA-C team has to make a decision: should it move from Referencing C95 (9899:1990+A1+RC1+TC2) to referencing C99 for the next MISRA-C (version 3) In the real world (especially embedded, safety-critical and high-integrity circles) there are no C99 compilers in use as on September 2007. They are C95+. Any thoughts from anyone involved in writing compilers? Either to the NG or to my email address. Yes, I have asked most of the...
6
2501
by: Ark Khasin | last post by:
MISRA came up with those "underlying types" of sub-int size (like likely char and perhaps short) and the whole arithmetic on them. Basically, I need to continually cast back to the "underlying" type "as if" the computations were done on them without promotion. E.g., uint16_t a, b; a = (uint16_t)(~b); //RH would not approve of it instead of a = ~b; On the one hand, I find myself utterly unable to read my own MISRA-compliant code written...
0
10778
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10538
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10210
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7750
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6951
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5622
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5788
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4419
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3077
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.