Board index » cppbuilder » CodeGuard error not understood

CodeGuard error not understood


2008-03-25 09:23:22 PM
cppbuilder11
Hi Folks,
I am getting a CodeGuard error report in a place where I cannot see any
error in my code and where it works.
Extract from function body:
static char item[100]; char tmp[20];
memset( item, ' ', 98 ); item[98] = '\0'; // setting spaces and terminator
memcpy( item + <const == 12>, <function returning Char*, len 10>, 10 ); //
CG error here
sprintf( tmp, "%10.2f", <float value>);
memcpy( item + <const == 30>, tmp, strlen(tmp) ); CG error here as well
The offset constants are defined by #defines elsewhere as an easy way of
controlling the layout of item built up from a number of things. (I know
there are 'more modern?' ways of doing this but as 'it wasn't broke I didn't
intend to fix it'!)
CG says I am trying to access 100 bytes from 'item + ...' when item is only
100 bytes, BUT I am only trying to access 10 bytes!? The result if I press
on from the errors is correct.
The above code, and many similar examples, has been in use for several
versions of BCB and CG did not report any errors in BCB6.
Is there something wrong with this code that was not wrong before?
Or is this a bug in CodeGuard, in not understanding memcpy function??
The CG errors do not occur if I remove the 'static' or define item as, e.g.,
300 bytes.
Derek
 
 

Re:CodeGuard error not understood

"Derek Harvey" < XXXX@XXXXX.COM >wrote:
Quote
Hi Folks,
I am getting a CodeGuard error report in a place where I cannot see any
error in my code and where it works.
Extract from function body:
static char item[100]; char tmp[20];
memset( item, ' ', 98 ); item[98] = '\0'; // setting spaces and terminator
You don't know for sure whether the problem is with CodeGuard or with the address calculation.
My advice here: Assign the address calculation result to a pointer and see what it is.
char *temp_ptr;
temp_ptr = item + <const == 12>;
cout << "item address = " << hex << item;
cout << "item offset addr = " << hex << temp_ptr;
Also, move this to a separate line:
<function returning Char*, len 10>
and print this out as well.
Quote
memcpy( item + <const == 12>, <function returning Char*, len 10>, 10 ); //
CG error here
sprintf( tmp, "%10.2f", <float value>);
memcpy( item + <const == 30>, tmp, strlen(tmp) ); CG error here as well

The offset constants are defined by #defines elsewhere as an easy way of
controlling the layout of item built up from a number of things. (I know
there are 'more modern?' ways of doing this but as 'it wasn't broke I didn't
intend to fix it'!)

CG says I am trying to access 100 bytes from 'item + ...' when item is only
100 bytes, BUT I am only trying to access 10 bytes!? The result if I press
on from the errors is correct.
The above code, and many similar examples, has been in use for several
versions of BCB and CG did not report any errors in BCB6.

Is there something wrong with this code that was not wrong before?
Or is this a bug in CodeGuard, in not understanding memcpy function??

The CG errors do not occur if I remove the 'static' or define item as, e.g.,
300 bytes.

Derek


 

Re:CodeGuard error not understood

You didn't say what version you are using. We've had significant trouble
with CodeGuard in bds2006; its been enough months now since I've even tried
to use it that I don't really recall specifics. But my general recollection
is that compiling with CodeGuard enabled seemed about as likely to cause
otherwise unexplainable errors, as it was to help find them. That said, it
has helped occasionally, but its also had us running in circles a few times.
"Derek Harvey" < XXXX@XXXXX.COM >wrote in message
Quote
Hi Folks,
I am getting a CodeGuard error report in a place where I cannot see any
error in my code and where it works.
Extract from function body:
static char item[100]; char tmp[20];
memset( item, ' ', 98 ); item[98] = '\0'; // setting spaces and terminator
memcpy( item + <const == 12>, <function returning Char*, len 10>, 10 ); //
CG error here
sprintf( tmp, "%10.2f", <float value>);
memcpy( item + <const == 30>, tmp, strlen(tmp) ); CG error here as well

The offset constants are defined by #defines elsewhere as an easy way of
controlling the layout of item built up from a number of things. (I know
there are 'more modern?' ways of doing this but as 'it wasn't broke I
didn't intend to fix it'!)

CG says I am trying to access 100 bytes from 'item + ...' when item is
only 100 bytes, BUT I am only trying to access 10 bytes!? The result if I
press on from the errors is correct.
The above code, and many similar examples, has been in use for several
versions of BCB and CG did not report any errors in BCB6.

Is there something wrong with this code that was not wrong before?
Or is this a bug in CodeGuard, in not understanding memcpy function??

The CG errors do not occur if I remove the 'static' or define item as,
e.g., 300 bytes.

Derek

 

{smallsort}

Re:CodeGuard error not understood

"Derek Harvey" < XXXX@XXXXX.COM >wrote in message
Quote
The offset constants are defined by #defines elsewhere as an
easy way of controlling the layout of item built up from a
number of things.
The #define statements may be the problem.
Quote
CG says I am trying to access 100 bytes from 'item + ...' when
item is only 100 bytes
Then you really are trying to access 100 bytes from the starting position.
Quote
BUT I am only trying to access 10 bytes!?
I am guessing that your #define values are being changed from 10 to 100
without your knowledge. Do you see any compiler warnings saying something
like "definition of ... is different..."? I would suggest you change your
#define statements into const variables instead, and then see if the problem
continues.
Gambit
 

Re:CodeGuard error not understood

"Remy Lebeau (TeamB)" < XXXX@XXXXX.COM >wrote in message
Quote
Do you see any compiler warnings saying something like
"definition of ... is different..."?
Sorry, I meant this: "Redefination of .. is not identical"
Gambit
 

Re:CodeGuard error not understood

I am getting this error with BCB2007; sorry if I omitted to say this.
The identical code did not upset CodeGuard when I was using BCB6 (I never
went to 2006).
If I hover over the constant with a breakpoint at the 'offending' line it
shows the correct value (10 or 12).
I have not seen a #define redefinition warning message. (These defines are
in the same unit and the names are unique in my project)
The result of the memcpy is correct, i.e., the format of the things inserted
into 'item' char array is as expected.
The 'error' does not arise if the char array is made NOT static.
The above remarks lead me again to think it is a CodeGuard error.
I work round the problem by making the char array>220 in size and CG is
happy with no other code changes.
Derek
"Remy Lebeau (TeamB)" < XXXX@XXXXX.COM >wrote in message
Quote

"Remy Lebeau (TeamB)" < XXXX@XXXXX.COM >wrote in message
news:47e92f14$ XXXX@XXXXX.COM ...

>Do you see any compiler warnings saying something like
>"definition of ... is different..."?

Sorry, I meant this: "Redefination of .. is not identical"


Gambit


 

Re:CodeGuard error not understood

"Derek Harvey" < XXXX@XXXXX.COM >writes:
Quote
I am getting this error with BCB2007; sorry if I omitted to say this.
The identical code did not upset CodeGuard when I was using BCB6 (I never
went to 2006).
If I hover over the constant with a breakpoint at the 'offending' line it
shows the correct value (10 or 12).
I have not seen a #define redefinition warning message. (These defines are
in the same unit and the names are unique in my project)
The result of the memcpy is correct, i.e., the format of the things inserted
into 'item' char array is as expected.
The 'error' does not arise if the char array is made NOT static.
The above remarks lead me again to think it is a CodeGuard error.
Hello Derek,
Hard to say without seeing the source in question. Attempting to
duplicate what you described doesn't trigger an error:
Regards,
Lee
----
#include <string.h>
#include <stdio.h>
char *foo()
{
return "0123456789";
}
#pragma argsused
int main(int argc, char* argv[])
{
static char item[100];
char tmp[20];
memset(item, ' ', 98);
item[98] = 0;
memcpy(item+12, foo(), 10);
sprintf(tmp, "10.2f", 3.1415f);
memcpy(item+30, tmp, strlen(tmp));
return 0;
}
 

