Recent

Author Topic: Crash caused by Widestrig usage on Win32?  (Read 2032 times)

Valdas

  • New Member
  • *
  • Posts: 49
Crash caused by Widestrig usage on Win32?
« on: October 31, 2022, 10:09:07 am »
Hello,

I encountered a strange crash when my project is compiled for Win32 and i386. After long investigation I nailed it to couple of lines (Lazarus project: "Simple FPC program", OS: win32, CPU: i386; cross compiled with help of FPCUPDeluxe):

Code: Pascal  [Select][+][-]
  1. program project1;
  2.  
  3. const
  4. IVESTIS__DYDZIO_EILES_PRIESDELIS: array of WideString = ('y', 'z', 'a', 'f', 'p', 'n', #181, 'u', 'm', '',
  5.                                                          'k', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y');
  6.  
  7. var
  8.   tekstas: WideString;
  9.   indeksas: Integer;
  10. begin
  11.   Writeln('testas');
  12.   indeksas := 0;
  13.   tekstas := IVESTIS__DYDZIO_EILES_PRIESDELIS[indeksas];
  14. end.
  15.  

Tested under Linux (using Wine) and Win7. In both cases application crashes without any explanations.
I checked exit value: it is 204.

I am missing something?

Sample project attached.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12634
  • FPC developer.
Re: Crash caused by Widestrig usage on Win32?
« Reply #1 on: October 31, 2022, 10:17:28 am »
I wouldn't expect it to compile, and it doesn't in $mode delphi: (both 3.2.2 and 3.3.1):

Code: [Select]
Free Pascal Compiler version 3.2.2 [2021/05/15] for i386
Copyright (c) 1993-2021 by Florian Klaempfl and others
Target OS: Win32 for i386
Compiling project1.lpr
project1.lpr(4,58) Fatal: Syntax error, ";" expected but "const char" found
Fatal: Compilation aborted
Error: d:\fpc\3.2.2\bin\i386-win32\ppc386.exe returned an error exitcode

So it seems to be a $mode objfpc feature or bug.

Valdas

  • New Member
  • *
  • Posts: 49
Re: Crash caused by Widestrig usage on Win32?
« Reply #2 on: October 31, 2022, 10:50:13 am »
I get no crash when I convert an static array to dynamic:
Code: Pascal  [Select][+][-]
  1. const
  2. IVESTIS__DYDZIO_EILES_PRIESDELIS: array[0..17] of WideString = ('y', 'z', 'a', 'f', 'p', 'n', #181, 'u', 'm', '',
  3.                                                          'k', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y');
  4.  
Is it a bug or is it not advisable to use dynamic arrays as constants?

avk

  • Hero Member
  • *****
  • Posts: 825
Re: Crash caused by Widestrig usage on Win32?
« Reply #3 on: October 31, 2022, 11:01:33 am »
It's funny that in this form:
Code: Pascal  [Select][+][-]
  1. program project1;
  2. {$mode objfpc}{$h+}
  3. const
  4.   IVESTIS__DYDZIO_EILES_PRIESDELIS: array[0..17] of WideString = (
  5.     'y', 'z', 'a', 'f', 'p', 'n', #181, 'u', 'm', '', 'k', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y');
  6.  
  7. var
  8.   tekstas: WideString;
  9.   indeksas: Integer;
  10. begin
  11.   Writeln('testas');
  12.   indeksas := 0;
  13.   tekstas := IVESTIS__DYDZIO_EILES_PRIESDELIS[indeksas];
  14.   Writeln(tekstas);
  15. end.
  16.  

or in this form:
Code: Pascal  [Select][+][-]
  1. program project1;
  2. {$mode objfpc}{$h+}
  3. const
  4.   IVESTIS__DYDZIO_EILES_PRIESDELIS: array of String = (
  5.     'y', 'z', 'a', 'f', 'p', 'n', #181, 'u', 'm', '', 'k', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y');
  6.  
  7. var
  8.   tekstas: WideString;
  9.   indeksas: Integer;
  10. begin
  11.   Writeln('testas');
  12.   indeksas := 0;
  13.   tekstas := IVESTIS__DYDZIO_EILES_PRIESDELIS[indeksas];
  14.   Writeln(tekstas);
  15. end.
  16.  

everything seems to work.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12634
  • FPC developer.
Re: Crash caused by Widestrig usage on Win32?
« Reply #4 on: October 31, 2022, 11:03:12 am »
So it is probably the widestring initialization then. Worth a bugreport.


Valdas

  • New Member
  • *
  • Posts: 49

ASerge

  • Hero Member
  • *****
  • Posts: 2475
Re: Crash caused by Widestrig usage on Win32?
« Reply #6 on: October 31, 2022, 03:12:17 pm »
If replace WideString with UnicodeString, the error disappears under Windows. Please check on Linux, where these types do not differ.
Code: Pascal  [Select][+][-]
  1. {$MODE OBJFPC}
  2. {$APPTYPE CONSOLE}
  3. {$LONGSTRINGS ON}
  4.  
  5. const
  6.   IVESTIS__DYDZIO_EILES_PRIESDELIS: array of UnicodeString =
  7.     ('y', 'z', 'a', 'f', 'p', 'n', #181, 'u', 'm', '',
  8.     'k', 'M',  'G', 'T', 'P', 'E', 'Z', 'Y');
  9.  
  10. var
  11.   Text: WideString;
  12.   Index: Integer;
  13. begin
  14.   Writeln('testas');
  15.   Index := 0;
  16.   Text := IVESTIS__DYDZIO_EILES_PRIESDELIS[Index];
  17.   Readln;
  18. end.

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: Crash caused by Widestrig usage on Win32?
« Reply #7 on: October 31, 2022, 03:40:51 pm »
Hi!

#181 is not a legal Unicode sign

Winni

Jorg3000

  • Jr. Member
  • **
  • Posts: 81
Re: Crash caused by Widestrig usage on Win32?
« Reply #8 on: October 31, 2022, 04:00:37 pm »
Hi!

#181 is a permitted Unicode character, it is a "µ".

In UTF-8 the value 181 would not be a permitted character, but this is not about UTF-8.
However, in this example I don't see any reason why you don't just write 'µ' into the array and use Utf8String instead of WideString/UnicodeString.

Jörg

Thaddy

  • Hero Member
  • *****
  • Posts: 18729
  • To Europe: simply sell USA bonds: dollar collapses
Re: Crash caused by Widestrig usage on Win32?
« Reply #9 on: October 31, 2022, 04:03:31 pm »
And the array is an array of WideChar, not WideString. Either way even on Linux a UnicodeString <> WideString, because WideString is not a managed type on any platform, where UnicodeString is.... Docs are not correct for <> Windows. Linux eo need just a proper string manager installed. See https://www.freepascal.org/docs-html/rtl/system/tunicodestringmanager.html. IOW the part that says "on Windows" is misleading: https://www.freepascal.org/docs-html/rtl/system/unicodestring.html
« Last Edit: October 31, 2022, 04:09:40 pm by Thaddy »
If Europe sells their USA bonds the USD will collapse. Europe can affort that given average state debts. The USA can't affort that. Just an advice...

PascalDragon

  • Hero Member
  • *****
  • Posts: 6311
  • Compiler Developer
Re: Crash caused by Widestrig usage on Win32?
« Reply #10 on: November 01, 2022, 08:24:32 pm »
I wouldn't expect it to compile, and it doesn't in $mode delphi: (both 3.2.2 and 3.3.1):

In both Delphi and mode Delphi you need to use square brackets for dynamic array constants.

#181 is not a legal Unicode sign

It is a valid UTF-16 code point.

And the array is an array of WideChar, not WideString.

The array is declared as array of WideString thus the constants will be parsed as WideString constants no matter if they're single character or not.

Either way even on Linux a UnicodeString <> WideString, because WideString is not a managed type on any platform, where UnicodeString is.... Docs are not correct for <> Windows.

The WideString type on Windows is considered as managed as well.

Also the WideString type on non-Windows is internally a UnicodeString. You can see that by looking at the used RTL helper function: e.g. u := 'Foobar' will use fpc_unicodestr_assign on non-Windows instead of fpc_widestr_assign.

 

TinyPortal © 2005-2018