From 1b9376feda2a17f1dd728bc4ee2117f3b2cde567 Mon Sep 17 00:00:00 2001 From: Kai Tietz Date: Thu, 29 Dec 2011 14:44:40 +0100 Subject: [PATCH 2/7] msvcrt: Use LOCK_ENV locking in _chdrive and do validatation of input argument. --- dlls/msvcrt/dir.c | 19 +++++++++++++++---- dlls/msvcrt/tests/dir.c | 23 +++++++++++++++++++++++ 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/dlls/msvcrt/dir.c b/dlls/msvcrt/dir.c index 44802b3..3b9e3c7 100644 --- a/dlls/msvcrt/dir.c +++ b/dlls/msvcrt/dir.c @@ -365,14 +365,25 @@ int CDECL MSVCRT__chdrive(int newdrive) { WCHAR buffer[] = {'A', ':', 0}; + if (newdrive < 1 || newdrive > 31) + { + *MSVCRT___doserrno () = ERROR_INVALID_DRIVE; + *MSVCRT__errno () = MSVCRT_EACCES; + return -1; + } buffer[0] += newdrive - 1; - if (!SetCurrentDirectoryW( buffer )) + + _lock (_ENV_LOCK); + + if (!SetCurrentDirectoryW (buffer)) { - msvcrt_set_errno(GetLastError()); - if (newdrive <= 0) - *MSVCRT__errno() = MSVCRT_EACCES; + msvcrt_set_errno (GetLastError ()); + _unlock (_ENV_LOCK); return -1; } + + _unlock (_ENV_LOCK); + return 0; } diff --git a/dlls/msvcrt/tests/dir.c b/dlls/msvcrt/tests/dir.c index 6cc81d1..e0e4122 100644 --- a/dlls/msvcrt/tests/dir.c +++ b/dlls/msvcrt/tests/dir.c @@ -40,6 +40,7 @@ static int (__cdecl *p_wmkdir)(const wchar_t *); static int (__cdecl *p_rmdir)(const char *); static int (__cdecl *p_wrmdir)(const wchar_t *); static int (__cdecl *p_getdrive)(void); +static int (__cdecl *p_chdrive)(int); static void init(void) { @@ -55,6 +56,8 @@ static void init(void) p_rmdir = (void *)GetProcAddress(hmod, "_rmdir"); p_wrmdir = (void *)GetProcAddress(hmod, "_wrmdir"); p_getdrive = (void *)GetProcAddress(hmod, "_getdrive"); + + p_chdrive = (void*)GetProcAddress(hmod,"_chdrive"); } typedef struct @@ -555,6 +558,25 @@ static void test_chmkrmdirA (void) } } +static void test_chdrive(void) +{ + if (!p_getdrive || !p_chdrive) + { + win_skip("_getdrive/_chdrive not present.\n"); + } + else + { + int drive = _getdrive(); + + errno = 0; + ok((p_chdrive(0) == -1 && errno == EACCES + && _doserrno == ERROR_INVALID_DRIVE), "Check that drive-letter < 'A' is invalid (%u)", EACCES); + ok((p_chdrive(32) == -1 && errno == EACCES + && _doserrno == ERROR_INVALID_DRIVE), "Check that drive-letter > '_' is invalid (%u), EACCES); + ok((p_chdrive(drive) == 0, "Reset current drive %c", ('A' + drive - 1)); + } +} + START_TEST(dir) { init(); @@ -563,4 +585,5 @@ START_TEST(dir) test_makepath(); test_makepath_s(); test_chmkrmdirA(); + test_chdrive(); } -- 1.7.9