View Issue Details

IDProjectCategoryView StatusLast Update
0037775FPCFree Visionpublic2020-09-21 21:01
ReporterJoe care Assigned ToFlorian  
PrioritynormalSeverityminorReproducibilityalways
Status resolvedResolutionwon't fix 
Platformx86-64OSWin64 
Product Version3.3.1 
Summary0037775: (editors.pas) Set length of string with setlength instad of st[0]:=len
DescriptionSetting the length of a string should be done with SetLength instead of setting the char with index 0
Additional InformationPatch provided ...
TagsNo tags attached.
Fixed in Revision
FPCOldBugId
FPCTarget-
Attached Files

Activities

Joe care

2020-09-19 15:20

reporter  

editors.patch (1,494 bytes)   
Index: editors.pas
===================================================================
--- editors.pas	(Revision 46893)
+++ editors.pas	(Arbeitskopie) 
@@ -1069,7 +1069,7 @@
 begin
   BMMakeTable(str,bt);
   len:=length(str);
-  s2[0]:=chr(len);       { sets the length to that of the search String }
+  setlength(s2,len);       { sets the length to that of the search String }
   found:=False;
   numb:=pred(len);
   While (not found) and (numb<(size-len)) do
@@ -1118,7 +1118,7 @@
     exit;
   end;
   { create uppercased string }
-  s[0]:=chr(len);
+  setlength(s,len);
   for x:=1 to len do
    begin
      if str[x] in ['a'..'z'] then
@@ -3463,10 +3463,12 @@
 constructor TFileEditor.Load (var S : Objects.TStream);
 VAR
   SStart,SEnd,Curs : Sw_Word;
+  NameLength:byte;
 begin
   Inherited Load (S);
   BufSize := 0;
-  S.Read (FileName[0], SizeOf (Byte));
+  S.Read (NameLength{%H-}, SizeOf (Byte));
+  SetLength(FileName,NameLength);
   S.Read (Filename[1], Length (FileName));
   if IsValid then
     IsValid := LoadFile;
@@ -3639,9 +3641,13 @@
 
 
 procedure TFileEditor.Store (var S : Objects.TStream);
+var
+  NameLength: Byte; // Length of FileName
 begin
   Inherited Store (S);
-  S.Write (FileName, Length (FileName) + 1);
+  NameLength := min(Length (FileName),255);
+  S.Write (NameLength, SizeOf(NameLength));
+  S.Write (FileName[1], NameLength);
   S.Write (SelStart, SizeOf (SelStart));
   S.Write (SelEnd, SizeOf (SelEnd));
   S.Write (CurPtr, SizeOf (CurPtr));
editors.patch (1,494 bytes)   

Florian

2020-09-20 10:03

administrator   ~0125652

FV is written in Turbo Pascal style pascal. So using Setlength makes no sense, in particular as it fixes no bug.

Joe care

2020-09-21 21:01

reporter   ~0125732

I'am sorry too hear that, of cause it's not a bug, but even for Turbo Pascal i'd consider this as bad style, especially when one routine uses setlength & length and the other uses shortstr[0]. I even asked Sven if I should report those parts. How about make it a "Style improvement request" ?

Issue History

Date Modified Username Field Change
2020-09-19 15:20 Joe care New Issue
2020-09-19 15:20 Joe care File Added: editors.patch
2020-09-20 10:03 Florian Assigned To => Florian
2020-09-20 10:03 Florian Status new => resolved
2020-09-20 10:03 Florian Resolution open => won't fix
2020-09-20 10:03 Florian FPCTarget => -
2020-09-20 10:03 Florian Note Added: 0125652
2020-09-21 21:01 Joe care Note Added: 0125732