View Issue Details

IDProjectCategoryView StatusLast Update
0037777FPCFree Visionpublic2020-09-21 22:17
ReporterJoe care Assigned To 
PrioritynormalSeverityminorReproducibilityalways
Status newResolutionopen 
Platformx86-64OSWin64 
Product Version3.3.1 
Summary0037777: (HistList.pas) Length of string with length() and SetLength instead of char at index 0
DescriptionHandling the length of a string should be done with setlength and length.
Additional InformationPatch provided
  & UnitTest
TagsStyle Improvement
Fixed in Revision
FPCOldBugId
FPCTarget
Attached Files

Activities

Joe care

2020-09-19 16:01

reporter  

histlist.patch (2,708 bytes)   
Index: histlist.pas
===================================================================
--- histlist.pas	(Revision 46893)
+++ histlist.pas	(Arbeitskopie)
@@ -243,32 +243,38 @@
 {  InsertString -> Platforms DOS/DPMI/WIN/NT/OS2 - Updated 30Sep99 LdB      }
 {---------------------------------------------------------------------------}
 PROCEDURE InsertString (Id: Byte; Const Str: String);
-VAR P, P1, P2: PChar;
+VAR P, P1, P2: PChar; 
+  L: Integer;
 BEGIN
-  while (HistoryUsed+Length(Str)+3>HistorySize) do
+  L := Length(Str);
+  if L>255 then
+    L := 255;
+  while (HistoryUsed + L + 3 > HistorySize) do
    begin
-       P:=PChar(HistoryBlock);
-       while Pointer(P)<Pointer(HistoryBlock)+HistorySize do
+       P := PChar(HistoryBlock);
+       while Pointer(P) < Pointer(HistoryBlock) + HistorySize do
          begin
-           if Pointer(P)+Length(PShortString(P+2)^)+6+Length(Str) >
-              Pointer(HistoryBlock)+HistorySize then
+           if Pointer(P) + Length(PShortString(P + 2)^) + 6 + L >
+              Pointer(HistoryBlock) + HistorySize then
              begin
