wimlib icon indicating copy to clipboard operation
wimlib copied to clipboard

Fixed realpath return pointer to allocation allocated by the LibC

Open erin-desu opened this issue 1 year ago • 1 comments

Wimlib supports setting its own allocation hooks. I tried writing custom allocator, but then I encountered error when it tried to read off-bounds. Found out the allocation comes from LibC, to be specific, its a buffer allocated by realpath when a parameter for output buffer is specified as NULL (which is with wimlib in all cases).

So I created a wrapper called wimlib_realpath, which in case of the NULL parameter, after realpath returns reallocates and copies the buffer over with wimlib's allocator hook (memdup).

Originally, I allocated a buffer manually, but PATH_MAX, at least on my system has a size of one page which I considered too much and rather chose to reallocate.

erin-desu avatar Dec 23 '24 10:12 erin-desu

So I created a wrapper called wimlib_realpath, which in case of the NULL parameter, after realpath returns reallocates and copies the buffer over with wimlib's allocator hook (memdup).

Note that this fix implies that using malloc and free is still fine -- it's just the mismatch that causes the problem. I think that calls into question the purpose of setting a custom allocator in the first place.

If custom allocator is really to be supported, then no allocation using malloc should occur.

ebiggers avatar Dec 23 '24 19:12 ebiggers