View Issue Details

IDProjectCategoryView StatusLast Update
0038337FPCCompilerpublic2021-01-10 18:39
ReporterMartin Friebe Assigned ToJonas Maebe  
PrioritynormalSeverityminorReproducibilityalways
Status closedResolutionfixed 
PlatformLinuxOSUbuntu 
Product Version3.2.1 
Fixed in Version3.2.1 
Summary0038337: Crashes with wrongly freed ansi strings in 3.2.1 fixes / ok in trunk
DescriptionThe Code below (its just copied and pasted from the LCL, to include the parts needed to trigger the issue)...
It works fine in 3.0.4 and in trunk.

It wrongly frees strings in 3.2.0 and 3.2.1
The errors in 3.2.1 are different from 3.2.0, leading to believe that part of the fix may have been merged.
(Some errors can also be observed on Windows, only tested 3.2.0)

Valgrind log attached.

One of the traces suggests that for
function GetCodePoint(const S: String; const Index: PtrInt): string;
the same variable is passed for "s" and "result", as the memory for "s" is freed in "result := '';".

fpc -B -MObjFPC -Scghi -Cg -O1 -gw3 -gl -gv -l -vewnhibq -al project1.lpr

Steps To Reproduceprogram project1;
{$mode objfpc}{$H+}

function GetCodePoint(const S: String; const Index: PtrInt): string;
//equivalent for Result := S[Index], but for Utf8 encoded strings
var
  p: PChar;
  PLen: PtrInt;
  Res: AnsiString; //intermediate needed for PChar -> String -> ShortString assignement
begin
  Result := '';
    writeln(index, ' // ', length(s), ' // ');

  p := @s[1 + (index and 3) ];
  PLen := 1;
  Res := p;
  SetLength(Res,PLen);
  Result := Res;
end;


function UTF8Length(p: PChar; ByteCount: PtrInt): PtrInt;
var
  CharLen: LongInt;
begin
  Result:=0;
  while (ByteCount>0) do begin
    if byte(p^)<192 then
      inc(result);
    inc (p);
    dec(ByteCount);
  end;
end;

function UTF8Length(const s: string): PtrInt; inline;
begin
  Result:=UTF8Length(PChar(s),length(s));
end;

procedure foo(v: string);
var
  s: ShortString;
  i: Integer;
begin
  s := v;
  for i := 0 to UTF8Length(s) do begin
    if GetCodePoint(s,i) = 0000001 then writeln;
  end;
  writeln(v);
  v := 'a';
end;

var v1: string;
begin
  v1 := copy('ahtnoeuouauec',2,11);
  foo(v1);
  readln;
