Browse Source

* treat zero-based arrays of char also as pchar's when reading
(i.e., always zero-terminate) (bug 7567)

git-svn-id: trunk@4826 -

Jonas Maebe 19 years ago
parent
commit
b386335f27
4 changed files with 27 additions and 4 deletions
  1. 1 0
      .gitattributes
  2. 1 2
      compiler/ninl.pas
  3. 5 2
      rtl/inc/text.inc
  4. 20 0
      tests/webtbs/tw7567.pp

+ 1 - 0
.gitattributes

@@ -7353,6 +7353,7 @@ tests/webtbs/tw7425.pp svneol=native#text/plain
 tests/webtbs/tw7440.pp svneol=native#text/plain
 tests/webtbs/tw7446.pp svneol=native#text/plain
 tests/webtbs/tw7489.pp svneol=native#text/plain
+tests/webtbs/tw7567.pp svneol=native#text/plain
 tests/webtbs/tw7568.pp svneol=native#text/plain
 tests/webtbs/ub1873.pp svneol=native#text/plain
 tests/webtbs/ub1883.pp svneol=native#text/plain

+ 1 - 2
compiler/ninl.pas

@@ -821,8 +821,7 @@ implementation
                         tcallparanode(para.right).right := lenpara;
                         { in case of writing a chararray, add whether it's }
                         { zero-based                                       }
-                        if not(do_read) and
-                           (para.left.resulttype.def.deftype = arraydef) then
+                        if (para.left.resulttype.def.deftype = arraydef) then
                           para := ccallparanode.create(cordconstnode.create(
                             ord(tarraydef(para.left.resulttype.def).lowrange=0),booltype,false),para);
                         { create the call statement }

+ 5 - 2
rtl/inc/text.inc

@@ -971,12 +971,15 @@ Begin
 End;
 
 
-Procedure fpc_Read_Text_PChar_As_Array(var f : Text;out s : array of char); iocheck; [Public,Alias:'FPC_READ_TEXT_PCHAR_AS_ARRAY']; compilerproc;
+Procedure fpc_Read_Text_PChar_As_Array(var f : Text;out s : array of char; zerobased: boolean = false); iocheck; [Public,Alias:'FPC_READ_TEXT_PCHAR_AS_ARRAY']; compilerproc;
 var
   len: longint;
 Begin
   len := ReadPCharLen(f,pchar(@s),high(s)+1);
-  if len <= high(s) then
+  if zerobased and
+     (len > high(s)) then
+    len := high(s);
+  if (len <= high(s)) then
     s[len] := #0;
 End;
 

+ 20 - 0
tests/webtbs/tw7567.pp

@@ -0,0 +1,20 @@
+var
+  a: array[0..9] of char;
+  b: array[1..10] of char;
+  t: text;
+begin
+  assign(t,'tw7567.txt');
+  rewrite(t);
+  writeln(t,'0123456789abcdef');
+  writeln(t,'0123456789abcdef');
+  close(t);
+  reset(t);
+  readln(t,a);
+  readln(t,b);
+  close(t);
+  erase(t);
+  if (a <> '012345678') then
+    halt(1);
+  if (b <> '0123456789') then
+    halt(2);
+end.