* * *

Author Topic: Is this a bug in compiler widestr.unicode2ascii  (Read 453 times)

engkin

  • Hero Member
  • *****
  • Posts: 1969
Is this a bug in compiler widestr.unicode2ascii
« on: May 13, 2018, 07:17:59 pm »
Unit widestr has a function unicode2ascii:
Code: Pascal  [Select]
  1.     procedure unicode2ascii(r : pcompilerwidestring;p:pchar;cp : tstringencoding);
  2.       var
  3.         m : punicodemap;
  4.         source : tcompilerwidecharptr;
  5.         dest   : pchar;
  6.         i      : longint;
  7.       begin
  8.         { can't implement that here, because the memory size for p() cannot
  9.           be changed here, and we may need more bytes than have been allocated }
  10.         if cp=CP_UTF8 then
  11.           internalerrorproc(2015092701);
  12.  
  13.         if (cp = 0) or (cp=CP_NONE) then
  14.           m:=getmap(current_settings.sourcecodepage)
  15. ...

The function makes sure that cp is not CP_UTF8, but later on it does not do the same check for current_settings.sourcecodepage which is also possible to be CP_UTF8.

Is this a bug?

Thaddy

  • Hero Member
  • *****
  • Posts: 5968
Re: Is this a bug in compiler widestr.unicode2ascii
« Reply #1 on: May 14, 2018, 09:45:50 am »
No, because if cp happens to be UTF8 that code will never execute, because the internalerrorproc prevents that.
recommends {$macro on}{$define Silly:=ObjFpc}

engkin

  • Hero Member
  • *****
  • Posts: 1969
Re: Is this a bug in compiler widestr.unicode2ascii
« Reply #2 on: May 15, 2018, 06:39:02 am »
No, because if cp happens to be UTF8 that code will never execute, because the internalerrorproc prevents that.

The bug that I see is in the highlighted lines:
Code: Pascal  [Select]
  1.     procedure unicode2ascii(r : pcompilerwidestring;p:pchar;cp : tstringencoding);
  2.       var
  3.         m : punicodemap;
  4.         source : tcompilerwidecharptr;
  5.         dest   : pchar;
  6.         i      : longint;
  7.       begin
  8.         { can't implement that here, because the memory size for p() cannot
  9.           be changed here, and we may need more bytes than have been allocated }
  10.         if cp=CP_UTF8 then
  11.           internalerrorproc(2015092701);
  12.  
  13.         if (cp = 0) or (cp=CP_NONE) then
  14.           m:=getmap(current_settings.sourcecodepage)
  15. ...

The bug happens when cp is 0 or CP_NONE AND sourcecodepage is CP_UTF8. This procedure can not convert to UTF8.
« Last Edit: May 15, 2018, 06:41:42 am by engkin »

Thaddy

  • Hero Member
  • *****
  • Posts: 5968
Re: Is this a bug in compiler widestr.unicode2ascii
« Reply #3 on: May 15, 2018, 08:42:19 am »
@getmem
I am trying to force that bug but did not succeed yet in reproducing it. Do you have an example?
recommends {$macro on}{$define Silly:=ObjFpc}

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus