8303814: getLastErrorString should avoid charset conversions

Reviewed-by: naoto, cjplummer, rriggs
This commit is contained in:
Daniel Jeliński 2023-03-14 17:18:56 +00:00
parent 830fd41346
commit baf11e734f
8 changed files with 106 additions and 129 deletions

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2004, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2004, 2023, 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
@ -63,34 +63,34 @@ void buildJniFunctionName(const char *sym, const char *cname,
return;
}
JNIEXPORT size_t JNICALL
getLastErrorString(char *buf, size_t len) {
jstring
getLastErrorString(JNIEnv *env) {
#define BUFSIZE 256
DWORD errval;
WCHAR buf[BUFSIZE];
if ((errval = GetLastError()) != 0) {
// DOS error
size_t n = (size_t)FormatMessage(
jsize n = FormatMessageW(
FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
errval,
0,
buf,
(DWORD)len,
BUFSIZE,
NULL);
if (n > 3) {
// Drop final '.', CR, LF
if (buf[n - 1] == '\n') n--;
if (buf[n - 1] == '\r') n--;
if (buf[n - 1] == '.') n--;
buf[n] = '\0';
if (buf[n - 1] == L'\n') n--;
if (buf[n - 1] == L'\r') n--;
if (buf[n - 1] == L'.') n--;
buf[n] = L'\0';
}
return n;
jstring s = (*env)->NewString(env, buf, n);
return s;
}
// C runtime error that has no corresponding DOS error code
if (errno == 0 || len < 1) return 0;
return strerror_s(buf, len, errno);
return NULL;
}
JNIEXPORT int JNICALL

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2000, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2000, 2023, 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
@ -49,7 +49,7 @@ Java_sun_nio_ch_FileDispatcherImpl_read0(JNIEnv *env, jclass clazz, jobject fdo,
HANDLE h = (HANDLE)(handleval(env, fdo));
if (h == INVALID_HANDLE_VALUE) {
JNU_ThrowIOExceptionWithLastError(env, "Invalid handle");
JNU_ThrowIOException(env, "Invalid handle");
return IOS_THROWN;
}
result = ReadFile(h, /* File handle to read */
@ -85,7 +85,7 @@ Java_sun_nio_ch_FileDispatcherImpl_readv0(JNIEnv *env, jclass clazz, jobject fdo
HANDLE h = (HANDLE)(handleval(env, fdo));
if (h == INVALID_HANDLE_VALUE) {
JNU_ThrowIOExceptionWithLastError(env, "Invalid handle");
JNU_ThrowIOException(env, "Invalid handle");
return IOS_THROWN;
}
@ -131,7 +131,7 @@ Java_sun_nio_ch_FileDispatcherImpl_pread0(JNIEnv *env, jclass clazz, jobject fdo
OVERLAPPED ov;
if (h == INVALID_HANDLE_VALUE) {
JNU_ThrowIOExceptionWithLastError(env, "Invalid handle");
JNU_ThrowIOException(env, "Invalid handle");
return IOS_THROWN;
}
@ -199,9 +199,12 @@ Java_sun_nio_ch_FileDispatcherImpl_write0(JNIEnv *env, jclass clazz, jobject fdo
len, /* number of bytes to write */
&written, /* receives number of bytes written */
lpOv); /* overlapped struct */
} else {
JNU_ThrowIOException(env, "Invalid handle");
return IOS_THROWN;
}
if ((h == INVALID_HANDLE_VALUE) || (result == 0)) {
if (result == 0) {
JNU_ThrowIOExceptionWithLastError(env, "Write failed");
return IOS_THROWN;
}
@ -248,9 +251,12 @@ Java_sun_nio_ch_FileDispatcherImpl_writev0(JNIEnv *env, jclass clazz, jobject fd
break;
}
}
} else {
JNU_ThrowIOException(env, "Invalid handle");
return IOS_THROWN;
}
if ((h == INVALID_HANDLE_VALUE) || (result == 0)) {
if (result == 0) {
JNU_ThrowIOExceptionWithLastError(env, "Write failed");
return IOS_THROWN;
}
@ -268,6 +274,10 @@ Java_sun_nio_ch_FileDispatcherImpl_pwrite0(JNIEnv *env, jclass clazz, jobject fd
LARGE_INTEGER currPos;
OVERLAPPED ov;
if (h == INVALID_HANDLE_VALUE) {
JNU_ThrowIOException(env, "Invalid handle");
return IOS_THROWN;
}
currPos.QuadPart = 0;
result = SetFilePointerEx(h, currPos, &currPos, FILE_CURRENT);
if (result == 0) {
@ -285,7 +295,7 @@ Java_sun_nio_ch_FileDispatcherImpl_pwrite0(JNIEnv *env, jclass clazz, jobject fd
&written, /* receives number of bytes written */
&ov); /* position to write at */
if ((h == INVALID_HANDLE_VALUE) || (result == 0)) {
if (result == 0) {
JNU_ThrowIOExceptionWithLastError(env, "Write failed");
return IOS_THROWN;
}
@ -341,7 +351,7 @@ Java_sun_nio_ch_FileDispatcherImpl_force0(JNIEnv *env, jobject this,
}
}
} else {
JNU_ThrowIOExceptionWithLastError(env, "Force failed");
JNU_ThrowIOException(env, "Invalid handle");
return IOS_THROWN;
}
return 0;