提交 3ad70dbe 编写于 作者: M msheppar

8157749: Improve handling of DNS error replies

Reviewed-by: chegar, rriggs, coffeys
上级 8f7d719c
......@@ -56,6 +56,7 @@ SUNWprivate_1.1 {
JNU_ThrowArrayIndexOutOfBoundsException;
JNU_ThrowByName;
JNU_ThrowByNameWithLastError;
JNU_ThrowByNameWithMessageAndLastError;
JNU_ThrowClassNotFoundException;
JNU_ThrowIllegalAccessError;
JNU_ThrowIllegalAccessException;
......
......@@ -148,6 +148,61 @@ JNU_ThrowInstantiationException(JNIEnv *env, const char *msg)
}
/*
* Throw an exception by name, using a given message and the string
* returned by getLastErrorString to construct the detail string.
*/
JNIEXPORT void JNICALL
JNU_ThrowByNameWithMessageAndLastError
(JNIEnv *env, const char *name, const char *message)
{
char buf[256];
size_t n = getLastErrorString(buf, sizeof(buf));
size_t messagelen = message == NULL ? 0 : strlen(message);
if (n > 0) {
jstring s = JNU_NewStringPlatform(env, buf);
if (s != NULL) {
jobject x = NULL;
if (messagelen) {
jstring s2 = NULL;
size_t messageextlen = messagelen + 4;
char *str1 = (char *)malloc((messageextlen) * sizeof(char));
if (str1 == 0) {
JNU_ThrowOutOfMemoryError(env, 0);
return;
}
jio_snprintf(str1, messageextlen, " (%s)", message);
s2 = (*env)->NewStringUTF(env, str1);
free(str1);
if (s2 != NULL) {
jstring s3 = JNU_CallMethodByName(
env, NULL, s, "concat",
"(Ljava/lang/String;)Ljava/lang/String;",
s2).l;
(*env)->DeleteLocalRef(env, s2);
if (s3 != NULL) {
(*env)->DeleteLocalRef(env, s);
s = s3;
}
}
}
x = JNU_NewObjectByName(env, name, "(Ljava/lang/String;)V", s);
if (x != NULL) {
(*env)->Throw(env, x);
}
}
}
if (!(*env)->ExceptionOccurred(env)) {
if (messagelen) {
JNU_ThrowByName(env, name, message);
} else {
JNU_ThrowByName(env, name, "no further information");
}
}
}
/* Throw an exception by name, using the string returned by
* JVM_LastErrorString for the detail string. If the last-error
* string is NULL, use the given default detail string.
......
......@@ -105,6 +105,13 @@ JNIEXPORT void JNICALL
JNU_ThrowByNameWithLastError(JNIEnv *env, const char *name,
const char *defaultMessage);
/* Throw an exception by name, using a given message and the string
* returned by getLastErrorString to construct the detail string.
*/
JNIEXPORT void JNICALL
JNU_ThrowByNameWithMessageAndLastError
(JNIEnv *env, const char *name, const char *message);
/* Throw an IOException, using the last-error string for the detail
* string. If the last-error string is NULL, use the given default
* detail string.
......
......@@ -106,6 +106,8 @@ void setDefaultScopeID(JNIEnv *env, struct sockaddr *him)
int getDefaultScopeID(JNIEnv *env) {
static jclass ni_class = NULL;
static jfieldID ni_defaultIndexID;
int defaultIndex = 0;
if (ni_class == NULL) {
jclass c = (*env)->FindClass(env, "java/net/NetworkInterface");
CHECK_NULL_RETURN(c, 0);
......@@ -116,7 +118,6 @@ int getDefaultScopeID(JNIEnv *env) {
CHECK_NULL_RETURN(ni_defaultIndexID, 0);
ni_class = c;
}
int defaultIndex = 0;
defaultIndex = (*env)->GetStaticIntField(env, ni_class,
ni_defaultIndexID);
return defaultIndex;
......@@ -257,9 +258,7 @@ int cmpScopeID (unsigned int scope, struct sockaddr *him) {
void
NET_ThrowByNameWithLastError(JNIEnv *env, const char *name,
const char *defaultDetail) {
char errmsg[255];
sprintf(errmsg, "errno: %d, error: %s\n", errno, defaultDetail);
JNU_ThrowByNameWithLastError(env, name, errmsg);
JNU_ThrowByNameWithMessageAndLastError(env, name, defaultDetail);
}
void
......
......@@ -218,9 +218,7 @@ NET_ThrowSocketException(JNIEnv *env, char* msg)
void
NET_ThrowByNameWithLastError(JNIEnv *env, const char *name,
const char *defaultDetail) {
char errmsg[255];
sprintf(errmsg, "errno: %d, error: %s\n", WSAGetLastError(), defaultDetail);
JNU_ThrowByNameWithLastError(env, name, errmsg);
JNU_ThrowByNameWithMessageAndLastError(env, name, defaultDetail);
}
jfieldID
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册