changeset 5634:5d94285533ae

6512101: Incorrect encoding in NetworkInterface.getDisplayName() Reviewed-by: chegar, dsamersoff
author dingxmin
date Wed, 16 Jan 2013 15:24:52 +0800
parents c9fb7c0e371f
children 56d7b34dc9af
files src/windows/native/java/net/NetworkInterface.c
diffstat 1 files changed, 34 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/src/windows/native/java/net/NetworkInterface.c	Wed Jan 16 04:34:29 2013 +0400
+++ b/src/windows/native/java/net/NetworkInterface.c	Wed Jan 16 15:24:52 2013 +0800
@@ -178,7 +178,7 @@
     int count;
     netif *netifP;
     DWORD i;
-    int lo=0, eth=0, tr=0, fddi=0, ppp=0, sl=0, net=0;
+    int lo=0, eth=0, tr=0, fddi=0, ppp=0, sl=0, net=0, wlen=0;
 
     /*
      * Ask the IP Helper library to enumerate the adapters
@@ -256,8 +256,17 @@
          */
         curr = (netif *)calloc(1, sizeof(netif));
         if (curr != NULL) {
+            wlen = MultiByteToWideChar(CP_OEMCP, 0, ifrowP->bDescr,
+                       ifrowP->dwDescrLen, NULL, 0);
+            if(wlen == 0) {
+                // MultiByteToWideChar should not fail
+                // But in rare case it fails, we allow 'char' to be displayed
+                curr->displayName = (char *)malloc(ifrowP->dwDescrLen + 1);
+            } else {
+                curr->displayName = (wchar_t *)malloc(wlen*(sizeof(wchar_t))+1);
+            }
+
             curr->name = (char *)malloc(strlen(dev_name) + 1);
-            curr->displayName = (char *)malloc(ifrowP->dwDescrLen + 1);
 
             if (curr->name == NULL || curr->displayName == NULL) {
                 if (curr->name) free(curr->name);
@@ -278,8 +287,29 @@
          * 32-bit numbers as index values.
          */
         strcpy(curr->name, dev_name);
-        strncpy(curr->displayName, ifrowP->bDescr, ifrowP->dwDescrLen);
-        curr->displayName[ifrowP->dwDescrLen] = '\0';
+        if (wlen == 0) {
+            // display char type in case of MultiByteToWideChar failure
+            strncpy(curr->displayName, ifrowP->bDescr, ifrowP->dwDescrLen);
+            curr->displayName[ifrowP->dwDescrLen] = '\0';
+        } else {
+            // call MultiByteToWideChar again to fill curr->displayName
+            // it should not fail, because we have called it once before
+            if (MultiByteToWideChar(CP_OEMCP, 0, ifrowP->bDescr,
+                   ifrowP->dwDescrLen, curr->displayName, wlen) == 0) {
+                JNU_ThrowByName(env, "java/lang/Error",
+                       "Cannot get multibyte char for interface display name");
+                free_netif(netifP);
+                free(tableP);
+                free(curr->name);
+                free(curr->displayName);
+                free(curr);
+                return -1;
+            } else {
+                curr->displayName[wlen*(sizeof(wchar_t))] = '\0';
+                curr->dNameIsUnicode = TRUE;
+            }
+        }
+
         curr->dwIndex = ifrowP->dwIndex;
         curr->ifType = ifrowP->dwType;
         curr->index = GetFriendlyIfIndex(ifrowP->dwIndex);