浏览代码

* patch by Michalis Kamburelis to keep the ending of TFPSList filled with zeros (space between count and capacity), resolves #20005

git-svn-id: trunk@18237 -
florian 14 年之前
父节点
当前提交
9b88297389
共有 3 个文件被更改,包括 47 次插入0 次删除
  1. 1 0
      .gitattributes
  2. 5 0
      rtl/objpas/fgl.pp
  3. 41 0
      tests/webtbs/tw20005.pp

+ 1 - 0
.gitattributes

@@ -11728,6 +11728,7 @@ tests/webtbs/tw19960.pp svneol=native#text/pascal
 tests/webtbs/tw19974.pp svneol=native#text/pascal
 tests/webtbs/tw19977.pp svneol=native#text/pascal
 tests/webtbs/tw20003.pp svneol=native#text/pascal
+tests/webtbs/tw20005.pp svneol=native#text/pascal
 tests/webtbs/tw2001.pp svneol=native#text/plain
 tests/webtbs/tw2002.pp svneol=native#text/plain
 tests/webtbs/tw2004.pp svneol=native#text/plain

+ 5 - 0
rtl/objpas/fgl.pp

@@ -500,6 +500,11 @@ begin
     FCapacity := FCapacity shr 1;
     ReallocMem(FList, (FCapacity+1) * FItemSize);
   end;
+  { Keep the ending of the list filled with zeros, don't leave garbage data
+    there. Otherwise, we could accidentally have there a copy of some item
+    on the list, and accidentally Deref it too soon.
+    See http://bugs.freepascal.org/view.php?id=20005. }
+  FillChar(InternalItems[FCount]^, (FCapacity+1-FCount) * FItemSize, #0);
 end;
 
 procedure TFPSList.Extract(Item: Pointer; ResultPtr: Pointer);

+ 41 - 0
tests/webtbs/tw20005.pp

@@ -0,0 +1,41 @@
+{ %OPT=-gl -gh }
+{$mode objfpc}{$H+}
+uses Classes, SysUtils, FGL;
+
+type
+  TMessages = specialize TFPGList<string>;
+
+var
+  Messages: TMessages;
+
+procedure WritelnMessages(const S: string);
+var
+  I: Integer;
+begin
+  Writeln('Messages ', S, ' : ', Messages.Count);
+  for i := 0 to Messages.Count - 1 do
+    Writeln('  Messages[', I, ']: ', PtrUInt(Pointer(Messages[I])), ' ', Length(Messages[I]), ' ', Messages[I]);
+end;
+
+procedure Show(S: string);
+var
+  NewS: string;
+begin
+  WritelnMessages('before Add');
+  NewS := Copy(S, 1, 10) + Copy(S, 11, MaxInt);
+  Messages.Add(NewS);
+  WritelnMessages('after Add');
+end;
+
+begin
+  Messages := TMessages.Create;
+
+  Show('Loaded level "Castle Hall"');
+  Show('You pick "Sword"');
+  Show('You''re using weapon "Sword" now');
+  Show('Hint: press "Escape" for game menu');
+  Messages.Delete(0);
+  Show('You pick "Potion Of Life"');
+
+  FreeAndNil(Messages);
+end.