-               Dec(HistoryUsed,Length(PShortString(P+2)^)+3);
-               FillChar(P^,Pointer(HistoryBlock)+HistorySize-Pointer(P),#0);
+               Dec(HistoryUsed, Length(PShortString(P + 2)^) + 3);
+               FillChar(P^, Pointer(HistoryBlock) + HistorySize - Pointer(P), #0);
                break;
              end;
-           Inc(P,Length(PShortString(P+2)^)+3);
+           Inc(P, Length(PShortString(P + 2)^) + 3);
          end;
    end;
-   P1 := PChar(HistoryBlock)+1;                     { First history record }
-   P2 := P1+Length(Str)+3;                          { History record after }
+   P1 := PChar(HistoryBlock) + 1;                   { First history record }
+   P2 := P1 + L + 3;                                { History record after }
    Move(P1^, P2^, HistoryUsed - 1);                 { Shuffle history data }
-   P1^:=#0;                         { Set marker byte }
+   P1^ := #0;                                       { Set marker byte }
    Inc(P1);
-   P1^:=Chr(Id);                          { Set history id }
+   P1^ := Chr(Id);                                  { Set history id }
    Inc(P1);
-   Move(Str[0], P1^, Length(Str)+1);  { Set history string }
-   Inc(HistoryUsed, Length(Str)+3);                 { Inc history used }
+   P1^ := Chr(L);                                   { Set length of string }
+   Inc(P1);
+   Move(Str[1], P1^, L);                            { Set history string }
+   Inc(HistoryUsed, L + 3);                         { Inc history used }
 END;
 
 {***************************************************************************}
histlist.patch (2,708 bytes)   
tst_FVHistList.pas (3,987 bytes)   
unit tst_FVHistList;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, fpcunit, testutils, testregistry, HistList;

type

  { TTestFVHistList }

  TTestFVHistList= class(TTestCase)
  protected
    procedure SetUp; override;
    procedure TearDown; override;
    Procedure GenerateTestData;
  published
    procedure TestSetUp;
    procedure TestInsert;
    procedure TestInsert2;
    procedure TestDelete;
  end;

implementation

procedure TTestFVHistList.TestSetUp;
begin
  CheckNotNull(TObject(HistList.HistoryBlock),'Historyblock is assigned');
  CheckEquals(0,HistList.HistoryCount(0),'No Entries');
  CheckEquals(65536,HistList.HistorySize,'HistorySize');
end;

procedure TTestFVHistList.TestInsert;
begin
  HistList.HistoryAdd(1,'Eins');
  CheckEquals(1,HistList.HistoryCount(1),'1 Entrie');
  CheckEquals(65536,HistList.HistorySize,'HistorySize');
  CheckEquals(8,HistList.HistoryUsed,'HistoryUsed1');
  HistList.HistoryAdd(1,'Zwei');
  CheckEquals(2,HistList.HistoryCount(1),'2 Entries');
  CheckEquals(65536,HistList.HistorySize,'HistorySize');
  CheckEquals(15,HistList.HistoryUsed,'HistoryUsed2');
  HistList.HistoryAdd(1,'Drei');
  CheckEquals(3,HistList.HistoryCount(1),'3 Entrie');
  CheckEquals(65536,HistList.HistorySize,'HistorySize');
  CheckEquals(22,HistList.HistoryUsed,'HistoryUsed3');
  HistList.HistoryAdd(1,'Vier');
  CheckEquals(4,HistList.HistoryCount(1),'4 Entries');
  CheckEquals(65536,HistList.HistorySize,'HistorySize');
  CheckEquals(29,HistList.HistoryUsed,'HistoryUsed4');
  checkEquals('Vier',HistoryStr(1,0),'Index 0');
  checkEquals('Drei',HistoryStr(1,1),'Index 1');
  checkEquals('Zwei',HistoryStr(1,2),'Index 2');
  checkEquals('Eins',HistoryStr(1,3),'Index 3');
  checkEquals('',HistoryStr(1,4),'Index 4');
end;

procedure TTestFVHistList.TestInsert2;
var
  i, j: Integer;
begin
  GenerateTestData;
  for i := 0 to 100 do
     for j := 0 to 28 do
        CheckEquals('List['+inttostr(j)+']: Entry['+inttostr(100-i)+']',HistoryStr(j,i));
end;

procedure TTestFVHistList.TestDelete;
begin
  //Todo :
end;

procedure TTestFVHistList.SetUp;
begin
  HistList.InitHistory;
end;

procedure TTestFVHistList.TearDown;
begin
  HistList.DoneHistory;
end;

procedure TTestFVHistList.GenerateTestData;
var
  i, j: Integer;
begin
  for i := 0 to 100 do
     for j := 0 to 28 do
        HistList.HistoryAdd(j,'List['+inttostr(j)+']: Entry['+inttostr(i)+']');
  CheckEquals(63168,HistList.HistoryUsed,'HistoryUsed1');
  HistList.HistoryAdd(30,'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, '+
  'sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, '+
  'sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet '+
  'clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. '+
  'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod '+
  'tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. '+
  'At vero eos et accusam et justo duo dolores et ea rebum. '+
  'Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. '+
  'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod '+
  'tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. '+
  'At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, '+
  'no sea takimata sanctus est Lorem ipsum dolor sit amet.'+LineEnding+LineEnding+
  'Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, '+
  'vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto '+
  'odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te '+
  'feugait nulla facilisi.');
  CheckEquals(63426,HistList.HistoryUsed,'HistoryUsed2');
end;

initialization

  RegisterTest(TTestFVHistList);
end.

tst_FVHistList.pas (3,987 bytes)   

Joe care

2020-09-21 21:04

reporter   ~0125733

Make this also a "Stype-Improvement" Request.

Sven Barth

2020-09-21 21:22

manager   ~0125735

Last edited: 2020-09-21 21:23

View 2 revisions

Please don't change the coding style/formatting just for the sake of changing it. It makes it really hard to see what changed.

Joe care

2020-09-21 22:17

reporter   ~0125740

Mostly I did'nt, but somebody else did, because the comments where out of column.
Then try this patch ...
histlist-2.patch (2,231 bytes)   
Index: histlist.pas
===================================================================
--- histlist.pas	(Revision 46893)
+++ histlist.pas	(Arbeitskopie)
@@ -243,32 +243,38 @@
 {  InsertString -> Platforms DOS/DPMI/WIN/NT/OS2 - Updated 30Sep99 LdB      }
 {---------------------------------------------------------------------------}
 PROCEDURE InsertString (Id: Byte; Const Str: String);
 VAR P, P1, P2: PChar;
+  L: Integer;
 BEGIN
+  L := Length(Str);
+  if L>255 then
+    L := 255;
-  while (HistoryUsed+Length(Str)+3>HistorySize) do
+  while (HistoryUsed+L+3>HistorySize) do
    begin
        P:=PChar(HistoryBlock);
        while Pointer(P)<Pointer(HistoryBlock)+HistorySize do
          begin
-           if Pointer(P)+Length(PShortString(P+2)^)+6+Length(Str) >
+           if Pointer(P)+Length(PShortString(P+2)^)+6+L>
               Pointer(HistoryBlock)+HistorySize then
              begin
                Dec(HistoryUsed,Length(PShortString(P+2)^)+3);
                FillChar(P^,Pointer(HistoryBlock)+HistorySize-Pointer(P),#0);
                break;
              end;
            Inc(P,Length(PShortString(P+2)^)+3);
          end;
    end;
    P1 := PChar(HistoryBlock)+1;                     { First history record }
-   P2 := P1+Length(Str)+3;                          { History record after }
+   P2 := P1+L+3;                                    { History record after }
    Move(P1^, P2^, HistoryUsed - 1);                 { Shuffle history data }
-   P1^:=#0;                         { Set marker byte }
+   P1^:=#0;                                         { Set marker byte }
    Inc(P1);
-   P1^:=Chr(Id);                          { Set history id }
+   P1^:=Chr(Id);                                    { Set history id }
    Inc(P1);
-   Move(Str[0], P1^, Length(Str)+1);  { Set history string }
-   Inc(HistoryUsed, Length(Str)+3);                 { Inc history used }
+   P1^:=Chr(L);                                     { Set length of string }
+   Inc(P1);
+   Move(Str[1], P1^, L);                            { Set history string }
+   Inc(HistoryUsed, L+3);                           { Inc history used }
 END;
 
 {***************************************************************************}
histlist-2.patch (2,231 bytes)   

Issue History

Date Modified Username Field Change
2020-09-19 16:01 Joe care New Issue
2020-09-19 16:01 Joe care File Added: histlist.patch
2020-09-19 16:01 Joe care File Added: tst_FVHistList.pas
2020-09-21 21:02 Joe care Tag Attached: Style Improvement
2020-09-21 21:04 Joe care Note Added: 0125733
2020-09-21 21:22 Sven Barth Note Added: 0125735
2020-09-21 21:23 Sven Barth Note Edited: 0125735 View Revisions
2020-09-21 22:17 Joe care Note Added: 0125740
2020-09-21 22:17 Joe care File Added: histlist-2.patch