瀏覽代碼

* guard agains empty arrays in a few tArrayHelper binsearch methods
fixes bug #39761

(cherry picked from commit 471028193656a9e9221e93679ff35ba59b0e6e54)

marcoonthegit 3 年之前
父節點
當前提交
745e109411
共有 1 個文件被更改,包括 13 次插入0 次删除
  1. 13 0
      packages/rtl-generics/src/generics.collections.pas

+ 13 - 0
packages/rtl-generics/src/generics.collections.pas

@@ -1055,6 +1055,13 @@ class function TArrayHelper<T>.BinarySearch(constref AValues: array of T; constr
 var
 var
   imin, imax, imid: Int32;
   imin, imax, imid: Int32;
 begin
 begin
+  if Length(AValues) = 0 then
+  begin
+    ASearchResult.CompareResult := 0;
+    ASearchResult.FoundIndex := -1;
+    ASearchResult.CandidateIndex := -1;
+    Exit(False);
+  end;
   // continually narrow search until just one element remains
   // continually narrow search until just one element remains
   imin := AIndex;
   imin := AIndex;
   imax := Pred(AIndex + ACount);
   imax := Pred(AIndex + ACount);
@@ -1119,6 +1126,12 @@ var
   imin, imax, imid: Int32;
   imin, imax, imid: Int32;
   LCompare: SizeInt;
   LCompare: SizeInt;
 begin
 begin
+  if Length(AValues) = 0 then
+  begin
+    AFoundIndex := -1;
+    Exit(False);
+  end;
+
   // continually narrow search until just one element remains
   // continually narrow search until just one element remains
   imin := AIndex;
   imin := AIndex;
   imax := Pred(AIndex + ACount);
   imax := Pred(AIndex + ACount);