Recent

Author Topic: Application crash after Line: 32  (Read 833 times)

paule32

  • Sr. Member
  • ****
  • Posts: 280
Application crash after Line: 32
« on: February 29, 2024, 11:17:26 pm »
Hello,
the Code above produce me an Application Crash after Line: 32.
How can I fix this ?
The Code, I use it under Windows 10 64-Bit.

Code: Pascal  [Select][+][-]
  1. procedure fpc_ansistr_assign(var DestS: Pointer; S2: Pointer); [public, alias: 'FPC_ANSISTR_ASSIGN']; compilerproc;
  2. begin
  3.     MessageBox(0, LPCSTR( S2 ), '0 0 0 0 0', 0);
  4.     DestS := malloc( 200 );
  5. {$ASMMODE INTEL}
  6.     asm
  7.     mov rsi, S2             // src string
  8.     mov rdi, DestS          // dst string
  9.    
  10.     xor rax, rax            // counter for string length
  11.     @findLength:
  12.     cmp byte ptr [rsi + rax], 0
  13.     je @copyInit            // null terminator
  14.     inc rax
  15.     jmp @findLength
  16.        
  17.     @copyInit:
  18.     mov rcx, rax            // rcx now holds the length of the string
  19.    
  20.     @copyLoop:
  21.     test rcx, rcx           // check if RCX is empty
  22.     jz @endCopy             // when yes => done.
  23.    
  24.     dec rcx
  25.     mov al, [rsi + rcx]     // load byte from src in AL
  26.     mov [rdi + rcx], al     // store byte into dst
  27.     jmp @copyLoop           // to next charcter
  28.        
  29.     @endCopy:
  30.     mov byte ptr [rdi + rax], 0  // null-terminated dst string
  31.     end;
  32.     MessageBox(0, LPCSTR( S2 ), '1 1 0 1 0', 0);
  33.     MessageBox(0, LPCSTR( DestS ), '0 0 0 0 0', 0);
  34. end;
  35.  

rvk

  • Hero Member
  • *****
  • Posts: 6572
Re: Application crash after Line: 32
« Reply #1 on: February 29, 2024, 11:30:28 pm »
fpc_ansistr_assign is normally an essential part of the RTL.

In that procedure you can messagebox multiple times.
Are you absolutely sure those functions don't call fpc_ansistr_assign themselves because then you get an infinite loop.

Remove the messageboxes.
Does it still crash?
If so... On what line?

paule32

  • Sr. Member
  • ****
  • Posts: 280
Re: Application crash after Line: 32
« Reply #2 on: February 29, 2024, 11:34:13 pm »
the Function will not call it'self.
The first, and second MessageBox will be pop out on screen.
So, there is no Loop.

But the DestS seems be not valide

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2269
  • Fifty shades of code.
    • Delphi & FreePascal
Re: Application crash after Line: 32
« Reply #3 on: February 29, 2024, 11:45:50 pm »
I just transaled your asm back to pascal to find out things, here it works okay.
Code: Pascal  [Select][+][-]
  1. procedure FPC_ANSISTR_ASSIGN(var DestS: PChar; S2: PChar);
  2. var
  3.  Len: Integer;
  4. begin
  5.  MessageBox(0, S2, '0 0 0 0 0', 0);
  6.  GetMem(DestS, 200);
  7.  try
  8.     Len := 0;
  9.     while S2[Len] <> #0 do
  10.       Inc(Len);
  11.     Move(S2^, DestS^, Len);
  12.     DestS[Len] := #0;
  13.  finally
  14. //    FreeMem(DestS); // must be done outside else the pointer is garbage after method runs out of scope
  15.  end;
  16.  MessageBox(0, S2, '1 1 0 1 0', 0);
  17.  MessageBox(0, DestS, '0 0 0 0 0', 0);
  18. end;
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

jamie

  • Hero Member
  • *****
  • Posts: 6734
Re: Application crash after Line: 32
« Reply #4 on: March 01, 2024, 01:05:59 am »
Lots of holes on that ASM code.... it's like the slit experiment!
The only true wisdom is knowing you know nothing

ASerge

  • Hero Member
  • *****
  • Posts: 2336
Re: Application crash after Line: 32
« Reply #5 on: March 01, 2024, 01:57:37 am »
Lots of holes on that ASM code.... it's like the slit experiment!
I agree. Better FPC code:
Code: Pascal  [Select][+][-]
  1. procedure FPC_ANSISTR_ASSIGN(out DestS: PAnsiChar; S2: PAnsiChar);
  2. var
  3.   Len: SizeInt;
  4. begin
  5.   DestS := nil;
  6.   if S2 = nil then
  7.     Exit;
  8.   Len := IndexByte(S2^, -1, 0);
  9.   if Len > 0 then // the only #0 is ignored
  10.   begin
  11.     Inc(Len); // + #0
  12.     GetMem(DestS, Len);
  13.     Move(S2^, DestS^, Len);
  14.   end;
  15. end;

 

TinyPortal © 2005-2018