8223813: (aio) Iocp.getErrorMessage should drop trailing \r\n
authorbpb
Thu, 13 Jun 2019 13:43:34 -0700
changeset 55388 d57d61aafef9
parent 55387 761b86d5563d
child 55389 5c52d053cb14
child 57403 05e411fc4b77
8223813: (aio) Iocp.getErrorMessage should drop trailing \r\n Reviewed-by: alanb, igerasim, rriggs
src/java.base/windows/native/libnet/Inet4AddressImpl.c
src/java.base/windows/native/libnio/ch/Iocp.c
src/java.base/windows/native/libnio/fs/WindowsNativeDispatcher.c
--- a/src/java.base/windows/native/libnet/Inet4AddressImpl.c	Thu Jun 13 11:50:45 2019 -0700
+++ b/src/java.base/windows/native/libnet/Inet4AddressImpl.c	Thu Jun 13 13:43:34 2019 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2000, 2019, 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
@@ -359,7 +359,8 @@
     }
 
     if (dwRetVal == 0) { // if the call failed
-        TCHAR *buf;
+        TCHAR *buf = NULL;
+        DWORD n;
         DWORD err = WSAGetLastError();
         switch (err) {
             case ERROR_NO_NETWORK:
@@ -379,9 +380,17 @@
             case IP_REQ_TIMED_OUT:
                 break;
             default:
-                FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
-                              NULL, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
-                              (LPTSTR)&buf, 0, NULL);
+                n = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
+                        FORMAT_MESSAGE_FROM_SYSTEM,
+                        NULL, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
+                        (LPTSTR)&buf, 0, NULL);
+                if (n > 3) {
+                    // Drop final '.', CR, LF
+                    if (buf[n - 1] == TEXT('\n')) n--;
+                    if (buf[n - 1] == TEXT('\r')) n--;
+                    if (buf[n - 1] == TEXT('.')) n--;
+                    buf[n] = TEXT('\0');
+                }
                 NET_ThrowNew(env, err, buf);
                 LocalFree(buf);
                 break;
--- a/src/java.base/windows/native/libnio/ch/Iocp.c	Thu Jun 13 11:50:45 2019 -0700
+++ b/src/java.base/windows/native/libnio/ch/Iocp.c	Thu Jun 13 13:43:34 2019 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2008, 2015, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2008, 2019, 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
@@ -142,6 +142,14 @@
     if (len == 0) {
         return NULL;
     } else {
+        if (len > 3) {
+            // Drop final '.', CR, LF
+            if (message[len - 1] == L'\n') len--;
+            if (message[len - 1] == L'\r') len--;
+            if (message[len - 1] == L'.') len--;
+            message[len] = L'\0';
+        }
+
         return (*env)->NewString(env, (const jchar *)message, (jsize)wcslen(message));
     }
 }
--- a/src/java.base/windows/native/libnio/fs/WindowsNativeDispatcher.c	Thu Jun 13 11:50:45 2019 -0700
+++ b/src/java.base/windows/native/libnio/fs/WindowsNativeDispatcher.c	Thu Jun 13 13:43:34 2019 -0700
@@ -177,6 +177,14 @@
     if (len == 0) {
         return NULL;
     } else {
+        if (len > 3) {
+            // Drop final '.', CR, LF
+            if (message[len - 1] == L'\n') len--;
+            if (message[len - 1] == L'\r') len--;
+            if (message[len - 1] == L'.') len--;
+            message[len] = L'\0';
+        }
+
         return (*env)->NewString(env, (const jchar *)message, (jsize)wcslen(message));
     }
 }