Browse Source

* fix #39875: when a WideString constant is used to initialize a PChar the constant needs to be converted to the current code page
+ added test

Sven/Sarah Barth 2 years ago
parent
commit
e5957b1ef0
2 changed files with 24 additions and 0 deletions
  1. 3 0
      compiler/ngtcon.pas
  2. 21 0
      tests/webtbs/tw39875.pp

+ 3 - 0
compiler/ngtcon.pas

@@ -846,6 +846,9 @@ function get_next_varsym(def: tabstractrecorddef; const SymList:TFPHashObjectLis
           if is_char(def.pointeddef) and
           if is_char(def.pointeddef) and
             ((node.nodetype=stringconstn) or is_constcharnode(node)) then
             ((node.nodetype=stringconstn) or is_constcharnode(node)) then
             begin
             begin
+              { ensure that a widestring is converted to the current codepage }
+              if is_wide_or_unicode_string(node.resultdef) then
+                tstringconstnode(node).changestringtype(getansistringdef);
               { create a tcb for the string data (it's placed in a separate
               { create a tcb for the string data (it's placed in a separate
                 asmlist) }
                 asmlist) }
               ftcb.start_internal_data_builder(fdatalist,sec_rodata_norel,'',datatcb,ll);
               ftcb.start_internal_data_builder(fdatalist,sec_rodata_norel,'',datatcb,ll);

+ 21 - 0
tests/webtbs/tw39875.pp

@@ -0,0 +1,21 @@
+{$codepage utf8}
+program tw39875;
+var
+  ar: PChar = 'Ёжик';
+  ar2: PChar;
+  l, i: SizeInt;
+begin
+ Writeln(strlen(ar));  //Error
+  Writeln(Length(Ar)); //Error
+  l := Length(ar);
+  ar2 := 'Ёжик';
+  Writeln(strlen(ar2)); //Ok
+  Writeln(Length(Ar2)); //Ok
+  if l <> Length(ar2) then
+    Halt(1);
+  for i := 0 to l - 1 do
+    if ar[i] <> ar2[i] then
+      Halt(2 + i);
+  //Readln;
+end.
+