From 85556970cfca2c18e0fe37a51abd5eebdf371547 Mon Sep 17 00:00:00 2001 From: "akhil.arora%sun.com" Date: Thu, 28 Oct 1999 20:26:27 +0000 Subject: [PATCH] r=akhil.arora@sun.com By Igor Nekrestyanov Minor changes to handle unicode strings properly --- java/dom/jni/javaDOMEventsGlobals.cpp | 34 +++++++++++-------- .../jni/org_mozilla_dom_events_EventImpl.cpp | 5 +-- 2 files changed, 21 insertions(+), 18 deletions(-) diff --git a/java/dom/jni/javaDOMEventsGlobals.cpp b/java/dom/jni/javaDOMEventsGlobals.cpp index 101c8ece8d2..7b4ed30aa73 100644 --- a/java/dom/jni/javaDOMEventsGlobals.cpp +++ b/java/dom/jni/javaDOMEventsGlobals.cpp @@ -1835,16 +1835,15 @@ void JavaDOMEventsGlobals::Destroy(JNIEnv *env) //returns true if specified event "type" exists in the given list of types // NOTE: it is assumed that "types" list is enden with NULL -static jboolean isEventOfType(char **types, char *type) +static jboolean isEventOfType(char **types, nsString type) { int i=0; - if (type) - while (types[i]) { - if (strcmp(type, types[i]) == 0) - return JNI_TRUE; - i++; - } + while (types && types[i]) { + if (type == types[i]) + return JNI_TRUE; + i++; + } return JNI_FALSE; } @@ -1884,22 +1883,29 @@ jobject JavaDOMEventsGlobals::CreateEventSubtype(JNIEnv *env, "Event.getType at JavaDOMEventsGlobals: failed"); return NULL; } - char *type = nsType.ToNewCString(); - if (isEventOfType(mouseEventTypes, type) == JNI_TRUE) { + if (isEventOfType(mouseEventTypes, nsType) == JNI_TRUE) { clazz = mouseEventClass; - } else if (isEventOfType(keyEventTypes, type) == JNI_TRUE) { + } else if (isEventOfType(keyEventTypes, nsType) == JNI_TRUE) { clazz = keyEventClass; - } else if (isEventOfType(uiEventTypes, type) == JNI_TRUE) { + } else if (isEventOfType(uiEventTypes, nsType) == JNI_TRUE) { clazz = uiEventClass; } else { + /* Being paranoid here, make sure the (unicode) string is + NULL-terminated before passing it to PR_LOG. Otherwise, we + may cause a crash */ + + PRInt32 length = nsType.Length(); + char* buffer = new char[length+1]; + strncpy(buffer, nsType.GetBuffer(), length); + buffer[length] = 0; + PR_LOG(JavaDOMGlobals::log, PR_LOG_WARNING, - ("Unknown type of UI event (%s)", type)); + ("Unknown type of UI event (%s)", buffer)); + delete[] buffer; clazz = uiEventClass; } - delete [] type; - event->Release(); event = (nsIDOMEvent *) target; } diff --git a/java/dom/jni/org_mozilla_dom_events_EventImpl.cpp b/java/dom/jni/org_mozilla_dom_events_EventImpl.cpp index 87a5bcc55b0..2c95ad451f2 100644 --- a/java/dom/jni/org_mozilla_dom_events_EventImpl.cpp +++ b/java/dom/jni/org_mozilla_dom_events_EventImpl.cpp @@ -151,16 +151,13 @@ JNIEXPORT jstring JNICALL Java_org_mozilla_dom_events_EventImpl_getType return NULL; } - char* cret = ret.ToNewCString(); - jstring jret = env->NewStringUTF(cret); + jstring jret = env->NewString(ret.GetUnicode(), ret.Length()); if (!jret) { JavaDOMGlobals::ThrowException(env, "Event.getType: NewStringUTF failed"); return NULL; } - delete[] cret; - return jret; }