#34 stat fails on directory ending with slash

Unstable (example)
closed-fixed
nobody
CRT (12)
5
2014-03-12
2010-12-01
Pascal Obry
No

See attached patch which fixes this issue on _stat.c and _wstat.c.

Discussion

1 2 3 > >> (Page 1 of 3)
  • Pascal Obry
    Pascal Obry
    2010-12-01

    I forgot to say that this has been tested on i686 and x86_64.

     
  • Kai Tietz
    Kai Tietz
    2010-12-01

    • status: open --> open-accepted
     
  • Kai Tietz
    Kai Tietz
    2010-12-01

    Thanks for the patch. I will give it some test. Maybe it would be good to add here a testcase for those stat function- I want to verify if the code here handles '/' and UNC correct.

    Kai

     
  • Ozkan Sezer
    Ozkan Sezer
    2010-12-01

    • status: open-accepted --> open
     
  • Ozkan Sezer
    Ozkan Sezer
    2010-12-01

    As I understand it, stat("C:\\") or stat("C:/") are OK (or, ignored by the workaround). How about stat("/") or stat("\\") ?

     
  • Pascal Obry
    Pascal Obry
    2010-12-01

    Thanks for the quick feedback. Here is a small test case I have used:

    #include <stdio.h>
    #include <sys/stat.h>

    int
    main (int argc, char **argv)
    {
    struct stat buf;
    char *f1 = "c:\\windows\\";
    char *f2 = "c:\\";
    wchar_t *wf1 = L"c:\\windows\\";
    wchar_t *wf2 = L"c:\\";

    printf ("stat(%s) = %d\n", f1, stat(f1, &buf));
    printf ("stat(%s) = %d\n", f2, stat(f2, &buf));

    wprintf (L"stat(%s) = %d\n", wf1, wstat(wf1, &buf));
    wprintf (L"stat(%s) = %d\n", wf2, wstat(wf2, &buf));
    }

     
  • Pascal Obry
    Pascal Obry
    2010-12-01

    > As I understand it, stat("C:\\") or stat("C:/") are OK (or, ignored by the
    > workaround).

    Right.

    > How about stat("/") or stat("\\") ?

    Not tested, I'll update my patch which is probably broken here.

     
  • Kai Tietz
    Kai Tietz
    2010-12-01

    I suggest the following pattern (here for _stat, but same for _wstat with different type)

    [code]
    static char*
    _mingw_no_trailing_slash (const char* _path)
    {
    int len;
    char *p;
    int start;

    p = (char*)_path;

    if (_path && *_path) {
    len = strlen (_path);

    /* Ignore X:\ */

    if ((len == 2 || len == 3) && _path[1] == ':')
    return p;
    if (len <= 1)
    return p;
    /* Check UNC \\abc\&lt;name>\ */
    if ((_path[0] == '\\' || _path[0] == '/')
    && (_path[1] == '\\' || _path[1] == '/'))
    {
    const char *r = &_path[2];
    while (*r != 0 && *r != '\\' && *r != '/')
    ++r;
    if (*r != 0)
    ++r;
    if (*r == 0)
    return p;
    while (*r != 0 && *r != '\\' && *r != '/')
    ++r;
    if (*r != 0)
    ++r;
    if (*r == 0)
    return p;
    }

    if (_path [len - 1] == '/' || _path [len - 1] == '\\')
    {
    p = (char*)malloc (len);
    memcpy (p, _path, len - 1);
    p [len - 1] = '\0';
    }
    }

    return p;
    }
    [/code]

     

    Related

    Code: code

1 2 3 > >> (Page 1 of 3)