< prev index next >

src/java.base/windows/native/libjli/java_md.c

Print this page

        

@@ -1,7 +1,7 @@
 /*
- * 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
  * under the terms of the GNU General Public License version 2 only, as
  * published by the Free Software Foundation.  Oracle designates this

@@ -494,10 +494,41 @@
         buffer[size - 1] = '\0';
     }
     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);
+    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
  * JLI_MemFree by the caller.
  */

@@ -506,34 +537,17 @@
     size_t converted_chars = 0;
     size_t path_len = strlen(path) + 1; /* includes the terminating NULL */
     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;
 }
 
 int JLI_Open(const char* name, int flags) {
< prev index next >