Project

General

Profile

Feature #12656 » 0001-Expand-short-paths-with-File.expand_path.patch

davispuh (Dāvis Mosāns), 08/04/2016 08:01 PM

View differences:

win32/file.c
/* MultiByteToWideChar() doesn't work with code page 51932 */
#define INVALID_CODE_PAGE 51932
#define PATH_BUFFER_SIZE MAX_PATH * 2
#define insecure_obj_p(obj, level) ((level) > 0 && OBJ_TAINTED(obj))
......
VALUE
rb_file_expand_path_internal(VALUE fname, VALUE dname, int abs_mode, int long_name, VALUE result)
{
size_t size = 0, whome_len = 0;
size_t size = 0, size_new = 0, whome_len = 0;
size_t buffer_len = 0;
long wpath_len = 0, wdir_len = 0;
char *fullpath = NULL;
......
wchar_t *whome = NULL, *buffer = NULL, *buffer_pos = NULL;
UINT path_cp, cp;
VALUE path = fname, dir = dname;
wchar_t wfullpath_buffer[PATH_BUFFER_SIZE];
wchar_t path_drive = L'\0', dir_drive = L'\0';
int ignore_dir = 0;
rb_encoding *path_encoding;
......
if (!tainted && PathIsRelativeW(buffer) && !(buffer_len >= 2 && IS_DIR_UNC_P(buffer)))
tainted = 1;
/* FIXME: Make this more robust */
/* Determine require buffer size */
size = GetFullPathNameW(buffer, PATH_BUFFER_SIZE, wfullpath_buffer, NULL);
if (size > PATH_BUFFER_SIZE) {
/* allocate more memory than alloted originally by PATH_BUFFER_SIZE */
wfullpath = (wchar_t *)xmalloc(size * sizeof(wchar_t));
size = GetFullPathNameW(buffer, size, wfullpath, NULL);
}
else {
wfullpath = wfullpath_buffer;
size = GetFullPathNameW(buffer, 0, NULL, NULL);
wfullpath = (wchar_t *)xmalloc(size * sizeof(wchar_t));
size = GetFullPathNameW(buffer, size, wfullpath, NULL);
if (long_name) {
size_new = GetLongPathNameW(wfullpath, NULL, 0);
if (size_new > 0) {
wfullpath = (wchar_t *)xrealloc(wfullpath, size_new * sizeof(wchar_t));
size = size_new;
size_new = GetLongPathNameW(wfullpath, wfullpath, size);
if (size_new > 0) size = size_new;
}
}
/* Remove any trailing slashes */
......
/* removes trailing invalid ':$DATA' */
size = remove_invalid_alternative_data(wfullpath, size);
/* Replace the trailing path to long name */
if (long_name) {
size_t bufsize = wfullpath == wfullpath_buffer ? PATH_BUFFER_SIZE : 0;
size = replace_to_long_name(&wfullpath, size, bufsize);
}
/* sanitize backslashes with forwardslashes */
replace_wchar(wfullpath, L'\\', L'/');
......
if (whome)
xfree(whome);
if (wfullpath != wfullpath_buffer)
if (wfullpath)
xfree(wfullpath);
if (fullpath)
    (1-1/1)