Recent

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

engkin

  • Hero Member
  • *****
  • Posts: 3112
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: 14157
  • Probably until I exterminate Putin.
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.
Specialize a type, not a var.

engkin

  • Hero Member
  • *****
  • Posts: 3112
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: 14157
  • Probably until I exterminate Putin.
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?
Specialize a type, not a var.

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: Is this a bug in compiler widestr.unicode2ascii
« Reply #4 on: May 21, 2018, 01:02:11 am »
I am trying to force that bug but did not succeed yet in reproducing it. Do you have an example?

Thank you Thaddy for trying.

The procedure is used in two places (at this time), both protect it from this bug. So there is no visible bug related to this procedure, yet. But it does have a logical error.

 

TinyPortal © 2005-2018