end.
Additional Informationalgrind-3.15.0 and LibVEX; rerun with -h for copyright info
==88588== Command: ./project1
==88588==
==88588== Invalid read of size 8
==88588== at 0x4012BB: P$PROJECT1_$$_GETCODEPOINT$ANSISTRING$INT64$$ANSISTRING (project1.lpr:12)
==88588== by 0x401525: P$PROJECT1_$$_FOO$ANSISTRING (project1.lpr:47)
==88588== by 0x401638: main (project1.lpr:56)
==88588== Address 0x4a520c8 is 24 bytes inside a block of size 44 free'd
==88588== at 0x483CA3F: free (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==88588== by 0x42E22F: CMEM_$$_CFREEMEM$POINTER$$QWORD (cmem.pp:75)
==88588== by 0x41D902: SYSTEM_$$_FREEMEM$POINTER$$QWORD (heap.inc:324)
==88588== by 0x40DB54: fpc_ansistr_decr_ref (astrings.inc:149)
==88588== by 0x40DBD5: fpc_ansistr_assign (astrings.inc:186)
==88588== by 0x401280: P$PROJECT1_$$_GETCODEPOINT$ANSISTRING$INT64$$ANSISTRING (project1.lpr:11)
==88588== by 0x401525: P$PROJECT1_$$_FOO$ANSISTRING (project1.lpr:47)
==88588== by 0x401638: main (project1.lpr:56)
==88588== Block was alloc'd at
==88588== at 0x483B7F3: malloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==88588== by 0x42E1E8: CMEM_$$_CGETMEM$QWORD$$POINTER (cmem.pp:62)
==88588== by 0x41D7E9: SYSTEM_$$_GETMEM$POINTER$QWORD (heap.inc:284)
==88588== by 0x40DAAC: SYSTEM_$$_NEWANSISTRING$INT64$$POINTER (astrings.inc:115)
==88588== by 0x40EDB2: fpc_ansistr_setlength (astrings.inc:776)
==88588== by 0x40E625: fpc_shortstr_to_ansistr (astrings.inc:510)
==88588== by 0x4014AC: P$PROJECT1_$$_FOO$ANSISTRING (project1.lpr:46)
==88588== by 0x401638: main (project1.lpr:56)
==88588==
0 // 11 //
==88588== Invalid read of size 1
==88588== at 0x40E6F0: fpc_pchar_to_ansistr (astrings.inc:553)
==88588== by 0x401320: P$PROJECT1_$$_GETCODEPOINT$ANSISTRING$INT64$$ANSISTRING (project1.lpr:16)
==88588== by 0x401525: P$PROJECT1_$$_FOO$ANSISTRING (project1.lpr:47)
==88588== by 0x401638: main (project1.lpr:56)
==88588== Address 0x4a520d0 is 32 bytes inside a block of size 44 free'd
==88588== at 0x483CA3F: free (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==88588== by 0x42E22F: CMEM_$$_CFREEMEM$POINTER$$QWORD (cmem.pp:75)
==88588== by 0x41D902: SYSTEM_$$_FREEMEM$POINTER$$QWORD (heap.inc:324)
==88588== by 0x40DB54: fpc_ansistr_decr_ref (astrings.inc:149)
==88588== by 0x40DBD5: fpc_ansistr_assign (astrings.inc:186)
==88588== by 0x401280: P$PROJECT1_$$_GETCODEPOINT$ANSISTRING$INT64$$ANSISTRING (project1.lpr:11)
==88588== by 0x401525: P$PROJECT1_$$_FOO$ANSISTRING (project1.lpr:47)
==88588== by 0x401638: main (project1.lpr:56)
==88588== Block was alloc'd at
==88588== at 0x483B7F3: malloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==88588== by 0x42E1E8: CMEM_$$_CGETMEM$QWORD$$POINTER (cmem.pp:62)
==88588== by 0x41D7E9: SYSTEM_$$_GETMEM$POINTER$QWORD (heap.inc:284)
==88588== by 0x40DAAC: SYSTEM_$$_NEWANSISTRING$INT64$$POINTER (astrings.inc:115)
==88588== by 0x40EDB2: fpc_ansistr_setlength (astrings.inc:776)
==88588== by 0x40E625: fpc_shortstr_to_ansistr (astrings.inc:510)
==88588== by 0x4014AC: P$PROJECT1_$$_FOO$ANSISTRING (project1.lpr:46)
==88588== by 0x401638: main (project1.lpr:56)
==88588==
==88588== Invalid read of size 16
==88588== at 0x401CB7: SYSTEM_$$_INDEXBYTE$formal$INT64$BYTE$$INT64 (x86_64.inc:490)
==88588== by 0x40E710: fpc_pchar_to_ansistr (astrings.inc:556)
==88588== by 0x401320: P$PROJECT1_$$_GETCODEPOINT$ANSISTRING$INT64$$ANSISTRING (project1.lpr:16)
==88588== by 0x401525: P$PROJECT1_$$_FOO$ANSISTRING (project1.lpr:47)
==88588== by 0x401638: main (project1.lpr:56)
==88588== Address 0x4a520d0 is 32 bytes inside a block of size 44 free'd
==88588== at 0x483CA3F: free (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==88588== by 0x42E22F: CMEM_$$_CFREEMEM$POINTER$$QWORD (cmem.pp:75)
==88588== by 0x41D902: SYSTEM_$$_FREEMEM$POINTER$$QWORD (heap.inc:324)
==88588== by 0x40DB54: fpc_ansistr_decr_ref (astrings.inc:149)
==88588== by 0x40DBD5: fpc_ansistr_assign (astrings.inc:186)
==88588== by 0x401280: P$PROJECT1_$$_GETCODEPOINT$ANSISTRING$INT64$$ANSISTRING (project1.lpr:11)
==88588== by 0x401525: P$PROJECT1_$$_FOO$ANSISTRING (project1.lpr:47)
==88588== by 0x401638: main (project1.lpr:56)
==88588== Block was alloc'd at
==88588== at 0x483B7F3: malloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==88588== by 0x42E1E8: CMEM_$$_CGETMEM$QWORD$$POINTER (cmem.pp:62)
==88588== by 0x41D7E9: SYSTEM_$$_GETMEM$POINTER$QWORD (heap.inc:284)
==88588== by 0x40DAAC: SYSTEM_$$_NEWANSISTRING$INT64$$POINTER (astrings.inc:115)
==88588== by 0x40EDB2: fpc_ansistr_setlength (astrings.inc:776)
==88588== by 0x40E625: fpc_shortstr_to_ansistr (astrings.inc:510)
==88588== by 0x4014AC: P$PROJECT1_$$_FOO$ANSISTRING (project1.lpr:46)
==88588== by 0x401638: main (project1.lpr:56)
==88588==
==88588== Invalid read of size 8
==88588== at 0x4018E0: SYSTEM_$$_MOVE$formal$formal$INT64 (x86_64.inc:134)
==88588== by 0x401320: P$PROJECT1_$$_GETCODEPOINT$ANSISTRING$INT64$$ANSISTRING (project1.lpr:16)
==88588== by 0x401525: P$PROJECT1_$$_FOO$ANSISTRING (project1.lpr:47)
==88588== by 0x401638: main (project1.lpr:56)
==88588== Address 0x4a520d0 is 32 bytes inside a block of size 44 free'd
==88588== at 0x483CA3F: free (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==88588== by 0x42E22F: CMEM_$$_CFREEMEM$POINTER$$QWORD (cmem.pp:75)
==88588== by 0x41D902: SYSTEM_$$_FREEMEM$POINTER$$QWORD (heap.inc:324)
==88588== by 0x40DB54: fpc_ansistr_decr_ref (astrings.inc:149)
==88588== by 0x40DBD5: fpc_ansistr_assign (astrings.inc:186)
==88588== by 0x401280: P$PROJECT1_$$_GETCODEPOINT$ANSISTRING$INT64$$ANSISTRING (project1.lpr:11)
==88588== by 0x401525: P$PROJECT1_$$_FOO$ANSISTRING (project1.lpr:47)
==88588== by 0x401638: main (project1.lpr:56)
==88588== Block was alloc'd at
==88588== at 0x483B7F3: malloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==88588== by 0x42E1E8: CMEM_$$_CGETMEM$QWORD$$POINTER (cmem.pp:62)
==88588== by 0x41D7E9: SYSTEM_$$_GETMEM$POINTER$QWORD (heap.inc:284)
==88588== by 0x40DAAC: SYSTEM_$$_NEWANSISTRING$INT64$$POINTER (astrings.inc:115)
==88588== by 0x40EDB2: fpc_ansistr_setlength (astrings.inc:776)
==88588== by 0x40E625: fpc_shortstr_to_ansistr (astrings.inc:510)
==88588== by 0x4014AC: P$PROJECT1_$$_FOO$ANSISTRING (project1.lpr:46)
==88588== by 0x401638: main (project1.lpr:56)
==88588==
==88588== Invalid read of size 1
==88588== at 0x401900: SYSTEM_$$_MOVE$formal$formal$INT64 (x86_64.inc:147)
==88588== by 0x401320: P$PROJECT1_$$_GETCODEPOINT$ANSISTRING$INT64$$ANSISTRING (project1.lpr:16)
==88588== by 0x401525: P$PROJECT1_$$_FOO$ANSISTRING (project1.lpr:47)
==88588== by 0x401638: main (project1.lpr:56)
==88588== Address 0x4a520d8 is 40 bytes inside a block of size 44 free'd
==88588== at 0x483CA3F: free (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==88588== by 0x42E22F: CMEM_$$_CFREEMEM$POINTER$$QWORD (cmem.pp:75)
==88588== by 0x41D902: SYSTEM_$$_FREEMEM$POINTER$$QWORD (heap.inc:324)
==88588== by 0x40DB54: fpc_ansistr_decr_ref (astrings.inc:149)
==88588== by 0x40DBD5: fpc_ansistr_assign (astrings.inc:186)
==88588== by 0x401280: P$PROJECT1_$$_GETCODEPOINT$ANSISTRING$INT64$$ANSISTRING (project1.lpr:11)
==88588== by 0x401525: P$PROJECT1_$$_FOO$ANSISTRING (project1.lpr:47)
==88588== by 0x401638: main (project1.lpr:56)
==88588== Block was alloc'd at
==88588== at 0x483B7F3: malloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==88588== by 0x42E1E8: CMEM_$$_CGETMEM$QWORD$$POINTER (cmem.pp:62)
==88588== by 0x41D7E9: SYSTEM_$$_GETMEM$POINTER$QWORD (heap.inc:284)
==88588== by 0x40DAAC: SYSTEM_$$_NEWANSISTRING$INT64$$POINTER (astrings.inc:115)
==88588== by 0x40EDB2: fpc_ansistr_setlength (astrings.inc:776)
==88588== by 0x40E625: fpc_shortstr_to_ansistr (astrings.inc:510)
==88588== by 0x4014AC: P$PROJECT1_$$_FOO$ANSISTRING (project1.lpr:46)
==88588== by 0x401638: main (project1.lpr:56)
==88588==
==88588== Invalid read of size 1
==88588== at 0x40190B: SYSTEM_$$_MOVE$formal$formal$INT64 (x86_64.inc:151)
==88588== by 0x401320: P$PROJECT1_$$_GETCODEPOINT$ANSISTRING$INT64$$ANSISTRING (project1.lpr:16)
==88588== by 0x401525: P$PROJECT1_$$_FOO$ANSISTRING (project1.lpr:47)
==88588== by 0x401638: main (project1.lpr:56)
==88588== Address 0x4a520da is 42 bytes inside a block of size 44 free'd
==88588== at 0x483CA3F: free (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==88588== by 0x42E22F: CMEM_$$_CFREEMEM$POINTER$$QWORD (cmem.pp:75)
==88588== by 0x41D902: SYSTEM_$$_FREEMEM$POINTER$$QWORD (heap.inc:324)
==88588== by 0x40DB54: fpc_ansistr_decr_ref (astrings.inc:149)
==88588== by 0x40DBD5: fpc_ansistr_assign (astrings.inc:186)
==88588== by 0x401280: P$PROJECT1_$$_GETCODEPOINT$ANSISTRING$INT64$$ANSISTRING (project1.lpr:11)
==88588== by 0x401525: P$PROJECT1_$$_FOO$ANSISTRING (project1.lpr:47)
==88588== by 0x401638: main (project1.lpr:56)
==88588== Block was alloc'd at
==88588== at 0x483B7F3: malloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==88588== by 0x42E1E8: CMEM_$$_CGETMEM$QWORD$$POINTER (cmem.pp:62)
==88588== by 0x41D7E9: SYSTEM_$$_GETMEM$POINTER$QWORD (heap.inc:284)
==88588== by 0x40DAAC: SYSTEM_$$_NEWANSISTRING$INT64$$POINTER (astrings.inc:115)
==88588== by 0x40EDB2: fpc_ansistr_setlength (astrings.inc:776)
==88588== by 0x40E625: fpc_shortstr_to_ansistr (astrings.inc:510)
==88588== by 0x4014AC: P$PROJECT1_$$_FOO$ANSISTRING (project1.lpr:46)
==88588== by 0x401638: main (project1.lpr:56)
==88588==
==88588== Invalid read of size 8
==88588== at 0x40EDF1: fpc_ansistr_setlength (astrings.inc:784)
==88588== by 0x40E625: fpc_shortstr_to_ansistr (astrings.inc:510)
==88588== by 0x40150B: P$PROJECT1_$$_FOO$ANSISTRING (project1.lpr:47)
==88588== by 0x401638: main (project1.lpr:56)
==88588== Address 0x4a520c0 is 16 bytes inside a block of size 44 free'd
==88588== at 0x483CA3F: free (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==88588== by 0x42E22F: CMEM_$$_CFREEMEM$POINTER$$QWORD (cmem.pp:75)
==88588== by 0x41D902: SYSTEM_$$_FREEMEM$POINTER$$QWORD (heap.inc:324)
==88588== by 0x40DB54: fpc_ansistr_decr_ref (astrings.inc:149)
==88588== by 0x40DBD5: fpc_ansistr_assign (astrings.inc:186)
==88588== by 0x401280: P$PROJECT1_$$_GETCODEPOINT$ANSISTRING$INT64$$ANSISTRING (project1.lpr:11)
==88588== by 0x401525: P$PROJECT1_$$_FOO$ANSISTRING (project1.lpr:47)
==88588== by 0x401638: main (project1.lpr:56)
==88588== Block was alloc'd at
==88588== at 0x483B7F3: malloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==88588== by 0x42E1E8: CMEM_$$_CGETMEM$QWORD$$POINTER (cmem.pp:62)
==88588== by 0x41D7E9: SYSTEM_$$_GETMEM$POINTER$QWORD (heap.inc:284)
==88588== by 0x40DAAC: SYSTEM_$$_NEWANSISTRING$INT64$$POINTER (astrings.inc:115)
==88588== by 0x40EDB2: fpc_ansistr_setlength (astrings.inc:776)
==88588== by 0x40E625: fpc_shortstr_to_ansistr (astrings.inc:510)
==88588== by 0x4014AC: P$PROJECT1_$$_FOO$ANSISTRING (project1.lpr:46)
==88588== by 0x401638: main (project1.lpr:56)
==88588==
==88588== Invalid read of size 2
==88588== at 0x40EE88: fpc_ansistr_setlength (astrings.inc:800)
==88588== by 0x40E625: fpc_shortstr_to_ansistr (astrings.inc:510)
==88588== by 0x40150B: P$PROJECT1_$$_FOO$ANSISTRING (project1.lpr:47)
==88588== by 0x401638: main (project1.lpr:56)
==88588== Address 0x4a520b8 is 8 bytes inside a block of size 44 free'd
==88588== at 0x483CA3F: free (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==88588== by 0x42E22F: CMEM_$$_CFREEMEM$POINTER$$QWORD (cmem.pp:75)
==88588== by 0x41D902: SYSTEM_$$_FREEMEM$POINTER$$QWORD (heap.inc:324)
==88588== by 0x40DB54: fpc_ansistr_decr_ref (astrings.inc:149)
==88588== by 0x40DBD5: fpc_ansistr_assign (astrings.inc:186)
==88588== by 0x401280: P$PROJECT1_$$_GETCODEPOINT$ANSISTRING$INT64$$ANSISTRING (project1.lpr:11)
==88588== by 0x401525: P$PROJECT1_$$_FOO$ANSISTRING (project1.lpr:47)
==88588== by 0x401638: main (project1.lpr:56)
==88588== Block was alloc'd at
==88588== at 0x483B7F3: malloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==88588== by 0x42E1E8: CMEM_$$_CGETMEM$QWORD$$POINTER (cmem.pp:62)
==88588== by 0x41D7E9: SYSTEM_$$_GETMEM$POINTER$QWORD (heap.inc:284)
==88588== by 0x40DAAC: SYSTEM_$$_NEWANSISTRING$INT64$$POINTER (astrings.inc:115)
==88588== by 0x40EDB2: fpc_ansistr_setlength (astrings.inc:776)
==88588== by 0x40E625: fpc_shortstr_to_ansistr (astrings.inc:510)
==88588== by 0x4014AC: P$PROJECT1_$$_FOO$ANSISTRING (project1.lpr:46)
==88588== by 0x401638: main (project1.lpr:56)
==88588==
==88588== Invalid read of size 8
==88588== at 0x40EE9C: fpc_ansistr_setlength (astrings.inc:802)
==88588== by 0x40E625: fpc_shortstr_to_ansistr (astrings.inc:510)
==88588== by 0x40150B: P$PROJECT1_$$_FOO$ANSISTRING (project1.lpr:47)
==88588== by 0x401638: main (project1.lpr:56)
==88588== Address 0x4a520c8 is 24 bytes inside a block of size 44 free'd
==88588== at 0x483CA3F: free (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==88588== by 0x42E22F: CMEM_$$_CFREEMEM$POINTER$$QWORD (cmem.pp:75)
==88588== by 0x41D902: SYSTEM_$$_FREEMEM$POINTER$$QWORD (heap.inc:324)
==88588== by 0x40DB54: fpc_ansistr_decr_ref (astrings.inc:149)
==88588== by 0x40DBD5: fpc_ansistr_assign (astrings.inc:186)
==88588== by 0x401280: P$PROJECT1_$$_GETCODEPOINT$ANSISTRING$INT64$$ANSISTRING (project1.lpr:11)
==88588== by 0x401525: P$PROJECT1_$$_FOO$ANSISTRING (project1.lpr:47)
==88588== by 0x401638: main (project1.lpr:56)
==88588== Block was alloc'd at
==88588== at 0x483B7F3: malloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==88588== by 0x42E1E8: CMEM_$$_CGETMEM$QWORD$$POINTER (cmem.pp:62)
==88588== by 0x41D7E9: SYSTEM_$$_GETMEM$POINTER$QWORD (heap.inc:284)
==88588== by 0x40DAAC: SYSTEM_$$_NEWANSISTRING$INT64$$POINTER (astrings.inc:115)
==88588== by 0x40EDB2: fpc_ansistr_setlength (astrings.inc:776)
==88588== by 0x40E625: fpc_shortstr_to_ansistr (astrings.inc:510)
==88588== by 0x4014AC: P$PROJECT1_$$_FOO$ANSISTRING (project1.lpr:46)
==88588== by 0x401638: main (project1.lpr:56)
==88588==
==88588== Invalid read of size 8
==88588== at 0x4018E0: SYSTEM_$$_MOVE$formal$formal$INT64 (x86_64.inc:134)
==88588== by 0x40E625: fpc_shortstr_to_ansistr (astrings.inc:510)
==88588== by 0x40150B: P$PROJECT1_$$_FOO$ANSISTRING (project1.lpr:47)
==88588== by 0x401638: main (project1.lpr:56)
==88588== Address 0x4a520d0 is 32 bytes inside a block of size 44 free'd
==88588== at 0x483CA3F: free (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==88588== by 0x42E22F: CMEM_$$_CFREEMEM$POINTER$$QWORD (cmem.pp:75)
==88588== by 0x41D902: SYSTEM_$$_FREEMEM$POINTER$$QWORD (heap.inc:324)
==88588== by 0x40DB54: fpc_ansistr_decr_ref (astrings.inc:149)
==88588== by 0x40DBD5: fpc_ansistr_assign (astrings.inc:186)
==88588== by 0x401280: P$PROJECT1_$$_GETCODEPOINT$ANSISTRING$INT64$$ANSISTRING (project1.lpr:11)
==88588== by 0x401525: P$PROJECT1_$$_FOO$ANSISTRING (project1.lpr:47)
==88588== by 0x401638: main (project1.lpr:56)
==88588== Block was alloc'd at
==88588== at 0x483B7F3: malloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==88588== by 0x42E1E8: CMEM_$$_CGETMEM$QWORD$$POINTER (cmem.pp:62)
==88588== by 0x41D7E9: SYSTEM_$$_GETMEM$POINTER$QWORD (heap.inc:284)
==88588== by 0x40DAAC: SYSTEM_$$_NEWANSISTRING$INT64$$POINTER (astrings.inc:115)
==88588== by 0x40EDB2: fpc_ansistr_setlength (astrings.inc:776)
==88588== by 0x40E625: fpc_shortstr_to_ansistr (astrings.inc:510)
==88588== by 0x4014AC: P$PROJECT1_$$_FOO$ANSISTRING (project1.lpr:46)
==88588== by 0x401638: main (project1.lpr:56)
==88588==
==88588== Invalid read of size 1
==88588== at 0x401900: SYSTEM_$$_MOVE$formal$formal$INT64 (x86_64.inc:147)
==88588== by 0x40E625: fpc_shortstr_to_ansistr (astrings.inc:510)
==88588== by 0x40150B: P$PROJECT1_$$_FOO$ANSISTRING (project1.lpr:47)
==88588== by 0x401638: main (project1.lpr:56)
==88588== Address 0x4a520d8 is 40 bytes inside a block of size 44 free'd
==88588== at 0x483CA3F: free (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==88588== by 0x42E22F: CMEM_$$_CFREEMEM$POINTER$$QWORD (cmem.pp:75)
==88588== by 0x41D902: SYSTEM_$$_FREEMEM$POINTER$$QWORD (heap.inc:324)
==88588== by 0x40DB54: fpc_ansistr_decr_ref (astrings.inc:149)
==88588== by 0x40DBD5: fpc_ansistr_assign (astrings.inc:186)
==88588== by 0x401280: P$PROJECT1_$$_GETCODEPOINT$ANSISTRING$INT64$$ANSISTRING (project1.lpr:11)
==88588== by 0x401525: P$PROJECT1_$$_FOO$ANSISTRING (project1.lpr:47)
==88588== by 0x401638: main (project1.lpr:56)
==88588== Block was alloc'd at
==88588== at 0x483B7F3: malloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==88588== by 0x42E1E8: CMEM_$$_CGETMEM$QWORD$$POINTER (cmem.pp:62)
==88588== by 0x41D7E9: SYSTEM_$$_GETMEM$POINTER$QWORD (heap.inc:284)
==88588== by 0x40DAAC: SYSTEM_$$_NEWANSISTRING$INT64$$POINTER (astrings.inc:115)
==88588== by 0x40EDB2: fpc_ansistr_setlength (astrings.inc:776)
==88588== by 0x40E625: fpc_shortstr_to_ansistr (astrings.inc:510)
==88588== by 0x4014AC: P$PROJECT1_$$_FOO$ANSISTRING (project1.lpr:46)
==88588== by 0x401638: main (project1.lpr:56)
==88588==
==88588== Invalid read of size 1
==88588== at 0x40190B: SYSTEM_$$_MOVE$formal$formal$INT64 (x86_64.inc:151)
==88588== by 0x40E625: fpc_shortstr_to_ansistr (astrings.inc:510)
==88588== by 0x40150B: P$PROJECT1_$$_FOO$ANSISTRING (project1.lpr:47)
==88588== by 0x401638: main (project1.lpr:56)
==88588== Address 0x4a520da is 42 bytes inside a block of size 44 free'd
==88588== at 0x483CA3F: free (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==88588== by 0x42E22F: CMEM_$$_CFREEMEM$POINTER$$QWORD (cmem.pp:75)
==88588== by 0x41D902: SYSTEM_$$_FREEMEM$POINTER$$QWORD (heap.inc:324)
==88588== by 0x40DB54: fpc_ansistr_decr_ref (astrings.inc:149)
==88588== by 0x40DBD5: fpc_ansistr_assign (astrings.inc:186)
==88588== by 0x401280: P$PROJECT1_$$_GETCODEPOINT$ANSISTRING$INT64$$ANSISTRING (project1.lpr:11)
==88588== by 0x401525: P$PROJECT1_$$_FOO$ANSISTRING (project1.lpr:47)
==88588== by 0x401638: main (project1.lpr:56)
==88588== Block was alloc'd at
==88588== at 0x483B7F3: malloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==88588== by 0x42E1E8: CMEM_$$_CGETMEM$QWORD$$POINTER (cmem.pp:62)
==88588== by 0x41D7E9: SYSTEM_$$_GETMEM$POINTER$QWORD (heap.inc:284)
==88588== by 0x40DAAC: SYSTEM_$$_NEWANSISTRING$INT64$$POINTER (astrings.inc:115)
==88588== by 0x40EDB2: fpc_ansistr_setlength (astrings.inc:776)
==88588== by 0x40E625: fpc_shortstr_to_ansistr (astrings.inc:510)
==88588== by 0x4014AC: P$PROJECT1_$$_FOO$ANSISTRING (project1.lpr:46)
==88588== by 0x401638: main (project1.lpr:56)
==88588==
==88588== Invalid read of size 8
==88588== at 0x40DB34: fpc_ansistr_decr_ref (astrings.inc:146)
==88588== by 0x40EEDC: fpc_ansistr_setlength (astrings.inc:808)
==88588== by 0x40E625: fpc_shortstr_to_ansistr (astrings.inc:510)
==88588== by 0x40150B: P$PROJECT1_$$_FOO$ANSISTRING (project1.lpr:47)
==88588== by 0x401638: main (project1.lpr:56)
==88588== Address 0x4a520c0 is 16 bytes inside a block of size 44 free'd
==88588== at 0x483CA3F: free (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==88588== by 0x42E22F: CMEM_$$_CFREEMEM$POINTER$$QWORD (cmem.pp:75)
==88588== by 0x41D902: SYSTEM_$$_FREEMEM$POINTER$$QWORD (heap.inc:324)
==88588== by 0x40DB54: fpc_ansistr_decr_ref (astrings.inc:149)
==88588== by 0x40DBD5: fpc_ansistr_assign (astrings.inc:186)
==88588== by 0x401280: P$PROJECT1_$$_GETCODEPOINT$ANSISTRING$INT64$$ANSISTRING (project1.lpr:11)
==88588== by 0x401525: P$PROJECT1_$$_FOO$ANSISTRING (project1.lpr:47)
==88588== by 0x401638: main (project1.lpr:56)
==88588== Block was alloc'd at
==88588== at 0x483B7F3: malloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==88588== by 0x42E1E8: CMEM_$$_CGETMEM$QWORD$$POINTER (cmem.pp:62)
==88588== by 0x41D7E9: SYSTEM_$$_GETMEM$POINTER$QWORD (heap.inc:284)
==88588== by 0x40DAAC: SYSTEM_$$_NEWANSISTRING$INT64$$POINTER (astrings.inc:115)
==88588== by 0x40EDB2: fpc_ansistr_setlength (astrings.inc:776)
==88588== by 0x40E625: fpc_shortstr_to_ansistr (astrings.inc:510)
==88588== by 0x4014AC: P$PROJECT1_$$_FOO$ANSISTRING (project1.lpr:46)
==88588== by 0x401638: main (project1.lpr:56)
==88588==
==88588== Invalid read of size 8
==88588== at 0x401E85: SYSTEM_$$_DECLOCKED$INT64$$BOOLEAN (x86_64.inc:721)
==88588== by 0x40EEDC: fpc_ansistr_setlength (astrings.inc:808)
==88588== by 0x40E625: fpc_shortstr_to_ansistr (astrings.inc:510)
==88588== by 0x40150B: P$PROJECT1_$$_FOO$ANSISTRING (project1.lpr:47)
==88588== by 0x401638: main (project1.lpr:56)
==88588== Address 0x4a520c0 is 16 bytes inside a block of size 44 free'd
==88588== at 0x483CA3F: free (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==88588== by 0x42E22F: CMEM_$$_CFREEMEM$POINTER$$QWORD (cmem.pp:75)
==88588== by 0x41D902: SYSTEM_$$_FREEMEM$POINTER$$QWORD (heap.inc:324)
==88588== by 0x40DB54: fpc_ansistr_decr_ref (astrings.inc:149)
==88588== by 0x40DBD5: fpc_ansistr_assign (astrings.inc:186)
==88588== by 0x401280: P$PROJECT1_$$_GETCODEPOINT$ANSISTRING$INT64$$ANSISTRING (project1.lpr:11)
==88588== by 0x401525: P$PROJECT1_$$_FOO$ANSISTRING (project1.lpr:47)
==88588== by 0x401638: main (project1.lpr:56)
==88588== Block was alloc'd at
==88588== at 0x483B7F3: malloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==88588== by 0x42E1E8: CMEM_$$_CGETMEM$QWORD$$POINTER (cmem.pp:62)
==88588== by 0x41D7E9: SYSTEM_$$_GETMEM$POINTER$QWORD (heap.inc:284)
==88588== by 0x40DAAC: SYSTEM_$$_NEWANSISTRING$INT64$$POINTER (astrings.inc:115)
==88588== by 0x40EDB2: fpc_ansistr_setlength (astrings.inc:776)
==88588== by 0x40E625: fpc_shortstr_to_ansistr (astrings.inc:510)
==88588== by 0x4014AC: P$PROJECT1_$$_FOO$ANSISTRING (project1.lpr:46)
==88588== by 0x401638: main (project1.lpr:56)
==88588==
TagsNo tags attached.
Fixed in Revision48128
FPCOldBugId
FPCTarget-
Attached Files

Activities

Martin Friebe

2021-01-09 17:15

manager   ~0128205

One more note.

function UTF8Length(const s: string): PtrInt; inline;

The "inline" is important for the error. (at least in 3.2.0)

Bart Broersma

2021-01-09 19:59

reporter   ~0128212

Shortened version:
program fs;

{$mode objfpc}{$H+}


function GetCodePoint(const S: String; const Index: PtrInt): string;
//equivalent for Result := S[Index], but for Utf8 encoded strings
var
  p: PChar;
  Res: AnsiString;
begin
  writeln('GetCodPoint A: index: ',index, ' Length: ', length(s));
  Result := 'any arbitrary string or empty will do';
  writeln('GetCodPoint B: index: ',index, ' Length: ', length(s));// $F0F0F0F0

  p := @s[Index];
  Res := p;
  SetLength(Res,1);
  Result := Res;
end;


function UTF8Length(const s: string): PtrInt; inline;
begin
  Result:=9;
end;


var
  v1: string;
  s: shortstring;
  i: Integer;
begin
  v1 := '123456789';
  s := v1;
  for i := 1 to UTF8Length(s)-8 do begin
    GetCodePoint(s,i);
  end;
  write('Press Enter ');
  readln;
end.


FPC 3.2.0 32-bit on Win10-64.
With heaptrace enabled
GetCodPoint A: index: 1 Length: 9
GetCodPoint B: index: 1 Length: -252645136
Runtime error 201 at $004016BC
  $004016BC GETCODEPOINT, line 16 of fs.lpr
  $004017F0 main, line 42 of fs.lpr
  $00409177

No heap dump by heaptrc unit
Exitcode = 201

Without heaptrace:
GetCodPoint A: index: 1 Length: 9
GetCodPoint B: index: 1 Length: 9
Press Enter

Then after pressing Enter:
Never ending repeating blocks of:
Runtime error 216 at $0040404E
  $0040404E
  $77DE71E2
  $77DE71B4
  $77DD3B36
  $77DE71E2
  $77DE71B4
  $77DD3B36
  $77DE71E2
  $77DE71B4
  $77DD3B36

Runtime error 216 at $0040404E
  $0040404E
  $77DE71E2
  $77DE71B4
  $77DD3B36
  $77DE71E2
  $77DE71B4
  $77DD3B36
  $77DE71E2
  $77DE71B4
  $77DD3B36

Bart Broersma

2021-01-09 20:12

reporter   ~0128213

Attach the assembler output of fpc -al for both 3.2.0 and trunk.
fs-320.fs (8,532 bytes)   
	.file "fs.lpr"
# Begin asmlist al_procedures

.section .text.n_p$fs$_$getcodepoint$ansistring$longint$$ansistring_$$_fin$00000001,"x"
	.balign 16,0x90
P$FS$_$GETCODEPOINT$ANSISTRING$LONGINT$$ANSISTRING_$$_fin$00000001:
# [fs.lpr]
# [20] end;
	pushl	%ebp
	movl	%eax,%ebp
	leal	-20(%ebp),%eax
	call	fpc_ansistr_decr_ref
# [11] begin
	popl	%ebp
	ret

.section .text.n_p$fs_$$_getcodepoint$ansistring$longint$$ansistring,"x"
	.balign 16,0x90
.globl	P$FS_$$_GETCODEPOINT$ANSISTRING$LONGINT$$ANSISTRING
P$FS_$$_GETCODEPOINT$ANSISTRING$LONGINT$$ANSISTRING:
	pushl	%ebp
	movl	%esp,%ebp
	leal	-20(%esp),%esp
	pushl	%ebx
# Var S located at ebp-4, size=OS_32
# Var Index located at ebp-8, size=OS_S32
# Var $result located at ebp-12, size=OS_32
# Var p located at ebp-16, size=OS_32
# Var Res located at ebp-20, size=OS_32
	movl	%eax,-4(%ebp)
	movl	%edx,-8(%ebp)
	movl	%ecx,-12(%ebp)
	movl	$0,-20(%ebp)
	xorl	%eax,%eax
	pushl	$P$FS$_$GETCODEPOINT$ANSISTRING$LONGINT$$ANSISTRING_$$_fin$00000001
	pushl	%ebp
	pushl	$__FPC_finally_handler
	pushl	%fs:(%eax)
	movl	%esp,%fs:(%eax)
# [12] writeln('GetCodPoint A: index: ',index, ' Length: ', length(s));
	call	fpc_get_output
	movl	%eax,%ebx
	movl	$_$FS$_Ld1,%ecx
	movl	%ebx,%edx
	movl	$0,%eax
	call	fpc_write_text_shortstr
	call	fpc_iocheck
	movl	-8(%ebp),%ecx
	movl	%ebx,%edx
	movl	$0,%eax
	call	fpc_write_text_sint
	call	fpc_iocheck
	movl	$_$FS$_Ld2,%ecx
	movl	%ebx,%edx
	movl	$0,%eax
	call	fpc_write_text_shortstr
	call	fpc_iocheck
	movl	-4(%ebp),%ecx
	testl	%ecx,%ecx
	je	.Lj9
	movl	-4(%ecx),%ecx
.Lj9:
	movl	%ebx,%edx
	movl	$0,%eax
	call	fpc_write_text_sint
	call	fpc_iocheck
	movl	%ebx,%eax
	call	fpc_writeln_end
	call	fpc_iocheck
# [13] Result := 'any arbitrary string or empty will do';
	movl	-12(%ebp),%eax
	movl	$.Ld3,%edx
	call	fpc_ansistr_assign
# [14] writeln('GetCodPoint B: index: ',index, ' Length: ', length(s));// $F0F0F0F0
	call	fpc_get_output
	movl	%eax,%ebx
	movl	$_$FS$_Ld4,%ecx
	movl	%ebx,%edx
	movl	$0,%eax
	call	fpc_write_text_shortstr
	call	fpc_iocheck
	movl	-8(%ebp),%ecx
	movl	%ebx,%edx
	movl	$0,%eax
	call	fpc_write_text_sint
	call	fpc_iocheck
	movl	$_$FS$_Ld2,%ecx
	movl	%ebx,%edx
	movl	$0,%eax
	call	fpc_write_text_shortstr
	call	fpc_iocheck
	movl	-4(%ebp),%ecx
	testl	%ecx,%ecx
	je	.Lj10
	movl	-4(%ecx),%ecx
.Lj10:
	movl	%ebx,%edx
	movl	$0,%eax
	call	fpc_write_text_sint
	call	fpc_iocheck
	movl	%ebx,%eax
	call	fpc_writeln_end
	call	fpc_iocheck
# [16] p := @s[Index];
	movl	-4(%ebp),%edx
	movl	-8(%ebp),%eax
	leal	-1(%edx,%eax,1),%eax
	movl	%eax,-16(%ebp)
# [17] Res := p;
	leal	-20(%ebp),%ecx
	movl	-16(%ebp),%eax
	movw	$0,%dx
	call	fpc_pchar_to_ansistr
# [18] SetLength(Res,1);
	leal	-20(%ebp),%eax
	movw	$0,%cx
	movl	$1,%edx
	call	fpc_ansistr_setlength
# [19] Result := Res;
	movl	-12(%ebp),%eax
	movl	-20(%ebp),%edx
	call	fpc_ansistr_assign
	xorl	%eax,%eax
	popl	%edx
	addl	$12,%esp
	movl	%edx,%fs:(%eax)
	movl	%ebp,%eax
	call	P$FS$_$GETCODEPOINT$ANSISTRING$LONGINT$$ANSISTRING_$$_fin$00000001
	popl	%ebx
	movl	%ebp,%esp
	popl	%ebp
	ret

.section .text.n_p$fs_$$_utf8length$ansistring$$longint,"x"
	.balign 16,0x90
.globl	P$FS_$$_UTF8LENGTH$ANSISTRING$$LONGINT
P$FS_$$_UTF8LENGTH$ANSISTRING$$LONGINT:
# [29] begin
	pushl	%ebp
	movl	%esp,%ebp
	leal	-8(%esp),%esp
# Var s located at ebp-4, size=OS_32
# Var $result located at ebp-8, size=OS_S32
	movl	%eax,-4(%ebp)
# [30] Result:=9;
	movl	$9,-8(%ebp)
# [31] end;
	movl	-8(%ebp),%eax
	movl	%ebp,%esp
	popl	%ebp
	ret

.section .text.n_p$fs_$$_fin$0000000b,"x"
	.balign 16,0x90
P$FS_$$_fin$0000000B:
# [46] end.
	pushl	%ebp
	movl	%eax,%ebp
# [38] begin
	leal	-8(%ebp),%eax
	call	fpc_ansistr_decr_ref
	leal	-4(%ebp),%eax
	call	fpc_ansistr_decr_ref
	popl	%ebp
	ret

.section .text.n__main,"x"
	.balign 16,0x90
.globl	PASCALMAIN
PASCALMAIN:
.globl	_main
_main:
# Temps allocated between ebp-8 and ebp+0
	pushl	%ebp
	movl	%esp,%ebp
	leal	-8(%esp),%esp
	pushl	%ebx
	call	fpc_initializeunits
	movl	$0,-8(%ebp)
	movl	$0,-4(%ebp)
	xorl	%eax,%eax
	pushl	$P$FS_$$_fin$0000000B
	pushl	%ebp
	pushl	$__FPC_finally_handler
	pushl	%fs:(%eax)
	movl	%esp,%fs:(%eax)
# [39] v1 := '123456789';
	movl	$U_$P$FS_$$_V1,%eax
	movl	$.Ld5,%edx
	call	fpc_ansistr_assign
# [40] s := v1;
	movl	U_$P$FS_$$_V1,%ecx
	movl	$U_$P$FS_$$_S,%eax
	movl	$255,%edx
	call	fpc_ansistr_to_shortstr
# [41] for i := 1 to UTF8Length(s)-8 do begin
	leal	-4(%ebp),%eax
	call	fpc_ansistr_decr_ref
	leal	-8(%ebp),%eax
	call	fpc_ansistr_decr_ref
	movl	$U_$P$FS_$$_S,%eax
	leal	-8(%ebp),%ecx
	movw	$0,%dx
	call	fpc_shortstr_to_ansistr
	movl	-8(%ebp),%eax
	movl	%eax,-4(%ebp)
	movl	$9,%ebx
	subl	$8,%ebx
	cmpl	$1,%ebx
	jge	.Lj21
	jmp	.Lj22
.Lj21:
	movl	$0,U_$P$FS_$$_I
	.balign 8,0x90
.Lj23:
	movl	U_$P$FS_$$_I,%eax
	leal	1(%eax),%eax
	movl	%eax,U_$P$FS_$$_I
# [42] GetCodePoint(s,i);
	movl	$U_$P$FS_$$_S,%eax
	leal	-4(%ebp),%ecx
	movw	$0,%dx
	call	fpc_shortstr_to_ansistr
	movl	-4(%ebp),%eax
	movl	U_$P$FS_$$_I,%edx
	leal	-8(%ebp),%ecx
	call	P$FS_$$_GETCODEPOINT$ANSISTRING$LONGINT$$ANSISTRING
	leal	-8(%ebp),%eax
	call	fpc_ansistr_decr_ref
	cmpl	U_$P$FS_$$_I,%ebx
	jle	.Lj25
	jmp	.Lj23
.Lj25:
	.balign 4,0x90
.Lj22:
# [44] write('Press Enter ');
	call	fpc_get_output
	movl	%eax,%ebx
	movl	$_$FS$_Ld6,%ecx
	movl	%ebx,%edx
	movl	$0,%eax
	call	fpc_write_text_shortstr
	call	fpc_iocheck
	movl	%ebx,%eax
	call	fpc_write_end
	call	fpc_iocheck
# [45] readln;
	call	fpc_get_input
	movl	%eax,%ebx
	movl	%ebx,%eax
	call	fpc_readln_end
	call	fpc_iocheck
	xorl	%eax,%eax
	popl	%edx
	addl	$12,%esp
	movl	%edx,%fs:(%eax)
	movl	%ebp,%eax
	call	P$FS_$$_fin$0000000B
	call	fpc_do_exit
	popl	%ebx
	movl	%ebp,%esp
	popl	%ebp
	ret

.section .text.n__p$fs_$$_init_implicit$,"x"
	.balign 16,0x90
.globl	INIT$_$P$FS
INIT$_$P$FS:
.globl	_P$FS_$$_init_implicit$
_P$FS_$$_init_implicit$:
	pushl	%ebp
	movl	%esp,%ebp
	movl	%ebp,%esp
	popl	%ebp
	ret

.section .text.n__p$fs_$$_finalize_implicit$,"x"
	.balign 16,0x90
.globl	PASCALFINALIZE
PASCALFINALIZE:
.globl	FINALIZE$_$P$FS
FINALIZE$_$P$FS:
.globl	_P$FS_$$_finalize_implicit$
_P$FS_$$_finalize_implicit$:
	pushl	%ebp
	movl	%esp,%ebp
	movl	$U_$P$FS_$$_V1,%eax
	call	fpc_ansistr_decr_ref
	movl	%ebp,%esp
	popl	%ebp
	ret
# End asmlist al_procedures
# Begin asmlist al_globals

.section .bss
	.balign 4
# [fs.lpr]
# [35] v1: string;
U_$P$FS_$$_V1:
	.zero 4

.section .bss
# [36] s: shortstring;
U_$P$FS_$$_S:
	.zero 256

.section .bss
	.balign 4
# [37] i: Integer;
U_$P$FS_$$_I:
	.zero 4

.section .data.n_INITFINAL,"d"
	.balign 4
.globl	INITFINAL
INITFINAL:
	.long	4,0
	.long	INIT$_$SYSTEM
	.long	0
	.long	INIT$_$FPINTRES
	.long	0,0
	.long	FINALIZE$_$OBJPAS
	.long	INIT$_$P$FS
	.long	FINALIZE$_$P$FS

.section .data.n_FPC_THREADVARTABLES,"d"
	.balign 4
.globl	FPC_THREADVARTABLES
FPC_THREADVARTABLES:
	.long	1
	.long	THREADVARLIST_$SYSTEM$indirect

.section .rodata.n_FPC_RESOURCESTRINGTABLES,"d"
	.balign 4
.globl	FPC_RESOURCESTRINGTABLES
FPC_RESOURCESTRINGTABLES:
	.long	0

.section .data.n_FPC_WIDEINITTABLES,"d"
	.balign 4
.globl	FPC_WIDEINITTABLES
FPC_WIDEINITTABLES:
	.long	0

.section .data.n_FPC_RESSTRINITTABLES,"d"
	.balign 4
.globl	FPC_RESSTRINITTABLES
FPC_RESSTRINITTABLES:
	.long	0

.section .fpc.n_version
	.balign 16
__fpc_ident:
	.ascii	"FPC 3.2.0 [2020/06/04] for i386 - Win32"

.section .data.n___heapsize,"d"
	.balign 4
.globl	__heapsize
__heapsize:
	.long	0

.section .data.n___fpc_valgrind,"d"
	.balign 4
.globl	__fpc_valgrind
__fpc_valgrind:
	.byte	0
# End asmlist al_globals
# Begin asmlist al_typedconsts

.section .rodata.n__$FS$_Ld1,"d"
	.balign 4
.globl	_$FS$_Ld1
_$FS$_Ld1:
	.ascii	"\026GetCodPoint A: index: \000"

.section .rodata.n__$FS$_Ld2,"d"
	.balign 4
.globl	_$FS$_Ld2
_$FS$_Ld2:
	.ascii	"\011 Length: \000"

.section .rodata.n_.Ld3,"d"
	.balign 4
.Ld3$strlab:
	.short	0,1
	.long	-1,37
.Ld3:
	.ascii	"any arbitrary string or empty will do\000"

.section .rodata.n__$FS$_Ld4,"d"
	.balign 4
.globl	_$FS$_Ld4
_$FS$_Ld4:
	.ascii	"\026GetCodPoint B: index: \000"

.section .rodata.n_.Ld5,"d"
	.balign 4
.Ld5$strlab:
	.short	0,1
	.long	-1,9
.Ld5:
	.ascii	"123456789\000"

.section .rodata.n__$FS$_Ld6,"d"
	.balign 4
.globl	_$FS$_Ld6
_$FS$_Ld6:
	.ascii	"\014Press Enter \000"
# End asmlist al_typedconsts

fs-320.fs (8,532 bytes)   
fs-trunk.s (8,682 bytes)   
	.file "fs.lpr"
# Begin asmlist al_procedures

.section .text.n_p$fs$_$getcodepoint$ansistring$longint$$ansistring_$$_fin$00000001,"ax"
	.balign 16,0x90
.globl	P$FS$_$GETCODEPOINT$ANSISTRING$LONGINT$$ANSISTRING_$$_fin$00000001
P$FS$_$GETCODEPOINT$ANSISTRING$LONGINT$$ANSISTRING_$$_fin$00000001:
# [fs.lpr]
# [20] end;
	pushl	%ebp
	movl	%eax,%ebp
	leal	-20(%ebp),%eax
	call	fpc_ansistr_decr_ref
# [11] begin
	popl	%ebp
	ret

.section .text.n_p$fs_$$_getcodepoint$ansistring$longint$$ansistring,"ax"
	.balign 16,0x90
.globl	P$FS_$$_GETCODEPOINT$ANSISTRING$LONGINT$$ANSISTRING
P$FS_$$_GETCODEPOINT$ANSISTRING$LONGINT$$ANSISTRING:
	pushl	%ebp
	movl	%esp,%ebp
	leal	-20(%esp),%esp
	pushl	%ebx
# Var S located at ebp-4, size=OS_32
# Var Index located at ebp-8, size=OS_S32
# Var $result located at ebp-12, size=OS_32
# Var p located at ebp-16, size=OS_32
# Var Res located at ebp-20, size=OS_32
	movl	%eax,-4(%ebp)
	movl	%edx,-8(%ebp)
	movl	%ecx,-12(%ebp)
	movl	$0,-20(%ebp)
	xorl	%eax,%eax
	pushl	$P$FS$_$GETCODEPOINT$ANSISTRING$LONGINT$$ANSISTRING_$$_fin$00000001
	pushl	%ebp
	pushl	$__FPC_finally_handler
	pushl	%fs:(%eax)
	movl	%esp,%fs:(%eax)
# [12] writeln('GetCodPoint A: index: ',index, ' Length: ', length(s));
	call	fpc_get_output
	movl	%eax,%ebx
	movl	$_$FS$_Ld1,%ecx
	movl	%ebx,%edx
	movl	$0,%eax
	call	fpc_write_text_shortstr
	call	fpc_iocheck
	movl	%ebx,%edx
	movl	-8(%ebp),%ecx
	movl	$0,%eax
	call	fpc_write_text_sint
	call	fpc_iocheck
	movl	$_$FS$_Ld2,%ecx
	movl	%ebx,%edx
	movl	$0,%eax
	call	fpc_write_text_shortstr
	call	fpc_iocheck
	movl	-4(%ebp),%ecx
	cmpl	$0,%ecx
	je	.Lj9
	movl	-4(%ecx),%ecx
.Lj9:
	movl	%ebx,%edx
	movl	$0,%eax
	call	fpc_write_text_sint
	call	fpc_iocheck
	movl	%ebx,%eax
	call	fpc_writeln_end
	call	fpc_iocheck
# [13] Result := 'any arbitrary string or empty will do';
	movl	$.Ld3,%edx
	movl	-12(%ebp),%eax
	call	fpc_ansistr_assign
# [14] writeln('GetCodPoint B: index: ',index, ' Length: ', length(s));// $F0F0F0F0
	call	fpc_get_output
	movl	%eax,%ebx
	movl	$_$FS$_Ld4,%ecx
	movl	%ebx,%edx
	movl	$0,%eax
	call	fpc_write_text_shortstr
	call	fpc_iocheck
	movl	%ebx,%edx
	movl	-8(%ebp),%ecx
	movl	$0,%eax
	call	fpc_write_text_sint
	call	fpc_iocheck
	movl	$_$FS$_Ld2,%ecx
	movl	%ebx,%edx
	movl	$0,%eax
	call	fpc_write_text_shortstr
	call	fpc_iocheck
	movl	-4(%ebp),%ecx
	cmpl	$0,%ecx
	je	.Lj10
	movl	-4(%ecx),%ecx
.Lj10:
	movl	%ebx,%edx
	movl	$0,%eax
	call	fpc_write_text_sint
	call	fpc_iocheck
	movl	%ebx,%eax
	call	fpc_writeln_end
	call	fpc_iocheck
# [16] p := @s[Index];
	movl	-4(%ebp),%eax
	movl	-8(%ebp),%edx
	leal	-1(%eax,%edx,1),%eax
	movl	%eax,-16(%ebp)
# [17] Res := p;
	leal	-20(%ebp),%ecx
	movw	$0,%dx
	movl	-16(%ebp),%eax
	call	fpc_pchar_to_ansistr
# [18] SetLength(Res,1);
	leal	-20(%ebp),%eax
	movw	$0,%cx
	movl	$1,%edx
	call	fpc_ansistr_setlength
# [19] Result := Res;
	movl	-20(%ebp),%edx
	movl	-12(%ebp),%eax
	call	fpc_ansistr_assign
	xorl	%eax,%eax
	popl	%edx
	addl	$12,%esp
	movl	%edx,%fs:(%eax)
	movl	%ebp,%eax
	call	P$FS$_$GETCODEPOINT$ANSISTRING$LONGINT$$ANSISTRING_$$_fin$00000001
	popl	%ebx
	movl	%ebp,%esp
	popl	%ebp
	ret

.section .text.n_p$fs_$$_utf8length$ansistring$$longint,"ax"
	.balign 16,0x90
.globl	P$FS_$$_UTF8LENGTH$ANSISTRING$$LONGINT
P$FS_$$_UTF8LENGTH$ANSISTRING$$LONGINT:
# [29] begin
	pushl	%ebp
	movl	%esp,%ebp
	leal	-8(%esp),%esp
# Var s located at ebp-4, size=OS_32
# Var $result located at ebp-8, size=OS_S32
	movl	%eax,-4(%ebp)
# [30] Result:=9;
	movl	$9,-8(%ebp)
# [31] end;
	movl	-8(%ebp),%eax
	movl	%ebp,%esp
	popl	%ebp
	ret

.section .text.n_p$fs_$$_fin$0000000b,"ax"
	.balign 16,0x90
.globl	P$FS_$$_fin$0000000B
P$FS_$$_fin$0000000B:
# [46] end.
	pushl	%ebp
	movl	%eax,%ebp
	leal	-16(%ebp),%eax
	call	fpc_ansistr_decr_ref
	leal	-12(%ebp),%eax
	call	fpc_ansistr_decr_ref
	leal	-8(%ebp),%eax
	call	fpc_ansistr_decr_ref
# [38] begin
	popl	%ebp
	ret

.section .text.n__main,"ax"
	.balign 16,0x90
.globl	_main
_main:
.globl	PASCALMAIN
PASCALMAIN:
# Temps allocated between ebp-16 and ebp+0
	pushl	%ebp
	movl	%esp,%ebp
	leal	-16(%esp),%esp
	pushl	%ebx
	call	fpc_initializeunits
	movl	$0,-16(%ebp)
	movl	$0,-12(%ebp)
	movl	$0,-8(%ebp)
	xorl	%eax,%eax
	pushl	$P$FS_$$_fin$0000000B
	pushl	%ebp
	pushl	$__FPC_finally_handler
	pushl	%fs:(%eax)
	movl	%esp,%fs:(%eax)
# [39] v1 := '123456789';
	movl	$.Ld5,%edx
	movl	$U_$P$FS_$$_V1,%eax
	call	fpc_ansistr_assign
# [40] s := v1;
	movl	U_$P$FS_$$_V1,%ecx
	movl	$U_$P$FS_$$_S,%eax
	movl	$255,%edx
	call	fpc_ansistr_to_shortstr
# [41] for i := 1 to UTF8Length(s)-8 do begin
	leal	-8(%ebp),%ecx
	movl	$U_$P$FS_$$_S,%eax
	movw	$0,%dx
	call	fpc_shortstr_to_ansistr
	movl	-8(%ebp),%eax
	movl	%eax,-4(%ebp)
	movl	$9,%ebx
	subl	$8,%ebx
	cmpl	$1,%ebx
	jge	.Lj21
	jmp	.Lj22
.Lj21:
	movl	$0,U_$P$FS_$$_I
	.p2align 4,,10
	.p2align 3
.Lj23:
	movl	U_$P$FS_$$_I,%eax
	leal	1(%eax),%eax
	movl	%eax,U_$P$FS_$$_I
# [42] GetCodePoint(s,i);
	leal	-16(%ebp),%ecx
	movl	$U_$P$FS_$$_S,%eax
	movw	$0,%dx
	call	fpc_shortstr_to_ansistr
	movl	-16(%ebp),%eax
	leal	-12(%ebp),%ecx
	movl	U_$P$FS_$$_I,%edx
	call	P$FS_$$_GETCODEPOINT$ANSISTRING$LONGINT$$ANSISTRING
	leal	-12(%ebp),%eax
	call	fpc_ansistr_decr_ref
	cmpl	U_$P$FS_$$_I,%ebx
	jle	.Lj25
	jmp	.Lj23
.Lj25:
.Lj22:
# [44] write('Press Enter ');
	call	fpc_get_output
	movl	%eax,%ebx
	movl	$_$FS$_Ld6,%ecx
	movl	%ebx,%edx
	movl	$0,%eax
	call	fpc_write_text_shortstr
	call	fpc_iocheck
	movl	%ebx,%eax
	call	fpc_write_end
	call	fpc_iocheck
# [45] readln;
	call	fpc_get_input
	movl	%eax,%ebx
	movl	%ebx,%eax
	call	fpc_readln_end
	call	fpc_iocheck
	xorl	%eax,%eax
	popl	%edx
	addl	$12,%esp
	movl	%edx,%fs:(%eax)
	movl	%ebp,%eax
	call	P$FS_$$_fin$0000000B
	call	fpc_do_exit
	ret

.section .text.n__p$fs_$$_init_implicit$,"ax"
	.balign 16,0x90
.globl	_P$FS_$$_init_implicit$
_P$FS_$$_init_implicit$:
.globl	INIT$_$P$FS
INIT$_$P$FS:
	pushl	%ebp
	movl	%esp,%ebp
	movl	%ebp,%esp
	popl	%ebp
	ret

.section .text.n__p$fs_$$_finalize_implicit$,"ax"
	.balign 16,0x90
.globl	_P$FS_$$_finalize_implicit$
_P$FS_$$_finalize_implicit$:
.globl	FINALIZE$_$P$FS
FINALIZE$_$P$FS:
.globl	PASCALFINALIZE
PASCALFINALIZE:
	pushl	%ebp
	movl	%esp,%ebp
	movl	$U_$P$FS_$$_V1,%eax
	call	fpc_ansistr_decr_ref
	movl	%ebp,%esp
	popl	%ebp
	ret
# End asmlist al_procedures
# Begin asmlist al_globals

.section .bss,"aw"
	.balign 4
# [fs.lpr]
# [35] v1: string;
	.globl U_$P$FS_$$_V1
U_$P$FS_$$_V1:
	.zero 4

.section .bss,"aw"
# [36] s: shortstring;
	.globl U_$P$FS_$$_S
U_$P$FS_$$_S:
	.zero 256

.section .bss,"aw"
	.balign 4
# [37] i: Integer;
	.globl U_$P$FS_$$_I
U_$P$FS_$$_I:
	.zero 4

.section .data.n_INITFINAL,"aw"
	.balign 4
.globl	INITFINAL
INITFINAL:
	.long	4,0
	.long	INIT$_$SYSTEM
	.long	FINALIZE$_$SYSTEM
	.long	INIT$_$FPINTRES
	.long	0,0
	.long	FINALIZE$_$OBJPAS
	.long	INIT$_$P$FS
	.long	FINALIZE$_$P$FS

.section .data.n_FPC_THREADVARTABLES,"aw"
	.balign 4
.globl	FPC_THREADVARTABLES
FPC_THREADVARTABLES:
	.long	1
	.long	THREADVARLIST_$SYSTEM$indirect

.section .rodata.n_FPC_RESOURCESTRINGTABLES,"aw"
	.balign 4
.globl	FPC_RESOURCESTRINGTABLES
FPC_RESOURCESTRINGTABLES:
	.long	0

.section .data.n_FPC_WIDEINITTABLES,"aw"
	.balign 4
.globl	FPC_WIDEINITTABLES
FPC_WIDEINITTABLES:
	.long	0

.section .data.n_FPC_RESSTRINITTABLES,"aw"
	.balign 4
.globl	FPC_RESSTRINITTABLES
FPC_RESSTRINITTABLES:
	.long	0

.section .fpc.n_version,"aw"
	.balign 16
__fpc_ident:
	.ascii	"FPC 3.3.1 [2021/01/06] for i386 - Win32"

.section .data.n___heapsize,"aw"
	.balign 4
.globl	__heapsize
__heapsize:
	.long	0

.section .data.n___fpc_valgrind,"aw"
	.balign 4
.globl	__fpc_valgrind
__fpc_valgrind:
	.byte	0
# End asmlist al_globals
# Begin asmlist al_typedconsts

.section .rodata.n__$FS$_Ld1,"a"
	.balign 4
.globl	_$FS$_Ld1
_$FS$_Ld1:
	.ascii	"\026GetCodPoint A: index: \000"

.section .rodata.n__$FS$_Ld2,"a"
	.balign 4
.globl	_$FS$_Ld2
_$FS$_Ld2:
	.ascii	"\011 Length: \000"

.section .rodata.n_.Ld3,"a"
	.balign 4
.Ld3$strlab:
	.short	0,1
	.long	-1,37
.Ld3:
	.ascii	"any arbitrary string or empty will do\000"

.section .rodata.n__$FS$_Ld4,"a"
	.balign 4
.globl	_$FS$_Ld4
_$FS$_Ld4:
	.ascii	"\026GetCodPoint B: index: \000"

.section .rodata.n_.Ld5,"a"
	.balign 4
.Ld5$strlab:
	.short	0,1
	.long	-1,9
.Ld5:
	.ascii	"123456789\000"

.section .rodata.n__$FS$_Ld6,"a"
	.balign 4
.globl	_$FS$_Ld6
_$FS$_Ld6:
	.ascii	"\014Press Enter \000"
# End asmlist al_typedconsts

fs-trunk.s (8,682 bytes)   

Bart Broersma

2021-01-09 20:21

reporter   ~0128214

Compiler options -Cr -O- (or O1, O2, O3) is suffcient to provoke the crash in my simplified example.
No crash with -O4 though.

Bart Broersma

2021-01-09 22:21

reporter   ~0128224

If I add -gt to the compiler options the output is:
GetCodPoint A: index: 1 Length: -252645136
GetCodPoint B: index: 1 Length: -252645136
Runtime error 201 at $004016E8
  $004016E8 GETCODEPOINT, line 16 of fs.lpr
  $00401817 main, line 37 of fs.lpr
  $00409187

No heap dump by heaptrc unit
Exitcode = 201

Jonas Maebe

2021-01-10 15:12

manager   ~0128244

Thanks for the reduced test cases.

Issue History

Date Modified Username Field Change
2021-01-09 17:04 Martin Friebe New Issue
2021-01-09 17:15 Martin Friebe Note Added: 0128205
2021-01-09 19:59 Bart Broersma Note Added: 0128212
2021-01-09 20:12 Bart Broersma Note Added: 0128213
2021-01-09 20:12 Bart Broersma File Added: fs-320.fs
2021-01-09 20:12 Bart Broersma File Added: fs-trunk.s
2021-01-09 20:21 Bart Broersma Note Added: 0128214
2021-01-09 22:21 Bart Broersma Note Added: 0128224
2021-01-10 15:12 Jonas Maebe Assigned To => Jonas Maebe
2021-01-10 15:12 Jonas Maebe Status new => resolved
2021-01-10 15:12 Jonas Maebe Resolution open => fixed
2021-01-10 15:12 Jonas Maebe Fixed in Version => 3.2.1
2021-01-10 15:12 Jonas Maebe Fixed in Revision => 48128
2021-01-10 15:12 Jonas Maebe FPCTarget => -
2021-01-10 15:12 Jonas Maebe Note Added: 0128244
2021-01-10 18:39 Martin Friebe Status resolved => closed