CodeGuard shows buffer overrun when adding chars to AnsiString

I am trying to use CodeGuard to double check my always perfect code ;) and I am getting a very intriguing result. In one place I am attempting to copy several fields of information from a database into an ansistring. It looks like this:

s.sprintf("%10.10s %12.12s", Table1date->AsString, Table1tcode->AsString);

The date field is 10 characters long, the tcode field is 15 characters long (misdefined in the table - should have been twelve).
This is the error I get:

Error 00372. 0x130410 (Thread 0x0D38):
Access overrun: Attempt to access 12 byte(s) at 0x0346944C, that is at offset
 8 in heap block 0x03469444 which is only 18 bytes long.
vsnprintf(0x00000000, 0x0 [0], 0x00581D7D ["%-10.10s %12.12s %10.2lf %10.2lf
 %10.2lf %10ld %c %10ld"], 0x0012F350)

Call Tree:
   0x0041594C(=probill.exe:0x01:01494C) C:\Program Files\Borland\CBuilder6\Projects\probill\Unit13.cpp#63

The memory block (0x03469444) [size: 18 bytes] was allocated with SysGetMem
Call Tree:

Any ideas?

PS - the program runs fine, displays everything, and doesn't present overrun behavior...

Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

CayceConnect With a Mentor Commented:
Shouldn't it be:

s.sprintf("%10.10s %12.12s", Table1date->AsString.c_str(), Table1tcode->AsString.c_str());
George TokasCommented:
CodeGuard is displaying EVERY error it finds...
The only idea I have to propose is to make a debug build and run the app. and see the results after as first step ...
Second add a breakpoint where you imagine there is the error and after the app is paused bring up the CPU window and check it out...
The error CodeGuard reports maybe isn't a serious one to crash your app. but maybe it could cause a resource leak and after a while crash your app....

barrydbrownAuthor Commented:
I did that. The error occurs in the s.sprintf line. I tried using s.SetLength(255) to make sure s wasn't being set to a short length somewhere before. I also tried using s.cat_sprintf. s is a local variable that I use several times. I am using a debug build and I am using CodeGuard to try to be sure I don't have any resource leaks. I guess I'll just have to pull the routine out and make a test program to see if I can figure this out.

George TokasCommented:
>>s.sprintf("%10.10s %12.12s", Table1date->AsString, Table1tcode->AsString);
As I understand you want to copy Table1date->AsString and Table1tcode->AsString to s(ansistring)...
Why don't you try:
s = Table1date->AsString + Table1tcode->AsString;
This works usualy without set length...

I would recommend to Accept: Cayce  {http:#13971483}
All Courses

From novice to tech pro — start learning today.