--- old/src/java.base/windows/native/libjli/java_md.c 2020-03-11 09:29:26.480439400 +0900 +++ new/src/java.base/windows/native/libjli/java_md.c 2020-03-11 09:29:26.197060200 +0900 @@ -1,5 +1,5 @@ /* - * Copyright (c) 1997, 2019, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1997, 2020, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -496,6 +496,38 @@ return rc; } +static errno_t convert_to_unicode(const char* path, const wchar_t* prefix, wchar_t** wpath) { + int unicode_path_len; + size_t prefix_len, wpath_len; + + /* + * Get required buffer size to convert to Unicode. + * The return value includes the terminating null character. + */ + unicode_path_len = MultiByteToWideChar(CP_THREAD_ACP, MB_ERR_INVALID_CHARS, + path, -1, NULL, 0); + if (unicode_path_len == 0) { + return EINVAL; + } + + prefix_len = wcslen(prefix); + wpath_len = prefix_len + unicode_path_len; + *wpath = (wchar_t*)JLI_MemAlloc(wpath_len * sizeof(wchar_t)); + if (*wpath == NULL) { + return ENOMEM; + } + + wcsncpy(*wpath, prefix, prefix_len); + if (MultiByteToWideChar(CP_THREAD_ACP, MB_ERR_INVALID_CHARS, + path, -1, &((*wpath)[prefix_len]), (int)wpath_len) == 0) { + JLI_MemFree(*wpath); + *wpath = NULL; + return EINVAL; + } + + return ERROR_SUCCESS; +} + /* taken from hotspot and slightly adjusted for jli lib; * creates a UNC/ELP path from input 'path' * the return buffer is allocated in C heap and needs to be freed using @@ -508,30 +540,13 @@ if (path[0] == '\\' && path[1] == '\\') { if (path[2] == '?' && path[3] == '\\') { /* if it already has a \\?\ don't do the prefix */ - wpath = (wchar_t*) JLI_MemAlloc(path_len * sizeof(wchar_t)); - if (wpath != NULL) { - *err = mbstowcs_s(&converted_chars, wpath, path_len, path, path_len); - } else { - *err = ENOMEM; - } + *err = convert_to_unicode(path, L"", &wpath); } else { /* only UNC pathname includes double slashes here */ - wpath = (wchar_t*) JLI_MemAlloc((path_len + 7) * sizeof(wchar_t)); - if (wpath != NULL) { - wcscpy(wpath, L"\\\\?\\UNC\0"); - *err = mbstowcs_s(&converted_chars, &wpath[7], path_len, path, path_len); - } else { - *err = ENOMEM; - } + *err = convert_to_unicode(path, L"\\\\?\\UNC", &wpath); } } else { - wpath = (wchar_t*) JLI_MemAlloc((path_len + 4) * sizeof(wchar_t)); - if (wpath != NULL) { - wcscpy(wpath, L"\\\\?\\\0"); - *err = mbstowcs_s(&converted_chars, &wpath[4], path_len, path, path_len); - } else { - *err = ENOMEM; - } + *err = convert_to_unicode(path, L"\\\\?\\", &wpath); } return wpath; }