diff options
author | Björn Gustavsson <[email protected]> | 2017-02-27 16:12:41 +0100 |
---|---|---|
committer | Björn Gustavsson <[email protected]> | 2017-02-28 09:52:28 +0100 |
commit | e06dc58a9db38ad349143629f4ba7411275183ab (patch) | |
tree | 261e599f9db5ffa3919c6152a2525c429fe694b0 /lib/stdlib/src/zip.erl | |
parent | 45a64bda7c605bb5effca0085e9df6f56ddba24a (diff) | |
download | otp-e06dc58a9db38ad349143629f4ba7411275183ab.tar.gz otp-e06dc58a9db38ad349143629f4ba7411275183ab.tar.bz2 otp-e06dc58a9db38ad349143629f4ba7411275183ab.zip |
zip: Eliminate leak of open file after crash
Make sure that zip:extract() and zip:create() closes the zip
file if there is an error.
ERL-349
Diffstat (limited to 'lib/stdlib/src/zip.erl')
-rw-r--r-- | lib/stdlib/src/zip.erl | 28 |
1 files changed, 20 insertions, 8 deletions
diff --git a/lib/stdlib/src/zip.erl b/lib/stdlib/src/zip.erl index 31438ae36b..fadf96146e 100644 --- a/lib/stdlib/src/zip.erl +++ b/lib/stdlib/src/zip.erl @@ -368,9 +368,12 @@ do_unzip(F, Options) -> {Info, In1} = get_central_dir(In0, RawIterator, Input), %% get rid of zip-comment Z = zlib:open(), - Files = get_z_files(Info, Z, In1, Opts, []), - zlib:close(Z), - Input(close, In1), + Files = try + get_z_files(Info, Z, In1, Opts, []) + after + zlib:close(Z), + Input(close, In1) + end, {ok, Files}. %% Iterate over all files in a zip archive @@ -447,11 +450,20 @@ do_zip(F, Files, Options) -> #zip_opts{output = Output, open_opts = OpO} = Opts, Out0 = Output({open, F, OpO}, []), Z = zlib:open(), - {Out1, LHS, Pos} = put_z_files(Files, Z, Out0, 0, Opts, []), - zlib:close(Z), - Out2 = put_central_dir(LHS, Pos, Out1, Opts), - Out3 = Output({close, F}, Out2), - {ok, Out3}. + try + {Out1, LHS, Pos} = put_z_files(Files, Z, Out0, 0, Opts, []), + zlib:close(Z), + Out2 = put_central_dir(LHS, Pos, Out1, Opts), + Out3 = Output({close, F}, Out2), + {ok, Out3} + catch + C:R -> + Stk = erlang:get_stacktrace(), + zlib:close(Z), + Output({close, F}, Out0), + erlang:raise(C, R, Stk) + end. + %% List zip directory contents %% |