[erlang-questions] memory leak in zlib:unzip

Justus <>
Tue Mar 6 06:58:23 CET 2012


Hi all,

I found that when exceptions occur in zlib:unzip, the zlib_drv port
will be left open. So the system will run out of ports quickly.

So, when we call zlib:unzip, we must insure that the stream can be
unzipped successfully. But how could we know that without unzip it?

Maybe we need a try_unzip. But if we have try_unzip, do we need
zlib:unzip anymore? Quite a paradox here.

try_unzip(Data) ->
    Z = zlib:open(),
    zlib:inflateInit(Z, -?MAX_WBITS),
    R = case (catch zlib:inflate(Z, Data)) of
        {'EXIT', _Reason} ->
            error;
        Bs ->
            {ok, iolist_to_binary(Bs)}
    end,
    (catch zlib:inflateEnd(Z)),
    zlib:close(Z),
    R.

On Tue, Mar 6, 2012 at 11:22 AM, Justus <> wrote:
> Hi all,
>
> I think there are memory leaks in zlib:unzip, when it gets a wrong
> stream. Test code is listed below.
>
> check_erl() ->
>    Bin = erlang:list_to_binary([X rem 256 || X <- lists:seq(0, 10000)]),
>    check0(Bin, 0).
>
> check0(Bin, Off) when Off > byte_size(Bin) -> 0;
> check0(Bin, Off) ->
>    <<_X:Off/binary, SB/binary>> = Bin,
>    (catch zlib:unzip(SB)),
>    check0(Bin, Off + 1).
>
> Have a check on erlang:memory() before and after check_erl() is called.
>
> --
> Best Regards,
> Justus



-- 
Best Regards,
Justus



More information about the erlang-questions mailing list