Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add fix for issue #135 in Advapi32Util (reading empty values)

With this fix it is possible to read values from registry of the following
types even when the size of the value is 0:

REG_SZ
REG_EXPAND_SZ
REG_MULTI_SZ
REG_NONE
REG_BINARY (fixed by earlier committer)

Advapi32UtilTest updated with unit test triggering the bug.
  • Loading branch information...
commit 018ad7f2c0762b2d9a4d806dc0554b8a75f75527 1 parent c1a7fd7
@danwi danwi authored
View
24 contrib/platform/src/com/sun/jna/platform/win32/Advapi32Util.java
@@ -1262,8 +1262,28 @@ public static void registryDeleteValue(HKEY root, String keyPath, String valueNa
String nameString = Native.toString(name);
- if(lpcbData.getValue() == 0 && lpType.getValue() == WinNT.REG_BINARY) {
- keyValues.put(nameString, new byte[0]);
+ if(lpcbData.getValue() == 0) {
+ switch (lpType.getValue()) {
+ case WinNT.REG_BINARY: {
+ keyValues.put(nameString, new byte[0]);
+ break;
+ }
+ case WinNT.REG_SZ:
+ case WinNT.REG_EXPAND_SZ: {
+ keyValues.put(nameString, new char[0]);
+ break;
+ }
+ case WinNT.REG_MULTI_SZ: {
+ keyValues.put(nameString, new String[0]);
+ break;
+ }
+ case WinNT.REG_NONE: {
+ keyValues.put(nameString, null);
+ break;
+ }
+ default:
+ throw new RuntimeException("Unsupported empty type: " + lpType.getValue());
+ }
continue;
}
View
42 contrib/platform/test/com/sun/jna/platform/win32/Advapi32UtilTest.java
@@ -26,6 +26,8 @@
import com.sun.jna.platform.win32.WinNT.PSID;
import com.sun.jna.platform.win32.WinNT.SID_NAME_USE;
import com.sun.jna.platform.win32.WinNT.WELL_KNOWN_SID_TYPE;
+import com.sun.jna.platform.win32.WinReg.HKEY;
+import com.sun.jna.platform.win32.WinReg.HKEYByReference;
/**
* @author dblock[at]dblock[dot]org
@@ -316,6 +318,46 @@ public void testRegistryGetValues() {
assertEquals(0, stringsRead.length);
Advapi32Util.registryDeleteKey(WinReg.HKEY_CURRENT_USER, "Software", "JNA");
}
+
+ public void testRegistryGetEmptyValues() {
+ HKEY root = WinReg.HKEY_CURRENT_USER;
+ String keyPath = "Software\\JNA";
+ Advapi32Util.registryCreateKey(root, "Software", "JNA");
+ doTestRegistryGetEmptyValues(root, keyPath, WinNT.REG_BINARY);
+ doTestRegistryGetEmptyValues(root, keyPath, WinNT.REG_EXPAND_SZ);
+ doTestRegistryGetEmptyValues(root, keyPath, WinNT.REG_MULTI_SZ);
+ doTestRegistryGetEmptyValues(root, keyPath, WinNT.REG_NONE);
+ doTestRegistryGetEmptyValues(root, keyPath, WinNT.REG_SZ);
+ Advapi32Util.registryDeleteKey(root, "Software", "JNA");
+ }
+
+ private void doTestRegistryGetEmptyValues(HKEY root, String keyPath, int valueType) {
+ String valueName = "EmptyValue";
+ registrySetEmptyValue(root, keyPath, valueName, valueType);
+ Map<String, Object> values = Advapi32Util.registryGetValues(root, keyPath);
+ assertEquals(1, values.size());
+ assertTrue(values.containsKey(valueName));
+ }
+
+ private static void registrySetEmptyValue(HKEY root, String keyPath, String name, final int valueType) {
+ HKEYByReference phkKey = new HKEYByReference();
+ int rc = Advapi32.INSTANCE.RegOpenKeyEx(root, keyPath, 0, WinNT.KEY_READ | WinNT.KEY_WRITE, phkKey);
+ if (rc != W32Errors.ERROR_SUCCESS) {
+ throw new Win32Exception(rc);
+ }
+ try {
+ char[] data = new char[0];
+ rc = Advapi32.INSTANCE.RegSetValueEx(phkKey.getValue(), name, 0, valueType, data, 0);
+ if (rc != W32Errors.ERROR_SUCCESS) {
+ throw new Win32Exception(rc);
+ }
+ } finally {
+ rc = Advapi32.INSTANCE.RegCloseKey(phkKey.getValue());
+ if (rc != W32Errors.ERROR_SUCCESS) {
+ throw new Win32Exception(rc);
+ }
+ }
+ }
public void testIsWellKnownSid() {
String everyoneString = "S-1-1-0";
Please sign in to comment.
Something went wrong with that request. Please try again.