Re:CodeGuard error not understood

Hi Remy,
I tried replacing the define thing by the literal number, e.g., 12, and CG
still 'failed' the memcpy BUT the memcpy does the correct thing..
If I do this:
char* cptr = item + <#define thing>;
memcpy ( cptr, <shortstring>, len(shortstring>); // CG is happy.
The following is a piece of code from another function which was 'failing',
and shows the above fix.
================
static char item[200], q[20]; char* cptr;
memset( item, ' ', 100 ); item[100] = '\0';
memcpy( item, rollname.c_str(), rollname.Length() ); // rollname is
function argument
if ( qual == 2 ) {
cptr = item + CU_DIVI_QPOS1;
memcpy( cptr, "Excluded ", 9 ); // now no CG error
//memcpy(item + CU_DIVI_QPOS1, "Excluded ", 9 ); // original code, CG
error but result correct
}
sprintf( q, "%10.2f", shares );
cptr = item + CU_DIVI_QPOS2;
memcpy( cptr, q, strlen( q ) );
//memcpy( item + CU_DIVI_QPOS2, q, strlen( q ) ); // original code, CG
error
sprintf( q, "%10.2f", divi );
cptr = item + CU_DIVI_QPOS3;
memcpy( cptr, q, strlen( q ) );
//memcpy( item + 60, q, strlen( q ) ); // original changed to literal
offset, still CG error
item[ CU_DIVI_QPOS3 + 14 ] = '\0';
=============
As noted in earlier post, the memcpy is always producing the correct result,
the char array is NOT overrun.
CG says in this case 'attempting to access 200 bytes at <address>+ <offset
value reported correctly>.
It still seems to me that CG does not understand memcpy.
Why did the original code not upset CG in BCB6?
Derek
"Remy Lebeau (TeamB)" < XXXX@XXXXX.COM >wrote in message
Quote

"Derek Harvey" < XXXX@XXXXX.COM >wrote in message
news: XXXX@XXXXX.COM ...

>The offset constants are defined by #defines elsewhere as an
>easy way of controlling the layout of item built up from a
>number of things.

The #define statements may be the problem.

>CG says I am trying to access 100 bytes from 'item + ...' when
>item is only 100 bytes

Then you really are trying to access 100 bytes from the starting position.

>BUT I am only trying to access 10 bytes!?

I am guessing that your #define values are being changed from 10 to 100
without your knowledge. Do you see any compiler warnings saying something
like "definition of ... is different..."? I would suggest you change your
#define statements into const variables instead, and then see if the
problem continues.


Gambit

 

Re:CodeGuard error not understood

"Derek Harvey" < XXXX@XXXXX.COM >writes:
Quote
As noted in earlier post, the memcpy is always producing the correct result,
the char array is NOT overrun.
CG says in this case 'attempting to access 200 bytes at <address>+ <offset
value reported correctly>.
It still seems to me that CG does not understand memcpy.
Why did the original code not upset CG in BCB6?
Hello Derek,
Hard to say without seeing code that fails. Can you post some source
that fails (I tried to reproduce your problem in my other response but
wasn't able to). Or failing that show us the assembly generated
(using -S).
Regards,
Lee