Skip to content

Commit 19bcbc2

Browse files
junak-michaldblock
authored andcommitted
Fixed memory allocation in LPWSTR and LPSTR constructors.
1 parent dc7b4bb commit 19bcbc2

3 files changed

Lines changed: 64 additions & 3 deletions

File tree

CHANGES.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ Bug Fixes
3535
* Remove unsupported JAWT from OSX build - [@twall](https://github.com/twall).
3636
* Disable WebStart tests - [@twall](https://github.com/twall).
3737
* 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.
38-
* Weakly hold registered Direct-mapped classes - [@twall](https://github.com/twall).
38+
* Weakly hold registered Direct-mapped classes - [@twall](https://github.com/twall).
39+
* [#382](https://github.com/twall/jna/pull/382): Fixed memory allocation in `com.sun.jna.platform.win32.WTypes.LPWSTR` and `LPSTR` constructors - [@junak-michal](https://github.com/junak-michal).
3940

4041
Release 4.1
4142
===========

contrib/platform/src/com/sun/jna/platform/win32/WTypes.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ public LPSTR(Pointer pointer) {
135135
}
136136

137137
public LPSTR(String value) {
138-
this();
138+
this(new Memory((value.length() + 1L) * Native.WCHAR_SIZE));
139139
this.setValue(value);
140140
}
141141

@@ -172,7 +172,7 @@ public LPWSTR(Pointer pointer) {
172172
}
173173

174174
public LPWSTR(String value) {
175-
this();
175+
this(new Memory((value.length() + 1L) * Native.WCHAR_SIZE));
176176
this.setValue(value);
177177
}
178178

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/* Copyright (c) 2007-2014 Timothy Wall, All Rights Reserved
2+
*
3+
* This library is free software; you can redistribute it and/or
4+
* modify it under the terms of the GNU Lesser General Public
5+
* License as published by the Free Software Foundation; either
6+
* version 2.1 of the License, or (at your option) any later version.
7+
*
8+
* This library is distributed in the hope that it will be useful,
9+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11+
* Lesser General Public License for more details.
12+
*/
13+
package com.sun.jna.platform.win32;
14+
15+
import com.sun.jna.Memory;
16+
import com.sun.jna.Native;
17+
import com.sun.jna.Pointer;
18+
import junit.framework.TestCase;
19+
20+
public class WTypesTest extends TestCase {
21+
22+
private static final String TEST_STRING = "input";
23+
24+
private static final Pointer TEST_POINTER = new Memory((TEST_STRING.length() + 1L) * Native.WCHAR_SIZE);
25+
26+
static {
27+
TEST_POINTER.setWideString(0, TEST_STRING);
28+
}
29+
30+
public void testLPOLESTRConstruction() {
31+
WTypes.LPOLESTR fromString = new WTypes.LPOLESTR(TEST_STRING);
32+
assertEquals(fromString.getValue(), TEST_STRING);
33+
WTypes.LPOLESTR empty = new WTypes.LPOLESTR();
34+
assertNull(empty.getValue());
35+
WTypes.LPOLESTR fromPointer = new WTypes.LPOLESTR(TEST_POINTER);
36+
assertEquals(fromPointer.getValue(), TEST_STRING);
37+
}
38+
39+
public void testLPSTRConstruction() {
40+
WTypes.LPSTR instance = new WTypes.LPSTR(TEST_STRING);
41+
assertEquals(instance.getValue(), TEST_STRING);
42+
WTypes.LPSTR empty = new WTypes.LPSTR();
43+
assertNull(empty.getValue());
44+
WTypes.LPSTR fromPointer = new WTypes.LPSTR(TEST_POINTER);
45+
assertEquals(fromPointer.getValue(), TEST_STRING);
46+
}
47+
48+
public void testLPWSTRConstruction() {
49+
WTypes.LPWSTR instance = new WTypes.LPWSTR(TEST_STRING);
50+
assertEquals(instance.getValue(), TEST_STRING);
51+
WTypes.LPWSTR empty = new WTypes.LPWSTR();
52+
assertNull(empty.getValue());
53+
WTypes.LPWSTR fromPointer = new WTypes.LPWSTR(TEST_POINTER);
54+
assertEquals(fromPointer.getValue(), TEST_STRING);
55+
}
56+
57+
public static void main(String[] args) {
58+
junit.textui.TestRunner.run(WTypesTest.class);
59+
}
60+
}

0 commit comments

Comments
 (0)