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
25 25

  
26 26
/* MultiByteToWideChar() doesn't work with code page 51932 */
27 27
#define INVALID_CODE_PAGE 51932
28
#define PATH_BUFFER_SIZE MAX_PATH * 2
29 28

  
30 29
#define insecure_obj_p(obj, level) ((level) > 0 && OBJ_TAINTED(obj))
31 30

  
......
313 312
VALUE
314 313
rb_file_expand_path_internal(VALUE fname, VALUE dname, int abs_mode, int long_name, VALUE result)
315 314
{
316
    size_t size = 0, whome_len = 0;
315
    size_t size = 0, size_new = 0, whome_len = 0;
317 316
    size_t buffer_len = 0;
318 317
    long wpath_len = 0, wdir_len = 0;
319 318
    char *fullpath = NULL;
......
322 321
    wchar_t *whome = NULL, *buffer = NULL, *buffer_pos = NULL;
323 322
    UINT path_cp, cp;
324 323
    VALUE path = fname, dir = dname;
325
    wchar_t wfullpath_buffer[PATH_BUFFER_SIZE];
326 324
    wchar_t path_drive = L'\0', dir_drive = L'\0';
327 325
    int ignore_dir = 0;
328 326
    rb_encoding *path_encoding;
......
587 585
    if (!tainted && PathIsRelativeW(buffer) && !(buffer_len >= 2 && IS_DIR_UNC_P(buffer)))
588 586
	tainted = 1;
589 587

  
590
    /* FIXME: Make this more robust */
591 588
    /* Determine require buffer size */
592
    size = GetFullPathNameW(buffer, PATH_BUFFER_SIZE, wfullpath_buffer, NULL);
593
    if (size > PATH_BUFFER_SIZE) {
594
	/* allocate more memory than alloted originally by PATH_BUFFER_SIZE */
595
	wfullpath = (wchar_t *)xmalloc(size * sizeof(wchar_t));
596
	size = GetFullPathNameW(buffer, size, wfullpath, NULL);
597
    }
598
    else {
599
	wfullpath = wfullpath_buffer;
589
    size = GetFullPathNameW(buffer, 0, NULL, NULL);
590
    wfullpath = (wchar_t *)xmalloc(size * sizeof(wchar_t));
591
    size = GetFullPathNameW(buffer, size, wfullpath, NULL);
592

  
593
    if (long_name) {
594
        size_new = GetLongPathNameW(wfullpath, NULL, 0);
595
        if (size_new > 0) {
596
          wfullpath = (wchar_t *)xrealloc(wfullpath, size_new * sizeof(wchar_t));
597
          size = size_new;
598
          size_new = GetLongPathNameW(wfullpath, wfullpath, size);
599
          if (size_new > 0) size = size_new;
600
        }
600 601
    }
601 602

  
602 603
    /* Remove any trailing slashes */
......
616 617
    /* removes trailing invalid ':$DATA' */
617 618
    size = remove_invalid_alternative_data(wfullpath, size);
618 619

  
619
    /* Replace the trailing path to long name */
620
    if (long_name) {
621
	size_t bufsize = wfullpath == wfullpath_buffer ? PATH_BUFFER_SIZE : 0;
622
	size = replace_to_long_name(&wfullpath, size, bufsize);
623
    }
624

  
625 620
    /* sanitize backslashes with forwardslashes */
626 621
    replace_wchar(wfullpath, L'\\', L'/');
627 622

  
......
646 641
    if (whome)
647 642
	xfree(whome);
648 643

  
649
    if (wfullpath != wfullpath_buffer)
644
    if (wfullpath)
650 645
	xfree(wfullpath);
651 646

  
652 647
    if (fullpath)
653
-