View Issue Details

IDProjectCategoryView StatusLast Update
0038930FPCPackagespublic2021-06-08 08:59
ReporterHendi Assigned ToMichael Van Canneyt  
PrioritynormalSeverityminorReproducibilityalways
Status resolvedResolutionfixed 
Product Version3.3.1 
Summary0038930: [Patch] Fix TDecompressionStream.Seek in paszlib
DescriptionI found that if you call Read/Seek in an unfortunate order, decompression will fail with a data error. This is because Seek was passing the same buffer that Read was using to store data from the source stream. Once zlib did a flush, it overwrote the source data.

The patch fix_decompress_seek.patch fixes this by utilizing an 8 KB stack buffer in Seek instead of FBuffer.

While fixing this I also noticed that paszlib doesn't compile when using the ZLIB_DEBUG define. This was because some of the debug code calls IntToStr, but SysUtils was not included. Furthermore the Assert function was redefined which caused issues, so I removed it because there is no reason not to use the RTL Assert. These changes can be found in fix_zlib_debug.patch.
TagsNo tags attached.
Fixed in Revision49490,49492
FPCOldBugId
FPCTarget-
Attached Files

Activities

Hendi

2021-05-24 20:11

reporter  

fix_decompress_seek.patch (718 bytes)   
Index: packages/paszlib/src/zstream.pp
===================================================================
--- packages/paszlib/src/zstream.pp	(revision 49398)
+++ packages/paszlib/src/zstream.pp	(working copy)
@@ -325,6 +325,7 @@
 function Tdecompressionstream.Seek(const Offset: Int64; Origin: TSeekOrigin): Int64;
 
 var c,off: int64;
+    buf: array[0..8191] of Byte;
 
 begin
   off:=Offset;
@@ -344,9 +345,9 @@
   while off>0 do
     begin
       c:=off;
-      if c>bufsize then
-        c:=bufsize;
-      if read(Fbuffer^,c)<>c then
+      if c>SizeOf(buf) then
+        c:=SizeOf(buf);
+      if read(buf,c)<>c then
         raise Edecompressionerror.create(Sseek_failed);
       dec(off,c);
     end;
fix_decompress_seek.patch (718 bytes)   
fix_zlib_debug.patch (3,448 bytes)   
Index: packages/paszlib/src/infblock.pas
===================================================================
--- packages/paszlib/src/infblock.pas	(revision 49398)
+++ packages/paszlib/src/infblock.pas	(working copy)
@@ -45,7 +45,7 @@
 implementation
 
 uses
-  infcodes, inftrees, infutil;
+  infcodes, inftrees, infutil{$IFDEF ZLIB_DEBUG}, SysUtils{$ENDIF};
 
 { Tables for deflate from PKZIP's appnote.txt. }
 Const
Index: packages/paszlib/src/infcodes.pas
===================================================================
--- packages/paszlib/src/infcodes.pas	(revision 49398)
+++ packages/paszlib/src/infcodes.pas	(working copy)
@@ -31,7 +31,7 @@
 implementation
 
 uses
-  infutil, inffast;
+  infutil, inffast{$IFDEF ZLIB_DEBUG}, SysUtils{$ENDIF};
 
 
 function inflate_codes_new (bl : cardinal;
@@ -185,7 +185,7 @@
         if (t^.base >= $20) and (t^.base < $7f) then
           Tracevv('inflate:         literal '+char(t^.base))
         else
-          Tracevv('inflate:         literal '+IntToStr(t^.base));
+          Tracevv('inflate:         literal 0x'+IntToHex(t^.base, 2));
         {$ENDIF}          
         c^.mode := LIT;
         continue;  { break switch statement }
Index: packages/paszlib/src/inffast.pas
===================================================================
--- packages/paszlib/src/inffast.pas	(revision 49398)
+++ packages/paszlib/src/inffast.pas	(working copy)
@@ -29,7 +29,7 @@
 implementation
 
 uses
-  infutil;
+  infutil{$IFDEF ZLIB_DEBUG}, SysUtils{$ENDIF};
 
 
 { Called with number of bytes left to write in window at least 258
Index: packages/paszlib/src/zbase.pas
===================================================================
--- packages/paszlib/src/zbase.pas	(revision 49398)
+++ packages/paszlib/src/zbase.pas	(working copy)
@@ -380,10 +380,6 @@
   PRESET_DICT = $20; { preset dictionary flag in zlib header }
 
 
-  {$IFDEF ZLIB_DEBUG}
-  procedure Assert(cond : boolean; msg : string);
-  {$ENDIF}
-
   procedure Trace(x : string);
   procedure Tracev(x : string);
   procedure Tracevv(x : string);
@@ -461,12 +457,6 @@
   Halt(1);
 end;
 
-procedure Assert(cond : boolean; msg : string);
-begin
-  if not cond then
-    z_error(msg);
-end;
-
 procedure Trace(x : string);
 begin
   WriteLn(x);
Index: packages/paszlib/src/zdeflate.pas
===================================================================
--- packages/paszlib/src/zdeflate.pas	(revision 49398)
+++ packages/paszlib/src/zdeflate.pas	(working copy)
@@ -1502,6 +1502,27 @@
 {$endif} { FASTEST }
 
 {$ifdef ZLIB_DEBUG}
+function zmemcmp(s1p, s2p : PByte; len : Cardinal) : Integer;
+var
+  j : Cardinal;
+  source,
+  dest : PByte;
+begin
+  source := s1p;
+  dest := s2p;
+  for j := 0 to pred(len) do
+  begin
+    if (source^ <> dest^) then
+    begin
+      zmemcmp := 2*Ord(source^ > dest^)-1;
+      exit;
+    end;
+    Inc(source);
+    Inc(dest);
+  end;
+  zmemcmp := 0;
+end;
+
 { ===========================================================================
   Check that the match at match_start is indeed a match. }
 
Index: packages/paszlib/src/zconf.inc
===================================================================
--- packages/paszlib/src/zconf.inc	(revision 49398)
+++ packages/paszlib/src/zconf.inc	(working copy)
@@ -36,3 +36,9 @@
  {$UNDEF MAXSEG_64K}
  {$UNDEF Delphi32}
 {$ENDIF}
+
+{- $DEFINE ZLIB_DEBUG}
+
+{$IFDEF ZLIB_DEBUG}
+  {$ASSERTIONS ON}
+{$ENDIF}
fix_zlib_debug.patch (3,448 bytes)   

Marco van de Voort

2021-06-08 08:59

manager   ~0131196

Last edited: 2021-06-08 08:59

View 2 revisions

Checked and committed, thanks, with only modification substituting 0x with $ since that is the Pascal convention.

Issue History

Date Modified Username Field Change
2021-05-24 20:11 Hendi New Issue
2021-05-24 20:11 Hendi File Added: fix_decompress_seek.patch
2021-05-24 20:11 Hendi File Added: fix_zlib_debug.patch
2021-05-24 21:17 Michael Van Canneyt Assigned To => Michael Van Canneyt
2021-05-24 21:17 Michael Van Canneyt Status new => assigned
2021-06-08 08:59 Marco van de Voort Status assigned => resolved
2021-06-08 08:59 Marco van de Voort Resolution open => fixed
2021-06-08 08:59 Marco van de Voort Fixed in Revision => 49490,49492
2021-06-08 08:59 Marco van de Voort FPCTarget => -
2021-06-08 08:59 Marco van de Voort Note Added: 0131196
2021-06-08 08:59 Marco van de Voort Note Edited: 0131196 View Revisions