Solved

What is the Problem in this code?

Posted on 2002-05-28
10
131 Views
Last Modified: 2010-04-04
Hi guys,

I am not understanding where i am doing mistake in my code. I will give code snippet and try to explain what i am doing here. And Please tell me what is the mistake in this code.
 procedure CopyChars(src:PChar;var dest:Pchar;pos:Integer;count:Integer);
procedure Parse( strInputBuffer:PChar; streamLength:LongInt; var strOutputBuffer:PChar; var errorCode:Integer);


procedure CopyChars(src:PChar;var dest:Pchar;pos:Integer;count:Integer);
var
local:PChar;
local1:Pchar;
begin
     try
     local:=src;

     if dest = nil then
     begin
          GetMem(dest,count+1);
     end
     else
     begin
           ReAllocMem(dest,count+1);
     end;

     local1:=dest;
     try
         Move(src[pos],dest^,count);
     except
     on error : exception do
     begin
     showmessage(error.message);
//        showmessage('error raised here');
     end;
     end;
     inc(dest,count+1);
     dest^:=chr(0);

     dest:=local1;
     src:=local;


    except
       on error: exception do showmessage('Error in copy chars : '+error.message);
    end;


end;

in procedue Parse()

while true do
begin
 try
//begin of code
// some code
                                     CopyChars(strInputBuffer,strFilter, index,  counter-index);
                                       strFilterCode := strFilter;
                                       except

                                       end;

// and other code
end;

Case 1
when my parse procedure calls the copychars for the first time, code is working fine. But when it calls for second time. Copychars procedure reallocates the memory for destination pointer and try to copy the data from the source to destination pointer, It is giving Illegal Pointer access exception. I am not understanding, why it is happening like that
I tried to allocate or reallocate the memory external to copychars procedure also. But same exception was raised by the program.


Case 2.

As above is giving problems

i Used GetMem and FreeMem procedures also. When I do so for the first time itself it is giving problem

Case 3
I used SysAllocMem,SysGetMem,SysRealloc,SysFreeMem procedues also. But no use.


all above cases itried with in procedure and external to procedure
by creating variables as class members ./ by creating procedure variables
 
***************************************************************************************************************
I am not understanding  where i am doing problem. Please help me. If u identify the mistake in my code
***************************************************************************************************************
Thanks
Koundinya
0
Comment
Question by:sudhakar_koundinya
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 11

Accepted Solution

by:
robert_marquardt earned 50 total points
Comment Utility
inc(dest,count+1);
dest^:=chr(0);

This is 1 Byte beyond end of buffer.

Is is possible that it crashes because the source string is too short? You are copying count bytes which may be beyond buffer end of source.
0
 
LVL 11

Expert Comment

by:robert_marquardt
Comment Utility
inc(dest,count+1);
dest^:=chr(0);

Oops, i have to rethink that.
0
 
LVL 27

Expert Comment

by:kretzschmar
Comment Utility
hmmm,

robert i think you're on the right track,
as the pointer is moved by this increment
and is defined as var parameter which affect the var of the caller, so that it could be,
that the next call violates

just a guess

meikl ;-)
0
 
LVL 14

Author Comment

by:sudhakar_koundinya
Comment Utility
Thanks Robert and meikel fro ur quick response




inc(dest,count+1);
dest^:=chr(0); // i want to treat this as end of string


Move(src[pos],dest^,count);

Just tell me in this case it copies count no of bytes or count+1 bytes

if it is count + 1 bytes as Robert expected i may be  going wrong.

Thanks again and waiting for ur quick response

Sudhakar Koundinya
0
 
LVL 14

Author Comment

by:sudhakar_koundinya
Comment Utility
and i want to know which is faster


Move(src[pos],dest^,count);

         or

dest^:src[pos];
inc(dest,count);
dest^:=chr(0)// end of string

If i do like this will it effect the source stream ?


Thanks
Koundinya
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 11

Expert Comment

by:robert_marquardt
Comment Utility
The information we need are the parameter values of the first and second call.
I suspect that you call it
Pos=5, Count=30
Pos=10, Count=30
That is count does not decrease whereas Pos increases.
If so you get the overflow on the source string when you copy bytes outside the source string.
0
 

Expert Comment

by:leus
Comment Utility

Why don't just use Copy()?

0
 
LVL 14

Author Comment

by:sudhakar_koundinya
Comment Utility
Hi leus

I can use Copy


But Before using that, i want to know wheteher the copy function treats null (Ascii 0) charachter as termination point of string or not.(It is string handling routine) If it treats like that there is no use for me. Because my program totally depends on binary streams

In that case I have to depend either of following procedures

Move(src[pos],dest^,count);

        or

dest^:src[pos];
inc(dest,count);
dest^:=chr(0)// end of string

But I don't Know Which will work fast

Thanks
Koundinya
0
 

Expert Comment

by:leus
Comment Utility

I always prefer to use built in functions instead of provide replacements, because I'll never know if I'm doing something wrong with certain things (like different operating systems, etc.) So my bet is for Move() instead of your second solution.

0
 
LVL 14

Author Comment

by:sudhakar_koundinya
Comment Utility
Thanks Robert.
You thought in right way

Koundinya

0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now