[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[Libevent-users] Fwd: leaking munmap





---------- Forwarded message ----------
From: Black Hole <randomblackhole@xxxxxxxxx>
Date: Tue, Jul 30, 2013 at 3:46 PM
Subject: leaking munmap
To: majordomo@xxxxxxxx


Using 2.1-alpha.

In evbuffer_file_segment_free, buffer.c:3077

I think the line:
if (munmap(seg->mapping, seg->length) == -1)
should read:
off_t offset_leftover = seg->file_offset & (page_size - 1);
if (munmap(seg->mapping, seg->length + offset_leftover) == -1)

Without that change, it would leak memory and kernel descriptors.


On a related note, would there be any benefit in caching the pagesize instead of calling sysconf() each time? Something like:
/* pagesize should be a power of 2 */
long getpagesize_(void) {
    static long page_size = -2;
    if (page_size == -2) {
#ifdef SC_PAGE_SIZE
        page_size = sysconf(SC_PAGE_SIZE);
#elif defined(_SC_PAGE_SIZE)
        page_size = sysconf(_SC_PAGE_SIZE);
#else   
        page_size = 4096;
#endif
    }
    return page_size;
}

Also, in evbuffer_file_segment_materialize() around line 2980, the calculation of "offset_leftover" we can remove the modulus in favour of a mask.
long page_size = getpagesize_();
if (page_size == -1)
        goto err;
offset_leftover = offset & (page_size - 1);


Regards,
Bob