[eio] Fix memory leak in eio__scandir()

Marc Lehmann schmorp at schmorp.de
Tue Sep 13 07:14:41 CEST 2011

On Mon, Sep 12, 2011 at 05:28:36PM +0200, Ben Noordhuis <info at bnoordhuis.nl> wrote:
> eio__scandir() leaks memory when opendir() returns NULL. It sets
> req->result == -1 but doesn't free the memory it allocates.

Hmm, cannot reproduce this here, do you have a testcase that shows this

> Attached are two patches (different takes, same solution) that fix

Neither of these patches seem to actually do anything (they just free the
data earlier) - both ptr1 and ptr2 get freed in eio_destroy, which is
called after the user callback returns. If your patches fix a memleak,
then there must be a bug somewhere else.

> 1. doesn't set req->errorno when opendir() fails (both patches fix that), and

There is actually no possible codepath in libeio that doesn't set errorno
(it's set at the end of eio_execute), and it is certainly set when I just
tried to open a nonexistand path. Do you have a testcase that shows this?

> 2. uses readdir(), which is not guaranteed to be thread-safe. glibc

libeio doesn't rely on it being threadsafe - it never calls readdir from
multiple threads on the same dirstream.

a lock inside readdir doesn't make it threadsafe, btw., as readdir can
return a pointer to a per-dirstream structure that changes between calls.

                The choice of a       Deliantra, the free code+content MORPG
      -----==-     _GNU_              http://www.deliantra.net
      ----==-- _       generation
      ---==---(_)__  __ ____  __      Marc Lehmann
      --==---/ / _ \/ // /\ \/ /      schmorp at schmorp.de
      -=====/_/_//_/\_,_/ /_/\_\

More information about the libev mailing list