Recent

Author Topic: Issue with zlib and broken gz files  (Read 935 times)

ChristianH

  • New Member
  • *
  • Posts: 46
Issue with zlib and broken gz files
« on: January 27, 2023, 04:13:57 pm »
Hi,

i am aware that the Wrong signature $AAAAAAAA error I receive is when I free memory which is not allocated before. Usually I would do some sanity tests, but in this case it is in inflate_codes_free. It fails when the

  freemem(s.sub.trees.blens)

is executed. I attached the sample code which causes this problem. Maybe somebody has an idea how to solve this, without rewriting the gzip code.

balazsszekely

  • Guest
Re: Issue with zlib and broken gz files
« Reply #1 on: January 27, 2023, 05:14:46 pm »
Hi,

i am aware that the Wrong signature $AAAAAAAA error I receive is when I free memory which is not allocated before. Usually I would do some sanity tests, but in this case it is in inflate_codes_free. It fails when the

  freemem(s.sub.trees.blens)

is executed. I attached the sample code which causes this problem. Maybe somebody has an idea how to solve this, without rewriting the gzip code.
There is no issues at my side. No exception, no memory leak, the file is nicely unpacked. Tested with:
-  FPC 3.2.2
- Lazarus Trunk and Lazarus 2.2.4
- Windows 10(64 bit)

ChristianH

  • New Member
  • *
  • Posts: 46
Re: Issue with zlib and broken gz files
« Reply #2 on: January 27, 2023, 06:06:56 pm »
Thanks, this happen under the 32bit build with enabled Heaptrc in debug mode.
It seem to be that the inflate function returns an Z_MEM_ERROR which might be the problem.

Christian

balazsszekely

  • Guest
Re: Issue with zlib and broken gz files
« Reply #3 on: January 27, 2023, 06:29:04 pm »
Thanks, this happen under the 32bit build with enabled Heaptrc in debug mode.
Christian
Same here, no error. 

ChristianH

  • New Member
  • *
  • Posts: 46
Re: Issue with zlib and broken gz files
« Reply #4 on: January 27, 2023, 06:59:03 pm »
You have to enable heaprtc to see this. It should trigger straight out of the box when freemen is executed.

tetrastes

  • Sr. Member
  • ****
  • Posts: 473
Re: Issue with zlib and broken gz files
« Reply #5 on: January 27, 2023, 08:47:22 pm »
Your debug.gz is corrupted, according to 7zip and Far. May this is the cause of error.
Sorry, I was inattentive, you wrote that it's broken.
Though I don't get error with Laz 2.2.4 fpc 3.2.2 also as GetMem, it seems that your Lazarus is 2.0.x, according to version of .lpi, and maybe it's more "clever" in this case.
« Last Edit: January 27, 2023, 09:14:02 pm by tetrastes »

ChristianH

  • New Member
  • *
  • Posts: 46
Re: Issue with zlib and broken gz files
« Reply #6 on: January 27, 2023, 09:47:20 pm »
Yes, the gz is broken. I am not sure but I think I found the problem:

the following issue seem to be the reason for this.

In the inflate_blocks function, located in the infblock.pas:

t := inflate_trees_dynamic(257 + (t and $1f),
                  1 + ((t shr 5) and $1f),
                  s.sub.trees.blens^, bl, bd, tl, td, s.hufts^, z);   
freemem(s.sub.trees.blens);

This cleans up the trees.blens pointer, but it does not set it to NIL.
I assume you have to set the s.sub.trees.blens := nil afterwards. Otherwise you get a problem when you free up the structures.

It then will crash here:

procedure inflate_blocks_reset (var s : inflate_blocks_state;
                                var z : z_stream;
                                c : Pcardinal); { check value on output }
begin
  if (c <> nil) then
    c^ := s.check;
  if (s.mode = BTREE) or (s.mode = DTREE) then
    freemem(s.sub.trees.blens);                     
   
...

This is something happen very rarely and in my case (the app does not usually have heaprtc enabled when it is delivered to the users) very rarely. But every now and then a broken gz file can cause such a problem.

Christian
« Last Edit: January 27, 2023, 09:50:29 pm by ChristianH »

ChristianH

  • New Member
  • *
  • Posts: 46
Re: Issue with zlib and broken gz files
« Reply #7 on: January 27, 2023, 10:00:46 pm »
Oh they fixed this problem already last year:
https://github.com/fpc/FPCSource/blob/main/packages/paszlib/src/infblock.pas

Sorry for the post, maybe I should update my fpc.

Christian

 

TinyPortal © 2005-2018