gen2brain / go-unarr

Go bindings for unarr (decompression library for RAR, TAR, ZIP and 7z archives)
zlib License
282 stars 44 forks source link

File descriptor leak in example #2

Closed campbellr closed 7 years ago

campbellr commented 7 years ago

The example in the README shows:

a, err := unarr.NewArchive("test.7z")
if err != nil {
    panic(err)
}
defer a.Close()

I found out the hard way that this ends up leaking file descriptors, which was unexpected (to me). I had assumed that if NewArchive there would be no need to call Close. (that is, for example, how archive/zip.OpenReader behaves)

Looking through the code, the fd returned by C.ar_open_file(p) is created but not cleaned if a.open fails.

Is this expected behavior? If so, it would be helpful to update the docs. If not, a.stream should probably be closed if a.open fails`

I would be happy to submit a PR for either.

gen2brain commented 7 years ago

You are right, a.stream should be closed if open() doesn't find valid archive. I can fix that later today, PR would be also great, thanks!

campbellr commented 7 years ago

Awesome, thanks for this (and the library in general)!

gen2brain commented 7 years ago

No problem. Glad you've found the package useful.