Skip to content

Conversation

@erin-desu
Copy link

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.

Copy link
Owner

@ebiggers ebiggers left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you have a use case for setting a custom allocator? There are other cases in which it is known to not work, e.g. various allocations that occur in NTFS-3G and Windows modes. I feel it was kind of a mistake to support it. It makes sense in lower-level libraries like compression libraries, but wimlib is a bit higher level.

Would you mind squashing your commits and including your explanation in the commit message itself, not just in the pull request description, so it doesn't get lost? Thanks.

#define REALLOC wimlib_realloc
#define CALLOC wimlib_calloc
#define STRDUP wimlib_strdup
#define REALPATH wimlib_realpath
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to add this line


tchar *
realpath(const tchar *path, tchar *resolved_path);
wimlib_realpath(const tchar *path, tchar *resolved_path);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you drop the resolved_path argument and just use tchar *wimlib_realpath(const tchar *path)? Support for non-null resolved_path is not required, so this would be simpler.

return memdup(str, strlen(str) + 1);
}

char *
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to be under #ifndef _WIN32.

@ebiggers
Copy link
Owner

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants