2
0
Эх сурвалжийг харах

Add negative count checks on buffered read/write loops.

A negative count should never reach these loops, but if it did somehow
happen, it could overrun the buffer, and we don't want that.
Jordan Russell 2 долоо хоног өмнө
parent
commit
4f364ac064

+ 3 - 6
Projects/Src/Compiler.CompressionHandler.pas

@@ -236,19 +236,16 @@ procedure TCompressionHandler.CompressFile(const SourceFile: TFile;
 var
   Context: TSHA256Context;
   AddrOffset: LongWord;
-  BufSize: Cardinal;
   Buf: array[0..65535] of Byte;
   { ^ *must* be the same buffer size used in Setup (TFileExtractor), otherwise
     the TransformCallInstructions call will break }
 begin
   SHA256Init(Context);
   AddrOffset := 0;
-  while True do begin
-    BufSize := SizeOf(Buf);
+  while Bytes > 0 do begin
+    var BufSize: Cardinal := SizeOf(Buf);
     if Bytes < BufSize then
-      BufSize := Bytes;
-    if BufSize = 0 then
-      Break;
+      BufSize := Cardinal(Bytes);
 
     SourceFile.ReadBuffer(Buf, BufSize);
     Inc(FChunkBytesRead, BufSize);

+ 6 - 12
Projects/Src/Setup.FileExtractor.pas

@@ -202,15 +202,12 @@ procedure TFileExtractor.SeekTo(const FL: TSetupFileLocationEntry;
   procedure Discard(Count: Int64);
   var
     Buf: array[0..65535] of Byte;
-    BufSize: Cardinal;
   begin
     try
-      while True do begin
-        BufSize := SizeOf(Buf);
+      while Count > 0 do begin
+        var BufSize: Cardinal := SizeOf(Buf);
         if Count < BufSize then
-          BufSize := Count;
-        if BufSize = 0 then
-          Break;
+          BufSize := Cardinal(Count);
         DecompressBytes(Buf, BufSize);
         Dec(Count, BufSize);
         if Assigned(ProgressProc) then
@@ -314,7 +311,6 @@ procedure TFileExtractor.DecompressFile(const FL: TSetupFileLocationEntry;
 var
   Context: TSHA256Context;
   AddrOffset: LongWord;
-  BufSize: Cardinal;
   Buf: array[0..65535] of Byte;
   { ^ *must* be the same buffer size used by the compiler (TCompressionHandler),
     otherwise the TransformCallInstructions call will break }
@@ -337,12 +333,10 @@ begin
 
     try
       AddrOffset := 0;
-      while True do begin
-        BufSize := SizeOf(Buf);
+      while BytesLeft > 0 do begin
+        var BufSize: Cardinal := SizeOf(Buf);
         if BytesLeft < BufSize then
-          BufSize := BytesLeft;
-        if BufSize = 0 then
-          Break;
+          BufSize := Cardinal(BytesLeft);
 
         DecompressBytes(Buf, BufSize);
         if floCallInstructionOptimized in FL.Flags then begin

+ 3 - 6
Projects/Src/Setup.Install.HelperFunc.pas

@@ -298,7 +298,6 @@ procedure CopySourceFileToDestFile(const SourceF, DestF: TFile;
 { Copies all bytes from SourceF to DestF, incrementing process meter as it
   goes. Assumes file pointers of both are 0. }
 var
-  BufSize: Cardinal;
   Buf: array[0..16383] of Byte;
   Context: TSHA256Context;
 begin
@@ -322,12 +321,10 @@ begin
   DestF.Truncate;
   DestF.Seek(0);
 
-  while True do begin
-    BufSize := SizeOf(Buf);
+  while BytesLeft > 0 do begin
+    var BufSize: Cardinal := SizeOf(Buf);
     if BytesLeft < BufSize then
-      BufSize := BytesLeft;
-    if BufSize = 0 then
-      Break;
+      BufSize := Cardinal(BytesLeft);
 
     SourceF.ReadBuffer(Buf, BufSize);
     DestF.WriteBuffer(Buf, BufSize);