diff --git a/dlls/msvcr100/msvcr100.spec b/dlls/msvcr100/msvcr100.spec index e0783880398..4288dede1c4 100644 --- a/dlls/msvcr100/msvcr100.spec +++ b/dlls/msvcr100/msvcr100.spec @@ -1310,8 +1310,8 @@ @ stub _wgetdcwd_nolock @ cdecl _wgetenv(wstr) msvcrt._wgetenv @ stub _wgetenv_s -@ cdecl _wmakepath(wstr wstr wstr wstr wstr) msvcrt._wmakepath -@ stub _wmakepath_s +@ cdecl _wmakepath(ptr wstr wstr wstr wstr) msvcrt._wmakepath +@ cdecl _wmakepath_s(ptr long wstr wstr wstr wstr) msvcrt._wmakepath_s @ cdecl _wmkdir(wstr) msvcrt._wmkdir @ cdecl _wmktemp(wstr) msvcrt._wmktemp @ stub _wmktemp_s diff --git a/dlls/msvcr80/msvcr80.spec b/dlls/msvcr80/msvcr80.spec index 07b590e59a7..3d6b104419f 100644 --- a/dlls/msvcr80/msvcr80.spec +++ b/dlls/msvcr80/msvcr80.spec @@ -1159,8 +1159,8 @@ @ extern _winmajor msvcrt._winmajor @ extern _winminor msvcrt._winminor @ extern _winver msvcrt._winver -@ cdecl _wmakepath(wstr wstr wstr wstr wstr) msvcrt._wmakepath -@ stub _wmakepath_s +@ cdecl _wmakepath(ptr wstr wstr wstr wstr) msvcrt._wmakepath +@ cdecl _wmakepath_s(ptr long wstr wstr wstr wstr) msvcrt._wmakepath_s @ cdecl _wmkdir(wstr) msvcrt._wmkdir @ cdecl _wmktemp(wstr) msvcrt._wmktemp @ stub _wmktemp_s diff --git a/dlls/msvcr90/msvcr90.spec b/dlls/msvcr90/msvcr90.spec index 63077474259..3b04f961eb6 100644 --- a/dlls/msvcr90/msvcr90.spec +++ b/dlls/msvcr90/msvcr90.spec @@ -1143,8 +1143,8 @@ @ stub _wgetdcwd_nolock @ cdecl _wgetenv(wstr) msvcrt._wgetenv @ stub _wgetenv_s -@ cdecl _wmakepath(wstr wstr wstr wstr wstr) msvcrt._wmakepath -@ stub _wmakepath_s +@ cdecl _wmakepath(ptr wstr wstr wstr wstr) msvcrt._wmakepath +@ cdecl _wmakepath_s(ptr long wstr wstr wstr wstr) msvcrt._wmakepath_s @ cdecl _wmkdir(wstr) msvcrt._wmkdir @ cdecl _wmktemp(wstr) msvcrt._wmktemp @ stub _wmktemp_s diff --git a/dlls/msvcrt/dir.c b/dlls/msvcrt/dir.c index e3862f3ad20..c5b663814a7 100644 --- a/dlls/msvcrt/dir.c +++ b/dlls/msvcrt/dir.c @@ -1181,6 +1181,110 @@ range: return MSVCRT_ERANGE; } +/********************************************************************* + * _wmakepath_s (MSVCRT.@) + * + * Safe version of _wmakepath. + */ +int CDECL _wmakepath_s(MSVCRT_wchar_t *path, MSVCRT_size_t size, const MSVCRT_wchar_t *drive, + const MSVCRT_wchar_t *directory, const MSVCRT_wchar_t *filename, + const MSVCRT_wchar_t *extension) +{ + MSVCRT_wchar_t *p = path; + + if (!path || !size) + { + *MSVCRT__errno() = MSVCRT_EINVAL; + return MSVCRT_EINVAL; + } + + if (drive && drive[0]) + { + if (size <= 2) + goto range; + + *p++ = drive[0]; + *p++ = ':'; + size -= 2; + } + + if (directory && directory[0]) + { + unsigned int len = strlenW(directory); + unsigned int needs_separator = directory[len - 1] != '/' && directory[len - 1] != '\\'; + unsigned int copylen = min(size - 1, len); + + if (size < 2) + goto range; + + memmove(p, directory, copylen * sizeof(MSVCRT_wchar_t)); + + if (size <= len) + goto range; + + p += copylen; + size -= copylen; + + if (needs_separator) + { + if (size < 2) + goto range; + + *p++ = '\\'; + size -= 1; + } + } + + if (filename && filename[0]) + { + unsigned int len = strlenW(filename); + unsigned int copylen = min(size - 1, len); + + if (size < 2) + goto range; + + memmove(p, filename, copylen * sizeof(MSVCRT_wchar_t)); + + if (size <= len) + goto range; + + p += len; + size -= len; + } + + if (extension && extension[0]) + { + unsigned int len = strlenW(extension); + unsigned int needs_period = extension[0] != '.'; + unsigned int copylen; + + if (size < 2) + goto range; + + if (needs_period) + { + *p++ = '.'; + size -= 1; + } + + copylen = min(size - 1, len); + memcpy(p, extension, copylen * sizeof(MSVCRT_wchar_t)); + + if (size <= len) + goto range; + + p += copylen; + } + + *p = '\0'; + return 0; + +range: + path[0] = '\0'; + *MSVCRT__errno() = MSVCRT_ERANGE; + return MSVCRT_ERANGE; +} + /********************************************************************* * _searchenv (MSVCRT.@) * diff --git a/dlls/msvcrt/msvcrt.spec b/dlls/msvcrt/msvcrt.spec index 2a760af5751..1bd3abd5e7f 100644 --- a/dlls/msvcrt/msvcrt.spec +++ b/dlls/msvcrt/msvcrt.spec @@ -1089,8 +1089,8 @@ @ extern _winminor MSVCRT__winminor # stub _winput_s @ extern _winver MSVCRT__winver -@ cdecl _wmakepath(wstr wstr wstr wstr wstr) -# stub _wmakepath_s +@ cdecl _wmakepath(ptr wstr wstr wstr wstr) +@ cdecl _wmakepath_s(ptr long wstr wstr wstr wstr) @ cdecl _wmkdir(wstr) @ cdecl _wmktemp(wstr) # stub _wmktemp_s diff --git a/dlls/msvcrt/tests/dir.c b/dlls/msvcrt/tests/dir.c index 98776c3753a..83c533b5e52 100644 --- a/dlls/msvcrt/tests/dir.c +++ b/dlls/msvcrt/tests/dir.c @@ -32,12 +32,14 @@ #include static int (__cdecl *p_makepath_s)(char *, size_t, const char *, const char *, const char *, const char *); +static int (__cdecl *p_wmakepath_s)(wchar_t *, size_t, const wchar_t *,const wchar_t *, const wchar_t *, const wchar_t *); static void init(void) { HMODULE hmod = GetModuleHandleA("msvcrt.dll"); p_makepath_s = (void *)GetProcAddress(hmod, "_makepath_s"); + p_wmakepath_s = (void *)GetProcAddress(hmod, "_wmakepath_s"); } typedef struct @@ -130,6 +132,36 @@ static void test_makepath(void) } } +static const WCHAR expected0[] = {'\0','X','X','X','X','X','X','X','X','X','X','X','X'}; +static const WCHAR expected1[] = {'\0','X','X','X','X','X','X','X','X','X','X','X','X'}; +static const WCHAR expected2[] = {'\0',':','X','X','X','X','X','X','X','X','X','X','X'}; +static const WCHAR expected3[] = {'\0',':','d','X','X','X','X','X','X','X','X','X','X'}; +static const WCHAR expected4[] = {'\0',':','d','\\','X','X','X','X','X','X','X','X','X'}; +static const WCHAR expected5[] = {'\0',':','d','\\','f','X','X','X','X','X','X','X','X'}; +static const WCHAR expected6[] = {'\0',':','d','\\','f','i','X','X','X','X','X','X','X'}; +static const WCHAR expected7[] = {'\0',':','d','\\','f','i','l','X','X','X','X','X','X'}; +static const WCHAR expected8[] = {'\0',':','d','\\','f','i','l','e','X','X','X','X','X'}; +static const WCHAR expected9[] = {'\0',':','d','\\','f','i','l','e','.','X','X','X','X'}; +static const WCHAR expected10[] = {'\0',':','d','\\','f','i','l','e','.','e','X','X','X'}; +static const WCHAR expected11[] = {'\0',':','d','\\','f','i','l','e','.','e','x','X','X'}; + +static const WCHAR expected12[] = {'\0',':','X','X','X','X','X','X','X','X'}; +static const WCHAR expected13[] = {'\0',':','d','X','X','X','X','X','X','X'}; +static const WCHAR expected14[] = {'\0',':','d','i','X','X','X','X','X','X'}; +static const WCHAR expected15[] = {'\0',':','d','i','r','X','X','X','X','X'}; +static const WCHAR expected16[] = {'\0',':','d','i','r','\\','X','X','X','X'}; + +static const WCHAR expected17[] = {'\0','o','o'}; +static const WCHAR expected18[] = {'\0','o','o','\0','X'}; +static const WCHAR expected19[] = {'\0','o','o','\0'}; +static const WCHAR expected20[] = {'\0','o','o','\0','X','X','X','X','X'}; +static const WCHAR expected21[] = {'\0','o','o','\\','f','i','l','X','X'}; +static const WCHAR expected22[] = {'\0','o','o','\0','X','X','X','X','X','X','X','X','X'}; +static const WCHAR expected23[] = {'\0','o','o','\\','f','i','l','X','X','X','X','X','X'}; +static const WCHAR expected24[] = {'\0','o','o','\\','f','i','l','e','.','e','x','X','X'}; +static const WCHAR expected25[] = {'\0','o','o','\0','X','X','X','X'}; +static const WCHAR expected26[] = {'\0','o','o','.','e','x','X','X'}; + typedef struct { const char* buffer; @@ -139,52 +171,58 @@ typedef struct const char* file; const char* ext; const char* expected; + const WCHAR *expected_unicode; size_t expected_length; } makepath_s_case; static const makepath_s_case makepath_s_cases[] = { /* Behavior with directory parameter containing backslash. */ - {NULL, 1, "c:", "d\\", "file", "ext", "\0XXXXXXXXXXXX", 13}, - {NULL, 2, "c:", "d\\", "file", "ext", "\0XXXXXXXXXXXX", 13}, - {NULL, 3, "c:", "d\\", "file", "ext", "\0:XXXXXXXXXXX", 13}, - {NULL, 4, "c:", "d\\", "file", "ext", "\0:dXXXXXXXXXX", 13}, - {NULL, 5, "c:", "d\\", "file", "ext", "\0:d\\XXXXXXXXX", 13}, - {NULL, 6, "c:", "d\\", "file", "ext", "\0:d\\fXXXXXXXX", 13}, - {NULL, 7, "c:", "d\\", "file", "ext", "\0:d\\fiXXXXXXX", 13}, - {NULL, 8, "c:", "d\\", "file", "ext", "\0:d\\filXXXXXX", 13}, - {NULL, 9, "c:", "d\\", "file", "ext", "\0:d\\fileXXXXX", 13}, - {NULL, 10, "c:", "d\\", "file", "ext", "\0:d\\file.XXXX", 13}, - {NULL, 11, "c:", "d\\", "file", "ext", "\0:d\\file.eXXX", 13}, - {NULL, 12, "c:", "d\\", "file", "ext", "\0:d\\file.exXX", 13}, + {NULL, 1, "c:", "d\\", "file", "ext", "\0XXXXXXXXXXXX", expected0, 13}, + {NULL, 2, "c:", "d\\", "file", "ext", "\0XXXXXXXXXXXX", expected1, 13}, + {NULL, 3, "c:", "d\\", "file", "ext", "\0:XXXXXXXXXXX", expected2, 13}, + {NULL, 4, "c:", "d\\", "file", "ext", "\0:dXXXXXXXXXX", expected3, 13}, + {NULL, 5, "c:", "d\\", "file", "ext", "\0:d\\XXXXXXXXX", expected4, 13}, + {NULL, 6, "c:", "d\\", "file", "ext", "\0:d\\fXXXXXXXX", expected5, 13}, + {NULL, 7, "c:", "d\\", "file", "ext", "\0:d\\fiXXXXXXX", expected6, 13}, + {NULL, 8, "c:", "d\\", "file", "ext", "\0:d\\filXXXXXX", expected7, 13}, + {NULL, 9, "c:", "d\\", "file", "ext", "\0:d\\fileXXXXX", expected8, 13}, + {NULL, 10, "c:", "d\\", "file", "ext", "\0:d\\file.XXXX", expected9, 13}, + {NULL, 11, "c:", "d\\", "file", "ext", "\0:d\\file.eXXX", expected10, 13}, + {NULL, 12, "c:", "d\\", "file", "ext", "\0:d\\file.exXX", expected11, 13}, /* Behavior with directory parameter lacking backslash. */ - {NULL, 3, "c:", "dir", "f", "ext", "\0:XXXXXXXX", 10}, - {NULL, 4, "c:", "dir", "f", "ext", "\0:dXXXXXXX", 10}, - {NULL, 5, "c:", "dir", "f", "ext", "\0:diXXXXXX", 10}, - {NULL, 6, "c:", "dir", "f", "ext", "\0:dirXXXXX", 10}, - {NULL, 7, "c:", "dir", "f", "ext", "\0:dir\\XXXX", 10}, + {NULL, 3, "c:", "dir", "f", "ext", "\0:XXXXXXXX", expected12, 10}, + {NULL, 4, "c:", "dir", "f", "ext", "\0:dXXXXXXX", expected13, 10}, + {NULL, 5, "c:", "dir", "f", "ext", "\0:diXXXXXX", expected14, 10}, + {NULL, 6, "c:", "dir", "f", "ext", "\0:dirXXXXX", expected15, 10}, + {NULL, 7, "c:", "dir", "f", "ext", "\0:dir\\XXXX", expected16, 10}, /* Behavior with overlapped buffer. */ - {"foo", 2, USE_BUFF, NULL, NULL, NULL, "\0oo", 3}, - {"foo", 4, NULL, USE_BUFF, NULL, NULL, "\0oo\0X", 5}, - {"foo", 3, NULL, NULL, USE_BUFF, NULL, "\0oo\0", 4}, - {"foo", 4, NULL, USE_BUFF, "file", NULL, "\0oo\0XXXXX", 9}, - {"foo", 8, NULL, USE_BUFF, "file", NULL, "\0oo\\filXX", 9}, - {"foo", 4, NULL, USE_BUFF, "file", "ext", "\0oo\0XXXXXXXXX", 13}, - {"foo", 8, NULL, USE_BUFF, "file", "ext", "\0oo\\filXXXXXX", 13}, - {"foo", 12, NULL, USE_BUFF, "file", "ext", "\0oo\\file.exXX", 13}, - {"foo", 4, NULL, NULL, USE_BUFF, "ext", "\0oo\0XXXX", 8}, - {"foo", 7, NULL, NULL, USE_BUFF, "ext", "\0oo.exXX", 8}, + {"foo", 2, USE_BUFF, NULL, NULL, NULL, "\0oo", expected17, 3}, + {"foo", 4, NULL, USE_BUFF, NULL, NULL, "\0oo\0X", expected18, 5}, + {"foo", 3, NULL, NULL, USE_BUFF, NULL, "\0oo\0", expected19, 4}, + {"foo", 4, NULL, USE_BUFF, "file", NULL, "\0oo\0XXXXX", expected20, 9}, + {"foo", 8, NULL, USE_BUFF, "file", NULL, "\0oo\\filXX", expected21, 9}, + {"foo", 4, NULL, USE_BUFF, "file", "ext", "\0oo\0XXXXXXXXX", expected22, 13}, + {"foo", 8, NULL, USE_BUFF, "file", "ext", "\0oo\\filXXXXXX", expected23, 13}, + {"foo", 12, NULL, USE_BUFF, "file", "ext", "\0oo\\file.exXX", expected24, 13}, + {"foo", 4, NULL, NULL, USE_BUFF, "ext", "\0oo\0XXXX", expected25, 8}, + {"foo", 7, NULL, NULL, USE_BUFF, "ext", "\0oo.exXX", expected26, 8}, }; static void test_makepath_s(void) { + WCHAR driveW[MAX_PATH]; + WCHAR dirW[MAX_PATH]; + WCHAR fileW[MAX_PATH]; + WCHAR extW[MAX_PATH]; + WCHAR bufferW[MAX_PATH]; char buffer[MAX_PATH]; int ret; - unsigned int i; + unsigned int i, n; - if (!p_makepath_s) + if (!p_makepath_s || !p_wmakepath_s) { - win_skip("_makepath_s is not available\n"); + win_skip("Safe makepath functions are not available\n"); return; } @@ -198,6 +236,16 @@ static void test_makepath_s(void) ok(ret == EINVAL, "Expected _makepath_s to return EINVAL, got %d\n", ret); ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno); + errno = EBADF; + ret = p_wmakepath_s(NULL, 0, NULL, NULL, NULL, NULL); + ok(ret == EINVAL, "Expected _wmakepath_s to return EINVAL, got %d\n", ret); + ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno); + + errno = EBADF; + ret = p_wmakepath_s(bufferW, 0, NULL, NULL, NULL, NULL); + ok(ret == EINVAL, "Expected _wmakepath_s to return EINVAL, got %d\n", ret); + ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno); + /* Test with the normal _makepath cases. */ for (i = 0; i < sizeof(makepath_cases)/sizeof(makepath_cases[0]); i++) { @@ -217,6 +265,28 @@ static void test_makepath_s(void) buffer[MAX_PATH - 1] = '\0'; ok(!strcmp(p->expected, buffer), "got '%s' for case %d\n", buffer, i); + + /* Unicode */ + if (p->drive != USE_BUFF) MultiByteToWideChar(CP_ACP, 0, p->drive, -1, driveW, MAX_PATH); + if (p->dir != USE_BUFF) MultiByteToWideChar(CP_ACP, 0, p->dir, -1, dirW, MAX_PATH); + if (p->file != USE_BUFF) MultiByteToWideChar(CP_ACP, 0, p->file, -1, fileW, MAX_PATH); + if (p->ext != USE_BUFF) MultiByteToWideChar(CP_ACP, 0, p->ext, -1, extW, MAX_PATH); + + memset(buffer, 0, MAX_PATH); + for (n = 0; n < MAX_PATH; ++n) + bufferW[n] = 'X'; + if (p->buffer) MultiByteToWideChar( CP_ACP, 0, p->buffer, -1, bufferW, MAX_PATH); + + ret = p_wmakepath_s(bufferW, MAX_PATH, + p->drive == USE_BUFF ? bufferW : p->drive ? driveW : NULL, + p->dir == USE_BUFF ? bufferW : p->dir ? dirW : NULL, + p->file == USE_BUFF? bufferW : p->file ? fileW : NULL, + p->ext == USE_BUFF ? bufferW : p->ext ? extW : NULL); + ok(ret == 0, "[%d] Expected _wmakepath_s to return 0, got %d\n", i, ret); + + bufferW[MAX_PATH - 1] = '\0'; + WideCharToMultiByte(CP_ACP, 0, bufferW, -1, buffer, MAX_PATH, NULL, NULL); + ok(!strcmp(p->expected, buffer), "got '%s' for unicode case %d\n", buffer, i); } /* Try insufficient length cases. */ @@ -238,6 +308,28 @@ static void test_makepath_s(void) ok(ret == ERANGE, "[%d] Expected _makepath_s to return ERANGE, got %d\n", i, ret); ok(errno == ERANGE, "[%d] Expected errno to be ERANGE, got %d\n", i, errno); ok(!memcmp(p->expected, buffer, p->expected_length), "unexpected output for case %d\n", i); + + /* Unicode */ + if (p->drive != USE_BUFF) MultiByteToWideChar(CP_ACP, 0, p->drive, -1, driveW, MAX_PATH); + if (p->dir != USE_BUFF) MultiByteToWideChar(CP_ACP, 0, p->dir, -1, dirW, MAX_PATH); + if (p->file != USE_BUFF) MultiByteToWideChar(CP_ACP, 0, p->file, -1, fileW, MAX_PATH); + if (p->ext != USE_BUFF) MultiByteToWideChar(CP_ACP, 0, p->ext, -1, extW, MAX_PATH); + + memset(buffer, 0, MAX_PATH); + for (n = 0; n < MAX_PATH; ++n) + bufferW[n] = 'X'; + if (p->buffer) MultiByteToWideChar( CP_ACP, 0, p->buffer, -1, bufferW, MAX_PATH); + + errno = EBADF; + ret = p_wmakepath_s(bufferW, p->length, + p->drive == USE_BUFF ? bufferW : p->drive ? driveW : NULL, + p->dir == USE_BUFF ? bufferW : p->dir ? dirW : NULL, + p->file == USE_BUFF? bufferW : p->file ? fileW : NULL, + p->ext == USE_BUFF ? bufferW : p->ext ? extW : NULL); + ok(ret == ERANGE, "[%d] Expected _wmakepath_s to return ERANGE, got %d\n", i, ret); + ok(errno == ERANGE, "[%d] Expected errno to be ERANGE, got %d\n", i, errno); + + ok(!memcmp(p->expected_unicode, bufferW, p->expected_length * sizeof(WCHAR)), "unexpected output for case %d\n", i); } } diff --git a/include/msvcrt/stdlib.h b/include/msvcrt/stdlib.h index e7130b9471e..5e95d35fd33 100644 --- a/include/msvcrt/stdlib.h +++ b/include/msvcrt/stdlib.h @@ -205,6 +205,7 @@ wchar_t* __cdecl _ultow(__msvcrt_ulong,wchar_t*,int); wchar_t* __cdecl _wfullpath(wchar_t*,const wchar_t*,size_t); wchar_t* __cdecl _wgetenv(const wchar_t*); void __cdecl _wmakepath(wchar_t*,const wchar_t*,const wchar_t*,const wchar_t*,const wchar_t*); +int __cdecl _wmakepath_s(wchar_t*,size_t,const wchar_t*,const wchar_t*,const wchar_t*,const wchar_t*); void __cdecl _wperror(const wchar_t*); int __cdecl _wputenv(const wchar_t*); void __cdecl _wsearchenv(const wchar_t*,const wchar_t*,wchar_t*);