Welcome, Guest
Username: Password: Remember me
Discussions for CodeTyphon Studio Installation and Setup.
  • Page:
  • 1

TOPIC:

CT580 - ValidUTF8String hangs when input is invalid UTF8 8 years 4 weeks ago #10133

  • Dinko
  • Dinko's Avatar Topic Author
  • Offline
  • Junior Member
  • Junior Member
  • Posts: 91
  • Thank you received: 5
I found some behaviour difference between CT540 and CT580.
When I call function ValidUTF8String and input in function is invalid UTF8 (one byte is removed from the end of string), then function hangs.

I made an example and test project. I think this error is not from CT team but it is connected to fpc trunk that is used in CT580.

Source code
program TestValidUTF8;

{$mode objfpc}{$H+}

uses
  {$IFOPT D-}
  cmem, // CT570 does not like debug mode with cmem
  {$ENDIF}
  heaptrc,
  // lineinfo,

  {$DEFINE UseCThreads}
  {$IFDEF UNIX}{$IFDEF UseCThreads}
  cthreads,
  {$ENDIF}{$ENDIF}

  Classes, Interfaces, sysutils, LazUTF8
  //, ctutils
  { you can add units after this };

function FavLoadFromFile(InFileName: String; InRaiseErrorIfNotExists: Boolean = False): String;
var MyFile: Text;
    MyFileAssigned: Boolean;
    MyBuffer: String;
    MyString: String;
begin
  MyFileAssigned:=False;
  MyString := '';

  if InRaiseErrorIfNotExists then begin
    if FileExists(InFileName) = false then begin
      raise Exception.Create('File: "' + InFileName + '" does not exists!');
    end;
  end;
  try
    AssignFile(MyFile, InFileName);
    MyFileAssigned:=True;
    Reset(MyFile);
    while not Eof(MyFile) do begin
      ReadLn(MyFile, MyBuffer);
      if MyString <> '' then MyString := MyString + #13#10;
      MyString := MyString + MyBuffer;
    end;
    CloseFile(MyFile);
    MyFileAssigned:=False;
  except
    on E: Exception do begin
      if MyFileAssigned then begin
        try
          CloseFile(MyFile);
        finally
        end;
      end;
      raise;
    end;
  end;
  Result := MyString;
end;

procedure TestUTF8String;
var MyString: String;
    MyString1: String;
begin
  MyString := FavLoadFromFile('invaliduft8.txt', true);
  MyString1 := ValidUTF8String(MyString);  // this line hangs and take 100% of one processor
//  MyString1 := UTF8Decode(MyString);     // this is OK
  if MyString1 = MyString then begin
    Writeln('OK');
  end
  else begin
    Writeln('NotOK');
  end;
end;

begin
  if FileExists('.\heaptrc.trc') then begin
    DeleteFile('.\heaptrc.trc');
  end;

  heaptrc.printfaultyblock := True;
  heaptrc.printleakedblock := True;
  heaptrc.quicktrace := True;
  heaptrc.HaltOnNotReleased := True;
  heaptrc.SetHeapTraceOutput('.\heaptrc.trc');

  Writeln('This program will loadinvalid utf8 file as string and try to check if with ValidUTF8String function. Function will hang. Use UTF8Decode to check validity of UTF8 string.');
  Writeln;

  TestUTF8String;

  WriteLn('Press any key to exit and check heaptrc.trc file for leaks.');
  Readln;
end.

Regards, Dinko

Please Log in or Create an account to join the conversation.

  • Page:
  • 1