Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


[Delphi] Crash at "SetLength(...)"

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
penpenpen
Cheater
Reputation: 0

Joined: 23 Feb 2014
Posts: 39

PostPosted: Thu Oct 09, 2014 4:02 pm    Post subject: [Delphi] Crash at "SetLength(...)" Reply with quote

Hi,

I stumbled upon a problem while experimenting.
I hope we have some delphi experts here who can help Smile.

So Basicly what I'm doing is this:
(The Code is executed within a DLL)

Declarations:
Code:

var
  POldCode:Pointer;     
  NewCode:TBytes;
  OldCode:TBytes;


Code:
   
 setlength(oldcode,5);
 Move(POldCode^, OldCode, sizeof(oldcode));   

... Then this code is executed
Code:

   NewCode := CreateByteArrFromHex('C7 46 58 25 00 00 00 C7 46 54 25 00 00 00');
   MeltByteArray(NewCode,OldCode);   


And in the procedure MeltByteArray it crashes when setlength is called:
Code:

procedure MeltByteArray(var a,b:TBytes);
var
  Old: Integer;
begin
   old := Length(a);
   SetLength(a,old+Length(b)); //Crash
   Move(b[0],a[old],Length(b)*SizeOf(b[0]));
end; 


Not Important but this is my CreateByteArrayFromHex Function:
Code:
function CreateByteArrFromHex(ByteArr:String):TBytes;
var
  i:integer;
begin
  //Filter Leerzeichen
  ByteArr:=StringReplace(ByteArr,' ','',[rfReplaceAll]);
  i:=1; //String beginnt bei Index1
  while i < length(ByteArr) do
  begin
    SetLength(Result,high(Result)+2);
    Result[high(Result)] := HexToInt(Copy(ByteArr,i,2));
    i:=i+2;
  end;
end;     


I think it may have something to do with the Move Function which changes some references. But I'm not sure.

Hope you can help me Smile
Back to top
View user's profile Send private message
Dark Byte
Site Admin
Reputation: 470

Joined: 09 May 2003
Posts: 25788
Location: The netherlands

PostPosted: Thu Oct 09, 2014 4:29 pm    Post subject: Reply with quote

Code:

 setlength(oldcode,5);
 Move(POldCode^, OldCode, sizeof(oldcode));   


you're never setting POldCode to @oldcode[0] after the setlength (setlength changes the location of the array) , so POldCode will be pointing to some random memory location(worst case scenario it points to the old array that has a length of 0). Causing the move to most likely overwrite random memory in the stack. (basically, anything weird can happen from this point)

_________________
Do not ask me about online cheats. I don't know any and wont help finding them.

Like my help? Join me on Patreon so i can keep helping
Back to top
View user's profile Send private message MSN Messenger
penpenpen
Cheater
Reputation: 0

Joined: 23 Feb 2014
Posts: 39

PostPosted: Thu Oct 09, 2014 4:37 pm    Post subject: Reply with quote

//Edit:

I got it working Smile.
It was a problem with the move proc. I just replaced it by CopyMemory. And it works. I still dont really understand why though. But I will find out Smile

Thanks DarkByte
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites