From 25c3a927e70ba0473fe70f1339c02b02f0b2565e Mon Sep 17 00:00:00 2001 From: Doug MacEachern Date: Mon, 6 Dec 2004 01:06:15 +0000 Subject: [PATCH] better to make a copy than modify the const string --- bindings/java/src/jni/win32/registrykey.cpp | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/bindings/java/src/jni/win32/registrykey.cpp b/bindings/java/src/jni/win32/registrykey.cpp index 5c40adc6..01566cea 100644 --- a/bindings/java/src/jni/win32/registrykey.cpp +++ b/bindings/java/src/jni/win32/registrykey.cpp @@ -111,13 +111,22 @@ JNIEXPORT jlong SIGAR_JNI(win32_RegistryKey_RegOpenKey) HKEY hkeyResult = NULL; jsize len = env->GetStringLength(subkey); LPTSTR lpSubkey = (LPTSTR)env->GetStringChars(subkey, NULL); - char orig = lpSubkey[len]; - /* required under IBM/WebSphere 4.0 for certain keys */ - lpSubkey[len] = '\0'; - RegOpenKey((HKEY)hkey, lpSubkey, &hkeyResult); - lpSubkey[len] = orig; - env->ReleaseStringChars(subkey, (const jchar *)lpSubkey); + LPTSTR copy; + /* required under IBM/WebSphere 4.0 for certain keys */ + if (lpSubkey[len] != '\0') { + copy = wcsdup(lpSubkey); + copy[len] = '\0'; + } + else { + copy = lpSubkey; + } + RegOpenKey((HKEY)hkey, copy, &hkeyResult); + + env->ReleaseStringChars(subkey, (const jchar *)lpSubkey); + if (copy != lpSubkey) { + free(copy); + } return (jlong)hkeyResult; }