Skip to content

Commit

Permalink
Optimize Structure#validate and prevent AIOOBE in SAFEARRAY#read for …
Browse files Browse the repository at this point in the history
…zero dimensions
  • Loading branch information
matthiasblaesing committed Feb 25, 2018
1 parent 37f9629 commit 5662a6b
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Bug Fixes
* [#882](https://github.com/java-native-access/jna/pull/882): Correctly close file in `ELFAnalyser#runDetection`, fix suggested by [@Sylvyrfysh](https://github.com/Sylvyrfysh) in [#880](https://github.com/java-native-access/jna/pull/880) - [@matthiasblaesing](https://github.com/matthiasblaesing).
* [#887](https://github.com/java-native-access/jna/issues/887): MacFileUtils.moveToTrash() doesn't work in a sandboxed app fix suggested by [@sobakasu](https://github.com/sobakasu) - [@matthiasblaesing](https://github.com/matthiasblaesing).
* [#894](https://github.com/java-native-access/jna/issues/894): NullPointerException can be caused by calling `com.sun.jna.platform.win32.COM.util.ProxyObject#dispose` multiple times - [@matthiasblaesing](https://github.com/matthiasblaesing).
* [#925](https://github.com/java-native-access/jna/issues/925): Optimize `Structure#validate` and prevent `ArrayIndexOutOfBoundsException` in `SAFEARRAY#read` for zero dimensions - [@matthiasblaesing](https://github.com/matthiasblaesing).

Breaking Changes
----------------
Expand Down
6 changes: 5 additions & 1 deletion contrib/platform/src/com/sun/jna/platform/win32/OaIdl.java
Original file line number Diff line number Diff line change
Expand Up @@ -607,7 +607,11 @@ public SAFEARRAY(Pointer pointer) {
@Override
public void read() {
super.read();
rgsabound = (SAFEARRAYBOUND[]) rgsabound[0].toArray(cDims.intValue());
if(cDims.intValue() > 0) {
rgsabound = (SAFEARRAYBOUND[]) rgsabound[0].toArray(cDims.intValue());
} else {
rgsabound = new SAFEARRAYBOUND[]{ new SAFEARRAYBOUND() };
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
package com.sun.jna.platform.win32;

import com.sun.jna.Pointer;
import com.sun.jna.Structure;
import com.sun.jna.platform.win32.COM.COMException;
import com.sun.jna.platform.win32.COM.COMUtils;
import com.sun.jna.platform.win32.COM.util.ObjectFactory;
Expand Down Expand Up @@ -61,6 +62,7 @@
import static com.sun.jna.platform.win32.OaIdlUtil.toPrimitiveArray;
import com.sun.jna.platform.win32.WTypes.VARTYPE;
import com.sun.jna.platform.win32.WinDef.LONG;
import java.lang.reflect.Field;

public class SAFEARRAYTest {
static {
Expand All @@ -82,6 +84,15 @@ public void testCreateVarArray() {
SAFEARRAY varArray = SAFEARRAY.createSafeArray(1);
Assert.assertTrue(varArray != null);
}

@Test
public void testCreateEmpty() throws NoSuchFieldException, IllegalArgumentException, IllegalAccessException {
Field f = Structure.class.getDeclaredField("PLACEHOLDER_MEMORY");
f.setAccessible(true);
Pointer PLACEHOLDER_MEMORY = (Pointer) f.get(null);
SAFEARRAY sa = Structure.newInstance(SAFEARRAY.class, PLACEHOLDER_MEMORY);
Assert.assertTrue(sa != null);
}

@Test
public void testSafeArrayPutGetElement() throws Exception {
Expand Down
9 changes: 8 additions & 1 deletion src/com/sun/jna/Structure.java
Original file line number Diff line number Diff line change
Expand Up @@ -2125,6 +2125,13 @@ protected int getNativeSize(Class<?> nativeType, Object value) {
* @param cls Structure subclass to check
*/
static void validate(Class<? extends Structure> cls) {
Structure.newInstance(cls, PLACEHOLDER_MEMORY);
try {
cls.getConstructor();
return;
}catch(NoSuchMethodException e) {
}
catch(SecurityException e) {
}
throw new IllegalArgumentException("No suitable constructor found for class: " + cls.getName());
}
}

0 comments on commit 5662a6b

Please sign in to comment.