CodeGuard shows buffer overrun when adding chars to AnsiString

Posted on 2005-03-20
Medium Priority
Last Modified: 2013-11-17
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...

Question by:barrydbrown
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 16

Expert Comment

by:George Tokas
ID: 13590334
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....


Author Comment

ID: 13590770
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.

LVL 16

Expert Comment

by:George Tokas
ID: 13591017
>>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...


Accepted Solution

Cayce earned 2000 total points
ID: 13971483
Shouldn't it be:

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

Expert Comment

ID: 15756078
I would recommend to Accept: Cayce  {http:#13971483}

Featured Post

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question