From d03b35fe68616627c395f75d493767330e807ddb Mon Sep 17 00:00:00 2001 From: Michal Junak Date: Sat, 15 Nov 2014 20:28:24 +0100 Subject: [PATCH 1/3] Fixed: Memory is correctly allocated when constructing LPWSTR or LPSTR from Java String. Unallocated memory caused NullPointerException. Tests for this fix added. --- .../com/sun/jna/platform/win32/WTypes.java | 4 +- .../sun/jna/platform/win32/WTypesTest.java | 60 +++++++++++++++++++ 2 files changed, 62 insertions(+), 2 deletions(-) create mode 100644 contrib/platform/test/com/sun/jna/platform/win32/WTypesTest.java diff --git a/contrib/platform/src/com/sun/jna/platform/win32/WTypes.java b/contrib/platform/src/com/sun/jna/platform/win32/WTypes.java index e997f95e22..c4873128ef 100644 --- a/contrib/platform/src/com/sun/jna/platform/win32/WTypes.java +++ b/contrib/platform/src/com/sun/jna/platform/win32/WTypes.java @@ -135,7 +135,7 @@ public LPSTR(Pointer pointer) { } public LPSTR(String value) { - this(); + this(new Memory((value.length() + 1L) * Native.WCHAR_SIZE)); this.setValue(value); } @@ -172,7 +172,7 @@ public LPWSTR(Pointer pointer) { } public LPWSTR(String value) { - this(); + this(new Memory((value.length() + 1L) * Native.WCHAR_SIZE)); this.setValue(value); } diff --git a/contrib/platform/test/com/sun/jna/platform/win32/WTypesTest.java b/contrib/platform/test/com/sun/jna/platform/win32/WTypesTest.java new file mode 100644 index 0000000000..c5d07debb1 --- /dev/null +++ b/contrib/platform/test/com/sun/jna/platform/win32/WTypesTest.java @@ -0,0 +1,60 @@ +/* Copyright (c) 2007-2013 Timothy Wall, All Rights Reserved + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + *

+ * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + */ +package com.sun.jna.platform.win32; + +import com.sun.jna.Memory; +import com.sun.jna.Native; +import com.sun.jna.Pointer; +import junit.framework.TestCase; + +public class WTypesTest extends TestCase { + + private static final String TEST_STRING = "input"; + + private static final Pointer TEST_POINTER = new Memory((TEST_STRING.length() + 1L) * Native.WCHAR_SIZE); + + static { + TEST_POINTER.setWideString(0, TEST_STRING); + } + + public void testLPOLESTRConstruction() { + WTypes.LPOLESTR fromString = new WTypes.LPOLESTR(TEST_STRING); + assertEquals(fromString.getValue(), TEST_STRING); + WTypes.LPOLESTR empty = new WTypes.LPOLESTR(); + assertNull(empty.getValue()); + WTypes.LPOLESTR fromPointer = new WTypes.LPOLESTR(TEST_POINTER); + assertEquals(fromPointer.getValue(), TEST_STRING); + } + + public void testLPSTRConstruction() { + WTypes.LPSTR instance = new WTypes.LPSTR(TEST_STRING); + assertEquals(instance.getValue(), TEST_STRING); + WTypes.LPSTR empty = new WTypes.LPSTR(); + assertNull(empty.getValue()); + WTypes.LPSTR fromPointer = new WTypes.LPSTR(TEST_POINTER); + assertEquals(fromPointer.getValue(), TEST_STRING); + } + + public void testLPWSTRConstruction() { + WTypes.LPWSTR instance = new WTypes.LPWSTR(TEST_STRING); + assertEquals(instance.getValue(), TEST_STRING); + WTypes.LPWSTR empty = new WTypes.LPWSTR(); + assertNull(empty.getValue()); + WTypes.LPWSTR fromPointer = new WTypes.LPWSTR(TEST_POINTER); + assertEquals(fromPointer.getValue(), TEST_STRING); + } + + public static void main(String[] args) { + junit.textui.TestRunner.run(WTypesTest.class); + } +} From a34e28c3b432825062aad399e453b1037a13a200 Mon Sep 17 00:00:00 2001 From: Michal Junak Date: Mon, 17 Nov 2014 13:11:39 +0100 Subject: [PATCH 2/3] Mentioned LPWSTR and LPSTR memory allocation fix in CHANGES.md. Fixed license header in WTypesTest.java. --- CHANGES.md | 1 + .../sun/jna/platform/win32/WTypesTest.java | 20 +++++++++---------- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 5c787dce07..d4cc13a0f1 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -36,6 +36,7 @@ Bug Fixes * Disable WebStart tests - [@twall](https://github.com/twall). * Dispose all native resources when JNA's native library is unloaded - Paul Grütter of signotec GmbH and [@twall](https://github.com/twall). This fixes a number of seemingly random, sporadic crashes on windows. * Weakly hold registered Direct-mapped classes - [@twall](https://github.com/twall). +* Fixed memory allocation in classes `com.sun.jna.platform.win32.WTypes.LPWSTR` and `com.sun.jna.platform.win32.WTypes.LPSTR`. Release 4.1 =========== diff --git a/contrib/platform/test/com/sun/jna/platform/win32/WTypesTest.java b/contrib/platform/test/com/sun/jna/platform/win32/WTypesTest.java index c5d07debb1..eb6c9829fb 100644 --- a/contrib/platform/test/com/sun/jna/platform/win32/WTypesTest.java +++ b/contrib/platform/test/com/sun/jna/platform/win32/WTypesTest.java @@ -1,10 +1,10 @@ -/* Copyright (c) 2007-2013 Timothy Wall, All Rights Reserved - * +/* Copyright (c) 2011 Timothy Wall, All Rights Reserved + * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. - *

+ * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU @@ -18,15 +18,15 @@ import junit.framework.TestCase; public class WTypesTest extends TestCase { - + private static final String TEST_STRING = "input"; - + private static final Pointer TEST_POINTER = new Memory((TEST_STRING.length() + 1L) * Native.WCHAR_SIZE); - + static { TEST_POINTER.setWideString(0, TEST_STRING); } - + public void testLPOLESTRConstruction() { WTypes.LPOLESTR fromString = new WTypes.LPOLESTR(TEST_STRING); assertEquals(fromString.getValue(), TEST_STRING); @@ -35,7 +35,7 @@ public void testLPOLESTRConstruction() { WTypes.LPOLESTR fromPointer = new WTypes.LPOLESTR(TEST_POINTER); assertEquals(fromPointer.getValue(), TEST_STRING); } - + public void testLPSTRConstruction() { WTypes.LPSTR instance = new WTypes.LPSTR(TEST_STRING); assertEquals(instance.getValue(), TEST_STRING); @@ -44,7 +44,7 @@ public void testLPSTRConstruction() { WTypes.LPSTR fromPointer = new WTypes.LPSTR(TEST_POINTER); assertEquals(fromPointer.getValue(), TEST_STRING); } - + public void testLPWSTRConstruction() { WTypes.LPWSTR instance = new WTypes.LPWSTR(TEST_STRING); assertEquals(instance.getValue(), TEST_STRING); @@ -53,7 +53,7 @@ public void testLPWSTRConstruction() { WTypes.LPWSTR fromPointer = new WTypes.LPWSTR(TEST_POINTER); assertEquals(fromPointer.getValue(), TEST_STRING); } - + public static void main(String[] args) { junit.textui.TestRunner.run(WTypesTest.class); } From 5bf7c34005cbf67bd4a658cd0be9e5fafdc39aa4 Mon Sep 17 00:00:00 2001 From: Michal Junak Date: Mon, 17 Nov 2014 13:57:21 +0100 Subject: [PATCH 3/3] Yet another fix to license header of WTypesTest.java file. --- .../platform/test/com/sun/jna/platform/win32/WTypesTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contrib/platform/test/com/sun/jna/platform/win32/WTypesTest.java b/contrib/platform/test/com/sun/jna/platform/win32/WTypesTest.java index eb6c9829fb..cf01c210b2 100644 --- a/contrib/platform/test/com/sun/jna/platform/win32/WTypesTest.java +++ b/contrib/platform/test/com/sun/jna/platform/win32/WTypesTest.java @@ -1,4 +1,4 @@ -/* Copyright (c) 2011 Timothy Wall, All Rights Reserved +/* Copyright (c) 2007-2014 Timothy Wall, All Rights Reserved * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public