473,320 Members | 1,902 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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?tion?"*/

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 6416
"ha********@googlemail.com" <ha********@googlemail.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?tion?"*/
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********@googlemail.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?tion?"*/

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********@googlemail.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********@googlemail.com" <ha********@googlemail.com> wrote:
Ian Collins wrote:
ha********@googlemail.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
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...
34
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...
145
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...
7
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...
9
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
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...
18
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,...
6
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"...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.